Groovy 在 SOAPUI 中断言预期数据定位的重复节点

Groovy in SOAPUI Asserting on repeating nodes located by expected data

下面是解释我的场景的示例响应

              <ns2:Details xmlns:ns2="http://ww">
                 <ns2:Code>011</ns2:Code>
                 <ns2:Result>4</ns2:Result>
              </ns2:Details>
              <ns2:Details xmlns:ns2="http://ww">
                 <ns2:Code>018</ns2:Code>
                 <ns2:Result>0</ns2:Result>
              </ns2:Details>
              <ns2:Details xmlns:ns2="http://ww">
                 <ns2:Code>098</ns2:Code>
                 <ns2:Result>2</ns2:Result>
              </ns2:Details>

这里我想测试的数据在ns2:Result里面。我已经知道我在这个节点中期望的值是什么,但它必须与正确的 ns2:Code 值相关联。

例如,为了让这个测试通过,我希望

所以我已经知道的数据是代码和结果,但我需要确保为每个代码返回正确的结果值。我不需要验证返回的代码是什么,我只需要根据正确的代码验证结果数字。

创建一个预期结果作为下面的映射,它是 Code and Result 的组合,以便可以一起验证/断言两者。

['011': '4', '018': '0', '098': '2']

对同一请求测试步骤使用脚本断言

脚本如下:

//assert if there is response
assert context.response, 'Response is null or empty
//change below map as needed
def expected = ['011': '4', '018': '0', '098': '2']
def xml = new XmlSlurper().parseText(context.response)
def actual = xml.'**'.findAll{it.name() == 'Details'}.collectEntries{[(it.Code.text()): it.Result.text()]}
assert expected == actual