Using Groovy scripts to select Dispatch actions in soapUI

You can use the power of Groovy for selecting MockResponses in soapUI
If your request is of the format :-

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

You can use the following script :-

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def req = new XmlSlurper().parseText(mockRequest.requestContent)
log.info "Customer #${req.foo.data.CustomerNumber}"
context.result = req.foo.data.CustomerNumber
 

Here we are using mockRequest.requestContent to get the request soap envelope. We then use the XmlSlurper to parse this. XmlSlurper does not care about namespaces and it can parse XML into a document tree that may be traversed similar to XPath expressions. Doc : http://groovy.codehaus.org/api/groovy/util/XmlSlurper.html

context.result ensures that the MockResponse whose name =CustomerNumber (in this case 1234) is selected as a response.

log.info is for logging and can be tested in the “script log” tab below when you send a request

Other variables that can be used are : mockOperation to select responses based on operation, requestContext and context.

You can have fancy scripts which contain if statements, connect to database, files, inlist etc whatever Groovy supports!

You may also pass back calculations by parsing parameters from request and converting them:-

Sample Response

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

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Script

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

You can use XmlHolder as well if you care for namespaces:-

Request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
   <soapenv:Body>
      <tem:RunScript>
         
         <tem:optionObject>
            <tem:EntityID>5</tem:EntityID>
            <tem:Facility>98</tem:Facility>
              <Forms>
               <FormObject....

Script

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( mockRequest.requestContent ) 
holder.declareNamespace( 'tem', 'http://tempuri.org/')

context.Facility = holder.getNodeValue( "//tem:Facility")
context.EntityID = holder.getNodeValue( "//tem:EntityID")

But in this case you will have to explicitly declare namespaces.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s