2

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.

  • Loop through the whole ArrayOfObjects and Assert if the Key exists and if it exists assert the value.

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

SOAP structure looks like this:

<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>

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//

And the object looks like:

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

2 回答 2

3

您可以使用Script Assertion相同的肥皂请求测试步骤来验证相同的内容,而无需添加额外的Groovy Script测试步骤。

不确定上述示例是否具有相同的 xml 结构(如果不准确)。否则,请采用适合您需要的更改。

这是方法:

  • 由于有复杂的数据需要验证,并且有key、value的模式;因此,将地图定义为您上次指出的预期数据。
  • 阅读 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

您可以在线快速试用demo

于 2017-10-27T04:08:46.213 回答
1

下面的代码首先获取数组(x)中的所有键和另一个数组y(x)中的所有值。如果没有,您可以将它们添加到地图中,您可以直接断言值

以下是验证键值对的最简单的方法之一

只需将“第一步”替换为生成响应的步骤的名称

def groovyUtils = new  com.eviware.soapui.support.GroovyUtils(context)

def response = groovyUtils.getXmlHolder("First Step#Response")

def x = response.getNodeValues("//*[local-name()='ArrayOfObjects']//*[local-name()='Key']")
for ( def keys in x)
{
log.info keys
}
 def y = response.getNodeValues("//*[local-name()='ArrayOfObjects']//*[local-name()='Value']")
for ( def values in y)
 {
log.info values
}

log.info "total keys are " + x.size()

for(int i = 0 ; i < x.size() ; i++)
{
 log.info " key = " + x[i] + " Value = " + y[i] 
}

这是我运行上述脚本时的结果

IST 2017 年 10 月 28 日星期六 14:41:57:INFO:Key1

IST 2017 年 10 月 28 日星期六 14:41:57:INFO:Key2

IST 2017 年 10 月 28 日星期六 14:41:57:INFO:Key40

IST 2017 年 10 月 28 日星期六 14:41:57:INFO:Value1

IST 2017 年 10 月 28 日星期六 14:41:57:INFO:Value2

IST 2017 年 10 月 28 日星期六 14:41:57:INFO:Value40

Sat Oct 28 14:41:57 IST 2017:INFO:total keys are 3

IST 2017 年 10 月 28 日星期六 14:41:57:信息:键 = 键 1 值 = 值 1

IST 2017 年 10 月 28 日星期六 14:41:57:信息:键 = 键 2 值 = 值 2

10 月 28 日星期六 14:41:57 IST 2017:INFO: key = Key40 Value = Value40

于 2017-10-28T09:17:24.030 回答