2016-04-28 12 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