31 May 2011

SOAPUI and dynamic XML values

Bambitroll @ 17:44
I am currently using SOAPUI to load test an application via web services.
In order to do that, I need to be able to change the value of some XML elements on the fly.
Here is how you can do that:
- Create a Test Suite
- Create a Test Case (which will contains Test Steps)
- Your first test step will be a SOAP request called TestReq1
- In the Test Case Editor, you can create a Setup Script

Here is the Groovy script I use to set the value of one element dynamically (in this case the element attribute called Version):
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder("TestReq1#Request")
holder.setNodeValue("//@Version","7.2")
holder.updateProperty()

At the moment the script is not so useful since it dynamically sets the element to the same value. But there are several strategies to use different values each time, from reading from a file or a DB to writing a little generator in Groovy.


Another thing which is useful with SOAPUI is to be able to create a mock-up response which is dynamically created according to the incoming data. Once you have created your mock-up service, here is how to fetch data from the request and paste it in the response. First the incoming XML and then the Groovy script changing the response (in this example both the request and the response have a Version attribute on an element and we simply return the value we get in):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="http://my.project.com/abc">
   <soapenv:Header/>
   <soapenv:Body>
      <abc:customerApp Version="${version}" />
   </soapenv:Body>
</soapenv:Envelope>

def holder = new com.eviware.soapui.support.XmlHolder(mockRequest.requestContent)
context.version = holder["//@Version"]