0
votes

I am new to groovy and soapui pro. I have below sample response that displays 2 or more array elements with dynamic data. I am wondering how to write a script assertion or xpath match to check if script passes as long as one of the elements has value 1.

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

I have written this in script assertion but its failing.

1
"I have written this in script assertion but its failing." <- We need to see what you did, and how is it failing! - SiKing
There must be another unique identifier to be able to extract the required value or you can check for the existence of the value if that is always fixed one. for ex: exists(//ns1:SampleTests/ns1:SampleTest1/ns1:Test[ . = '1']) and expect it to be true - Rao

1 Answers

1
votes

Supposing that you've a response like:

<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>

You can perform the follow XPath: exists(//*:Test[.=1]) to check that exists at least one <ns1:Test> element with 1 as value.

Inside an XPath Match it looks like:

enter image description here

If instead you prefer to use an Script assertion you can use the XmlSlurper to parse your Xml, then get all <ns1:Test> values an assert that at least one has 1 as value. Look into the follow code:

// 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