views:

4251

answers:

2

This is probably a very easy one for all SoapUI regulars.

In a SoapUI mock service response script, how do I extract the value inside the request I'm replying to?

Let's say the incoming request has

<ns1:foo>
  <ns3:data>
    <ns3:CustomerNumber>1234</ns3:CustomerNumber>
  </ns3:data>
</ns1:foo>

How do I get the "1234" into a Groovy variable? I tried with an xmlHolder but I seem to have the wrong XPath.

(I know how to set a property and integrate its value into the response already.)

+5  A: 

Hi Thorsten,

If you want to access SOAP request and do some XPath processing, there's an easier way to do it in soapUI thanks to the power of GPath and XmlSlurper.

Here's how you would access the customer number:

def req = new XmlSlurper().parseText(mockRequest.requestContent)
log.info "Customer #${req.foo.data.CustomerNumber}"

As of Groovy 1.6.3 (which is used in soapUI 2.5 and beyond), XmlSlurper runs in namespace-aware and non-validating mode by default so there's nothing else you need to do.

Cheers!
Shonzilla

Shonzilla
+5  A: 

One more example:

def request = new XmlSlurper().parseText(mockRequest.requestContent)
def a = request.Body.Add.x.toDouble()
def b = request.Body.Add.y.toDouble()
context.result = a + b

In this example we get two parameters from the request and convert them to doubles. This way we can perform calculations on the parameters. The sample SoapUI response for this example is:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://example.org/math/types/"&gt;
   <soapenv:Header/>
   <soapenv:Body>
      <typ:AddResponse>
         <result>${result}</result>
      </typ:AddResponse>
   </soapenv:Body>
</soapenv:Envelope>

You can see how the calculations result is passed back to the response.

Mateusz Mrozewski
Thanks for your example! I like both the replies from Shonzilla and you!
Thorsten79