ADS

Tuesday, April 2, 2013

Handling SOAP Request and Response from Groovy script

In this post, we will be learning how we can access SOAP Requests and Response using Groovy Scripts.

Let us take the following example
Request:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">
<soap:Header/>
<soap:Body>
<web:ConversionRate>
<web:FromCurrency>SEK</web:FromCurrency>
<web:ToCurrency>DKK</web:ToCurrency>
</web:ConversionRate>
</soap:Body>
</soap:Envelope>

Response:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ConversionRateResponse xmlns="http://www.webserviceX.NET/">
<ConversionRateResult>5.7187</ConversionRateResult>
</ConversionRateResponse>
</soap:Body>
</soap:Envelope>

Now we want to access the content of above SOAP Request and Response, then we can use the following code:
import com.eviware.soapui.support.XmlHolder

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def step = context.testCase.getTestStepAt(0)
if( step instanceof com.eviware.soapui.model.testsuite.SamplerTestStep && step.testRequest.response != null ) // this step indicate that retrieved TestStep is a SOAP request and response is present for it
{
def response = groovyUtils.getXmlHolder( step.name +"#Response")
def request = groovyUtils.getXmlHolder(step.name + "#Request")
response.namespaces["ns"] = "http://www.w3.org/2001/XMLSchema"
log.info (request.getNodeValue("//ConversionRateResponse/ConversionRateResult")) 
log.info response.isEmpty()
log.info response.getNamespaces()

For more functions which you can do with SOAP Request and Response can be found at http://www.soapui.org/apidocs/index.html and go to package com.eviware.soapui.support and XmlHolder class in it.

2 comments:

  1. log.info (request.getNodeValue("//ConversionRateResponse/ConversionRateResult"))
    log.info response.isEmpty()
    log.info response.getNamespaces()

    above three line are printing
    Sat May 04 13:28:18 IST 2013:INFO:null
    Sat May 04 13:28:18 IST 2013:INFO:false
    Sat May 04 13:28:18 IST 2013:INFO:{ns=http://www.w3.org/2001/XMLSchema}

    however there is response available.

    ReplyDelete
  2. Thanks for the correction. It has been rectified. :)

    ReplyDelete