如何将所有 SOAP 响应值写入带有分隔符的 CSV 文件 Groovy for SoapUI
How to write all the SOAP response values into CSV file with delimiter in Groovy for SoapUI
我是 Groovy 的新手,正在研究 Soap UI。目前我正在研究 Groovy 模块,我需要在其中将所有带分隔符的标记值写入 .csv
文件。我从其他 post 得到的解决方案特定于 xpath
。
但我正在努力实现以下目标:
- 将所有重复数组的所有值存储在csv文件中
- 每个数组在单行中,带分隔符
预期输出:
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>
这是相同 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())
我是 Groovy 的新手,正在研究 Soap UI。目前我正在研究 Groovy 模块,我需要在其中将所有带分隔符的标记值写入 .csv
文件。我从其他 post 得到的解决方案特定于 xpath
。
但我正在努力实现以下目标:
- 将所有重复数组的所有值存储在csv文件中
- 每个数组在单行中,带分隔符
预期输出:
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>
这是相同 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())