2016-04-28 136 views
0

我是groovy和soapui pro的新手。我有下面的示例响应,显示2个或更多数组元素与动态数据。我想知道如何编写一个脚本断言或XPath匹配检查,如果脚本会将只要要素之一有值为1如何检查动态数组元素的xml响应

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

我在剧本的断言,但其失败写这个。

+0

“我在剧本的断言,但其失败写了这个。” < - 我们需要看看你做了什么,以及它如何失败! – SiKing

+0

必须有另一个唯一标识符才能提取所需的值,或者如果该值始终是固定的,您可以检查该值的存在。如果存在(// ns1:SampleTests/ns1:SampleTest1/ns1:Test [。='1'])'并且期望它是'true' – Rao

回答

0

假设你已经像一个响应:

<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作为值的至少一个<ns1:Test>元素。

内的XPath的比赛它看起来像:

enter image description here

相反,如果你更喜欢使用脚本断言可以使用XmlSlurper来分析XML,然后让所有<ns1:Test>值的断言至少有一个值为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>' 

enter image description here

+0

谢谢,脚本断言和xpath都正常工作。 – user6221615

+0

@ user6221615很高兴帮助你,如果回答解决你的问题想想接受吧':)' – albciff