SOAP UI XML响应中的键/值对的Groovy脚本断言 [英] Groovy Script assert on Key/Value pairs in SOAP UI XML response

查看:117
本文介绍了SOAP UI XML响应中的键/值对的Groovy脚本断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ready API/SOAP UI.我添加了一个SOAP请求,并且获得了SOAP响应XML.我的响应对象最多有40个键/值对.

I am using Ready API/SOAP UI. I added a SOAP request and I get a SOAP response XML. My response object has up to 40 Key/Value pairs.

我有功能测试来专门测试每个.

I have functional tests to specifically test each.

  • 遍历整个ArrayOfObjects并断言Key是否存在以及是否存在断言值.

在这种情况下,我能否获得可行的解决方案.我无法在输出对象上断言.

Can I get a working solution for this scenario. I am unable to do assert on the output object.

SOAP结构如下:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>  
     <ArrayOfallObjects>
       <ArrayOfObjects>
          <Key>Key1</Key>
          <Value>Value1</Value>
       </ArrayOfObjects>
       <ArrayOfObjects>
          <Key>Key2</Key>
          <Value>Value2</Value>
       </ArrayOfObjects>
       ---------------
       <ArrayOfObjects>
          <Key>Key40</Key>
          <Value>Value40</Value>
       </ArrayOfObjects>
     </ArrayOfallObjects>   
   </soap:Body>
</soap:Envelope>

我正在使用如下的groovy脚本片段

And I am using groovy script snippet as below

//Code Snippet Starts//    
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)    
def request = context.testCase.getTestStepByName("RequestName")    
def responseCurrentHolder = groovyUtils.getXmlHolder( request.name +"#Response")
responseCurrentHolder.namespaces["ns1"] = "https://test.com"
def nodes = responseCurrentHolder.getDomNodes( "//ns1:Response/ns1:Result/ns1:ArrayOfallObjects/ns1:ArrayOfObject/*" )
def nodeCount = responseCurrentHolder.getNodeValues("//ns1:Response/ns1:Result/ns1:ArrayOfallObjects/ns1:ArrayOfObject/ns1:Key").length

def object = [:]
for (def nodeIndex = 1; nodeIndex <= nodeCount; nodeIndex++) {    
def nodeKey = responseCurrentHolder.getNodeValues("//ns1:Response/ns1:Result/ns1:ArrayOfallObjects/ns1:ArrayOfObject[$nodeIndex]/ns1:Key/text()")    
def nodeValue = responseCurrentHolder.getNodeValue("//ns1:Response/ns1:Result/ns1:ArrayOfallObjects/ns1:ArrayOfObject[$nodeIndex]/ns1:Value/text()")

  object.put( nodeKey,nodeValue)
}
log.info "Object =" +object

// Code snippet ends//

对象看起来像:

Object =[[Key1]:Value1, [Key2]:Value2, and so on upto --,[Key40]:Value40]

推荐答案

您可以将Script Assertion用于同一肥皂请求测试步骤以验证同一步骤,而无需添加其他Groovy Script测试步骤.

You can use Script Assertion for the same soap request test step to verify the same without adding additional Groovy Script test step.

不确定上面的示例是否具有相同的xml结构(如果不完全相同).否则,请根据您的需要进行更改.

Not sure if above sample has the same xml structure (if not exact). Otherwise, adopt the changes to suit your need.

这是方法:

  • 由于有待验证的复杂数据,并且有键,值模式;因此,将地图定义为期望的数据,就像您上次指出的那样.
  • 读取xml响应,并从中创建类似的映射.有时,可能需要按键(或基于数据的其他条件)对检索到的数据进行排序.
  • 然后检查预期数据和实际数据.

脚本断言:

//Define expected data; using few elements as sample
def expectedMap = [Key1: 'Value1', Key2: 'Value2', Key40: 'Value40']

//Check if there is response
assert context.response, 'Response is empty or null'

//Parse the response
def xml = new XmlSlurper().parseText(context.response)

//Extract the data, create actual map and sort by key
def actualMap = xml.'**'.findAll {it.name() == 'ArrayOfObjects' }.collectEntries {[(it.Key.text()): it.Value.text()]}​?.sort {it.key}
log.info actualMap
assert expectedMap == actualMap

您可以快速在线尝试 演示

You can quickly try it online demo

这篇关于SOAP UI XML响应中的键/值对的Groovy脚本断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆