【问题标题】:How to check xml response for dynamic array elements如何检查动态数组元素的 xml 响应
【发布时间】:2016-04-28 16:14:25
【问题描述】:

我是 groovy 和 soapui pro 的新手。我有以下示例响应,显示 2 个或更多带有动态数据的数组元素。我想知道如何编写脚本断言或 xpath 匹配来检查脚本是否通过,只要其中一个元素的值为 1。

<ns1:SampleTests>
    <ns1:SampleTest1>
        <ns1:Test>1</ns1:Test>
    </ns1:SampleTest1>   
    <ns1:SampleTest2>
        <ns1:Test>2</ns1:Test>
    </ns1:SampleTest2>  
</ns1:SampleTests>

我已经在脚本断言中写了这个,但它失败了。

【问题讨论】:

  • “我已经在脚本断言中写了这个,但它失败了。”
  • 必须有另一个唯一标识符才能提取所需的值,或者如果该值始终是固定的,您可以检查该值是否存在。例如:exists(//ns1:SampleTests/ns1:SampleTest1/ns1:Test[ . = '1']) 并期望它是 true

标签: xpath soap soapui assertions


【解决方案1】:

假设您的响应如下:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
   <Body>
      <ns1:SampleTests xmlns:ns1="hola">
            <ns1:SampleTest1>
            <ns1:Test>1</ns1:Test>
            </ns1:SampleTest1>   
            <ns1:SampleTest2>
                <ns1:Test>2</ns1:Test>
            </ns1:SampleTest2>  
    </ns1:SampleTests>
   </Body>
</Envelope>

您可以执行以下XPathexists(//*:Test[.=1]) 来检查是否存在至少一个以1 为值的&lt;ns1:Test&gt; 元素。

在一个 XPath 匹配中它看起来像:

如果您更喜欢使用 脚本断言,您可以使用 XmlSlurper 来解析您的 Xml,然后获取所有 &lt;ns1:Test&gt; 值的断言,该断言至少有一个具有 1 作为值.查看以下代码:

// get the response
def responseStr = messageExchange.getResponseContent()
// parse the response as slurper
def response = new XmlSlurper().parseText(responseStr)
// get all <ns1:Test> values
def results = response.'**'.findAll { it.name() == 'Test' }
// now in results list we've NodeChild class instances we will convert it to
// string in order to perform the assert
results = results.collect { it.toString() }
// check that at least one element has '1' value
assert results.contains('1'),'RESPONSE NOT CONTAINS ANY <ns1:Test>1</ns1:Test>'

【讨论】:

  • 谢谢,脚本断言和 xpath 都运行良好。
  • @user6221615 很高兴为您提供帮助,如果答案解决了您的问题,请考虑接受它:)
猜你喜欢
  • 1970-01-01
  • 2012-11-10
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 2019-12-18
相关资源
最近更新 更多