假设你已经像一个响应:
<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>
可以执行遵循的XPath:exists(//*:Test[.=1])
检查所存在的与1
作为值的至少一个<ns1:Test>
元素。
内的XPath的比赛它看起来像:
相反,如果你更喜欢使用脚本断言可以使用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>'
“我在剧本的断言,但其失败写了这个。” < - 我们需要看看你做了什么,以及它如何失败! – SiKing
必须有另一个唯一标识符才能提取所需的值,或者如果该值始终是固定的,您可以检查该值的存在。如果存在(// ns1:SampleTests/ns1:SampleTest1/ns1:Test [。='1'])'并且期望它是'true' – Rao