Search code examples
groovysoapuiassertions

How to add XPath to XmlSlurper


I am trying to create a groovy script assertion in SoapUI. In the response, I and trying to pull a field called written. however there are +15 of these fields.

I is possible to add the XPath to the XmlSlurper to find the exact written fields I want to assert against.

Looking at the XML response below, I would like to assert the value in b:premium\written. not the one from the b:other. Given there are 15+ b:written fields i would like to assert the value using the xpath.

XML Response:

<s:Body>
    <NewRateResponse>
        <NewRateResult>
            <b:policies>
                 <b:other>
                     <b:written>00.00</b:written>
                 </b:other>
                 <b:premium>
                     <b:written>31.21</b:written>
                 </b:premium>
            </b:policies>
        </NewRateResult>
    </NewRateResponse>
</s:Body>

Assertion Code:

import org.junit.Assert
def response = messageExchange.getResponseContent()
def xml = new XmlSlurper().parseText(response)
def nodePremium = xml.'**'.find { it.name() == 'written'}
Assert.assertEquals(00.01, nodePremium.toDouble(),0)

I believe the area we need to change is def nodePremium = xml.'**'.find { it.name() == 'written'}. to be something like def nodePremium = xml.'**'.find { it.name() == 'premium\written'} but that does not work for me.


Solution

  • assert xml.'**'.find { it.name() == 'premium'}.written.text() as Double == 31.20