我尝试使用以下代码在SOAP UI中使用groovy将值插入数据存储区。
问题是它只在数据存储中插入一行。
import groovy.json.JsonSlurper
def utils = new com.eviware.soapui.support.GroovyUtils( context );
def project = testRunner.testCase.testSuite.project ;
def tcase = project.testSuites["MyTestSuite"].testCases["MyTestCase"] ;
def tstep = tcase.getTestStepByName("GetShops");
def JSONresponse = tstep.getPropertyValue("response");
def slurper = new JsonSlurper()
def jsonElements = slurper.parseText(JSONresponse)
for ( value in jsonElements.shopId ) {
log.info value
result["IDS"] = value
}
即使日志显示多个值,但最后一个值也会插入到数据存储区IDS列中。
我在这里缺少什么?
答案 0 :(得分:0)
我猜result
是Map
或类似地图的对象,所以每次迭代都要循环
for ( value in jsonElements.shopId ) {
log.info value
result["IDS"] = value
}
在“IDS”键下替换存储在此地图中的值。
答案 1 :(得分:0)
parseText
函数返回a data structure of lists and maps。做一个log.info
来看看json究竟是什么样的。当你使用jsonElements.shopId
时,你得到的将是地图。您应该将其作为地图访问。
尝试使用以下代码循环遍历json响应。
import groovy.json.JsonSlurper
def jsonResp = context.expand('${CitiesJSON - Request 1#Response}')
def jsonElements = new JsonSlurper().parseText(jsonResp)
for ( value in jsonElements.geonames ) {
value.each{
log.info "${it.key}:${it.value}"
}
}
我使用了geonames web service,CitiesJSON - Request 1
是我的休息测试请求的名称。
/*This code returns the below data
Thu Apr 10 17:29:50 ADT 2014:INFO:fcodeName:capital of a political entity
Thu Apr 10 17:29:50 ADT 2014:INFO:toponymName:Mexico City
Thu Apr 10 17:29:50 ADT 2014:INFO:countrycode:MX
Thu Apr 10 17:29:50 ADT 2014:INFO:fcl:P
Thu Apr 10 17:29:50 ADT 2014:INFO:fclName:city, village,...
Thu Apr 10 17:29:50 ADT 2014:INFO:name:Mexico City
Thu Apr 10 17:29:50 ADT 2014:INFO:wikipedia:en.wikipedia.org/wiki/Mexico_City
Thu Apr 10 17:29:50 ADT 2014:INFO:lng:-99.12766456604
Thu Apr 10 17:29:50 ADT 2014:INFO:fcode:PPLC
Thu Apr 10 17:29:50 ADT 2014:INFO:geonameId:3530597
Thu Apr 10 17:29:50 ADT 2014:INFO:lat:19.428472427036
Thu Apr 10 17:29:50 ADT 2014:INFO:population:12294193
Thu Apr 10 17:29:50 ADT 2014:INFO:fcodeName:capital of a political entity
Thu Apr 10 17:29:50 ADT 2014:INFO:toponymName:Manila
Thu Apr 10 17:29:50 ADT 2014:INFO:countrycode:PH
Thu Apr 10 17:29:50 ADT 2014:INFO:fcl:P
Thu Apr 10 17:29:50 ADT 2014:INFO:fclName:city, village,...
Thu Apr 10 17:29:50 ADT 2014:INFO:name:City of Manila
Thu Apr 10 17:29:50 ADT 2014:INFO:wikipedia:en.wikipedia.org/wiki/Manila
Thu Apr 10 17:29:50 ADT 2014:INFO:lng:120.9822
Thu Apr 10 17:29:50 ADT 2014:INFO:fcode:PPLC
Thu Apr 10 17:29:50 ADT 2014:INFO:geonameId:1701668
Thu Apr 10 17:29:50 ADT 2014:INFO:lat:14.6042
Thu Apr 10 17:29:50 ADT 2014:INFO:population:10444527
Thu Apr 10 17:29:50 ADT 2014:INFO:fcodeName:capital of a political entity
Thu Apr 10 17:29:50 ADT 2014:INFO:toponymName:Dhaka
Thu Apr 10 17:29:50 ADT 2014:INFO:countrycode:BD
Thu Apr 10 17:29:50 ADT 2014:INFO:fcl:P
Thu Apr 10 17:29:50 ADT 2014:INFO:fclName:city, village,...
Thu Apr 10 17:29:50 ADT 2014:INFO:name:Dhaka
Thu Apr 10 17:29:50 ADT 2014:INFO:wikipedia:en.wikipedia.org/wiki/Dhaka
Thu Apr 10 17:29:50 ADT 2014:INFO:lng:90.40743827819824
Thu Apr 10 17:29:50 ADT 2014:INFO:fcode:PPLC
Thu Apr 10 17:29:50 ADT 2014:INFO:geonameId:1185241
Thu Apr 10 17:29:50 ADT 2014:INFO:lat:23.710395616597037
Thu Apr 10 17:29:50 ADT 2014:INFO:population:10356500
Thu Apr 10 17:29:50 ADT 2014:INFO:fcodeName:capital of a political entity
Thu Apr 10 17:29:50 ADT 2014:INFO:toponymName:Seoul
Thu Apr 10 17:29:50 ADT 2014:INFO:countrycode:KR
Thu Apr 10 17:29:50 ADT 2014:INFO:fcl:P
Thu Apr 10 17:29:50 ADT 2014:INFO:fclName:city, village,...
Thu Apr 10 17:29:50 ADT 2014:INFO:name:Seoul
Thu Apr 10 17:29:50 ADT 2014:INFO:wikipedia:en.wikipedia.org/wiki/Seoul
Thu Apr 10 17:29:50 ADT 2014:INFO:lng:126.9784
Thu Apr 10 17:29:50 ADT 2014:INFO:fcode:PPLC
Thu Apr 10 17:29:50 ADT 2014:INFO:geonameId:1835848
Thu Apr 10 17:29:50 ADT 2014:INFO:lat:37.566
Thu Apr 10 17:29:50 ADT 2014:INFO:population:10349312
Thu Apr 10 17:29:50 ADT 2014:INFO:fcodeName:capital of a political entity
Thu Apr 10 17:29:50 ADT 2014:INFO:toponymName:Jakarta
Thu Apr 10 17:29:50 ADT 2014:INFO:countrycode:ID
Thu Apr 10 17:29:50 ADT 2014:INFO:fcl:P
Thu Apr 10 17:29:50 ADT 2014:INFO:fclName:city, village,...
Thu Apr 10 17:29:50 ADT 2014:INFO:name:Jakarta
Thu Apr 10 17:29:50 ADT 2014:INFO:wikipedia:en.wikipedia.org/wiki/Jakarta
Thu Apr 10 17:29:50 ADT 2014:INFO:lng:106.84513092041016
Thu Apr 10 17:29:50 ADT 2014:INFO:fcode:PPLC
Thu Apr 10 17:29:50 ADT 2014:INFO:geonameId:1642911
Thu Apr 10 17:29:50 ADT 2014:INFO:lat:-6.214623197035775
Thu Apr 10 17:29:50 ADT 2014:INFO:population:8540121
Thu Apr 10 17:29:50 ADT 2014:INFO:fcodeName:capital of a political entity
Thu Apr 10 17:29:50 ADT 2014:INFO:toponymName:Tokyo
Thu Apr 10 17:29:50 ADT 2014:INFO:countrycode:JP
Thu Apr 10 17:29:50 ADT 2014:INFO:fcl:P
Thu Apr 10 17:29:50 ADT 2014:INFO:fclName:city, village,...
Thu Apr 10 17:29:50 ADT 2014:INFO:name:Tokyo
Thu Apr 10 17:29:50 ADT 2014:INFO:wikipedia:en.wikipedia.org/wiki/Tokyo
Thu Apr 10 17:29:50 ADT 2014:INFO:lng:139.69171
Thu Apr 10 17:29:50 ADT 2014:INFO:fcode:PPLC
Thu Apr 10 17:29:50 ADT 2014:INFO:geonameId:1850147
Thu Apr 10 17:29:50 ADT 2014:INFO:lat:35.6895
Thu Apr 10 17:29:50 ADT 2014:INFO:population:8336599
Thu Apr 10 17:29:50 ADT 2014:INFO:fcodeName:capital of a political entity
Thu Apr 10 17:29:50 ADT 2014:INFO:toponymName:Taipei
Thu Apr 10 17:29:50 ADT 2014:INFO:countrycode:TW
Thu Apr 10 17:29:50 ADT 2014:INFO:fcl:P
Thu Apr 10 17:29:50 ADT 2014:INFO:fclName:city, village,...
Thu Apr 10 17:29:50 ADT 2014:INFO:name:Taipei
Thu Apr 10 17:29:50 ADT 2014:INFO:wikipedia:en.wikipedia.org/wiki/Taipei
Thu Apr 10 17:29:50 ADT 2014:INFO:lng:121.531846
Thu Apr 10 17:29:50 ADT 2014:INFO:fcode:PPLC
Thu Apr 10 17:29:50 ADT 2014:INFO:geonameId:1668341
Thu Apr 10 17:29:50 ADT 2014:INFO:lat:25.047763
Thu Apr 10 17:29:50 ADT 2014:INFO:population:7871900
Thu Apr 10 17:29:50 ADT 2014:INFO:fcodeName:capital of a political entity
Thu Apr 10 17:29:50 ADT 2014:INFO:toponymName:Bogotá
Thu Apr 10 17:29:50 ADT 2014:INFO:countrycode:CO
Thu Apr 10 17:29:50 ADT 2014:INFO:fcl:P
Thu Apr 10 17:29:50 ADT 2014:INFO:fclName:city, village,...
Thu Apr 10 17:29:50 ADT 2014:INFO:name:Bogotá
Thu Apr 10 17:29:50 ADT 2014:INFO:wikipedia:en.wikipedia.org/wiki/Bogot%C3%A1
Thu Apr 10 17:29:50 ADT 2014:INFO:lng:-74.08175468444824
Thu Apr 10 17:29:50 ADT 2014:INFO:fcode:PPLC
Thu Apr 10 17:29:50 ADT 2014:INFO:geonameId:3688689
Thu Apr 10 17:29:50 ADT 2014:INFO:lat:4.609705849789108
Thu Apr 10 17:29:50 ADT 2014:INFO:population:7674366
Thu Apr 10 17:29:50 ADT 2014:INFO:fcodeName:capital of a political entity
Thu Apr 10 17:29:50 ADT 2014:INFO:toponymName:Beijing
Thu Apr 10 17:29:50 ADT 2014:INFO:countrycode:CN
Thu Apr 10 17:29:50 ADT 2014:INFO:fcl:P
Thu Apr 10 17:29:50 ADT 2014:INFO:fclName:city, village,...
Thu Apr 10 17:29:50 ADT 2014:INFO:name:Beijing
Thu Apr 10 17:29:50 ADT 2014:INFO:wikipedia:en.wikipedia.org/wiki/Beijing
Thu Apr 10 17:29:50 ADT 2014:INFO:lng:116.397228240967
Thu Apr 10 17:29:50 ADT 2014:INFO:fcode:PPLC
Thu Apr 10 17:29:50 ADT 2014:INFO:geonameId:1816670
Thu Apr 10 17:29:50 ADT 2014:INFO:lat:39.9074977414405
Thu Apr 10 17:29:50 ADT 2014:INFO:population:7480601
Thu Apr 10 17:29:50 ADT 2014:INFO:fcodeName:capital of a political entity
Thu Apr 10 17:29:50 ADT 2014:INFO:toponymName:Hong Kong
Thu Apr 10 17:29:50 ADT 2014:INFO:countrycode:HK
Thu Apr 10 17:29:50 ADT 2014:INFO:fcl:P
Thu Apr 10 17:29:50 ADT 2014:INFO:fclName:city, village,...
Thu Apr 10 17:29:50 ADT 2014:INFO:name:Hong Kong
Thu Apr 10 17:29:50 ADT 2014:INFO:wikipedia:en.wikipedia.org/wiki/Hong_Kong
Thu Apr 10 17:29:50 ADT 2014:INFO:lng:114.157691001892
Thu Apr 10 17:29:50 ADT 2014:INFO:fcode:PPLC
Thu Apr 10 17:29:50 ADT 2014:INFO:geonameId:1819729
Thu Apr 10 17:29:50 ADT 2014:INFO:lat:22.2855225817732
Thu Apr 10 17:29:50 ADT 2014:INFO:population:7012738
*/
如果你想访问各个数据集,你可能会尝试这样的事情......
for ( value in jsonElements.geonames ) {
log.info value["name"]
}
/*this returns
Thu Apr 10 17:33:41 ADT 2014:INFO:Mexico City
Thu Apr 10 17:33:41 ADT 2014:INFO:City of Manila
Thu Apr 10 17:33:41 ADT 2014:INFO:Dhaka
Thu Apr 10 17:33:41 ADT 2014:INFO:Seoul
Thu Apr 10 17:33:41 ADT 2014:INFO:Jakarta
Thu Apr 10 17:33:41 ADT 2014:INFO:Tokyo
Thu Apr 10 17:33:41 ADT 2014:INFO:Taipei
Thu Apr 10 17:33:41 ADT 2014:INFO:Bogotá
Thu Apr 10 17:33:41 ADT 2014:INFO:Beijing
Thu Apr 10 17:33:41 ADT 2014:INFO:Hong Kong
*/
数据存储是什么意思?此外,您知道可以通过使代码更加时髦来缩短代码。
答案 2 :(得分:0)
正如我所看到的,您希望存储在属性步骤类型中,您可以执行以下操作
def idsObjectStored = context.expand( '${Properties#ids}' )
def slurper = new JsonSlurper()
idsObjectStored = slurper.parseText(idsObjectStored)
for ( value in jsonElements.shopId ) {
log.info value
idsObjectStored.add(value)
}
在此属性中创建一个条目,即 idsObjectStored ,以[]作为初始内容。 jsonSlurper将在对象中转换它
idsObjectStored = slurper.parseText(idsObjectStored)
现在你只需要迭代并添加所有这些值