【问题标题】:How to write all the SOAP response values into CSV file with delimiter in Groovy for SoapUI如何在 Groovy for SoapUI 中使用分隔符将所有 SOAP 响应值写入 CSV 文件
【发布时间】:2017-11-22 09:49:44
【问题描述】:

我是 Groovy 的新手,正在研究 Soap UI。目前我正在研究 Groovy 模块,我需要将带有分隔符的所有标记值写入.csv 文件。我从其他帖子中得到的解决方案特定于xpath

但我正在努力实现以下目标:

  1. 所有重复数组的所有值都存储在 csv 文件中
  2. 单行中的每个数组,带分隔符

预期输出:

code;Name;Category;Manufacturer;Price;Stock // as header
1234;product name;some category;manufacturer;100;1
1235;product name2;some category2;manufacturer2;1002;2

XML 示例:

<ns2:personalarray1Response>
    <ns2:personarray1>
        <Code>1234</Code>
        <Name>product name</Name>
        <Category>some category</Category>
        <Manufacturer>manufacturer</Manufacturer>
        <Price>100</Price>
        <Stock>1</Stock>
    </ns2:personarray1>
    <ns2:personarray1>
        <Code>1235</Code>
        <Name>product name2</Name>
        <Category>some category2</Category>
        <Manufacturer>manufacturer2</Manufacturer>
        <Price>1002</Price>
        <Stock>2</Stock>
    </ns2:personarray1>
    <ns2:personarray1>
        <Code>1234</Code>
        <Name>product name</Name>
        <Category>some category</Category>
        <Manufacturer>manufacturer</Manufacturer>
        <Price>100</Price>
        <Stock>1</Stock>
    </ns2:personalarray1>
</ns2:personalarray1Response>

【问题讨论】:

    标签: xml csv groovy soapui


    【解决方案1】:

    这是同一 Soap 请求步骤的Script Assertion,需要不需要才能使用额外的 Groovy 脚本步骤。

    脚本

     //Change file name as needed
    def fileName = '/file/path/to.csv'
    def delimiter = ',' 
    
    assert context.response, 'Response is empty or null'
    
    def xml = new XmlSlurper().parseText(context.response)
    
    def personalInfos = xml.'**'.findAll { it.name() == 'personarray1' }
    
    
    //Create the list of data (person array) 
    def list = personalInfos.collect {info -> info.children()*.name().collectEntries{[(it): info."$it"] } }
    
    
    def sb = new StringBuffer(list[0].keySet().join(delimiter))
    sb.append('\n')
    
    list.collect { sb.append(it.values().join(delimiter)).append('\n')}
    log.info "Data going to be written into file: \n ${sb.toString()}"
    
    new File(fileName).write(sb.toString())
    

    【讨论】:

    • 感谢 Rao 提供代码。但我希望它在一个 groovy 模块中,因为我在运行时在项目路径中创建一个 .csv 文件。我尝试了上面的代码,但收到错误为“wed Nov22 18:30.07 GMT+05:30 2017:ERROR:An erroro occurred [could not find matching constructor for: java:io:file(java.io.file)],有关详细信息,请参阅错误日志”
    • 在 SOAP Ui 日志中,正在获取并显示这些值。但同样没有写入文件。此外,我的响应文件已创建。
    • @SD31,您要么尝试了错误,要么没有按照解决方案中提供的方式尝试。
    • @SD31,如果您查看您在评论中发布的错误,您正在将文件对象传递给文件。如果您已经有文件对象,那么只需file.write(sb.toString())
    • 错误:发生错误 [找不到匹配的构造函数:java:io:file(java.io.file)] groovy.lang.GroovyRuntimeException:找不到匹配的构造函数:java:io :file(java.io.file) 在 groovy.lang.MetaclassImpl.invokeConstructor(MetaclassImpl.java.1550) 在 groovy.lang.MetaclassImpl.invokeConstructor(MetaclassImpl.java.1404) 在 org.codehaus.groovy.runtime.callsite。 MetaclassConstructorSite.callConstructor(MetaclassConstructorSite.Java:46) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.Java:57)
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多