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")

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">
      <abc:customerApp Version="${version}" />

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

5 Response to "SOAPUI and dynamic XML values"

  1. Anonymous said...

    I have a question, perhaps you can help me. I created a mock-up response like yours and set also some attributes.
    But after this, I want to write my response in a file, but i always get the response one before my current response. I tried it with this:
    responsef.write(mockResponse.getMockResult().getResponseContent(), "UTF-8")
    But do you know, how I can write the current response?

  2. Anonymous said...

    How about mockResponse.getResponseContent() instead?

  3. Anonymous said...

    In this step, def holder = groovyUtils.getXmlHolder("TestReq1#Request")
    you have explained how to get response only when it is from the same test case. But is it possible to get the request or response if it belongs to a different testcase.

  4. pps said...

    Hi, I am new to SOAP UI dynamic testing.Can we invoke dynamic response from code without JUnit kind of test cases ?
    If yes , can you please explain it how to set it up ?

  5. Whizdom Trainings said...

    hi,we provide online training & video tutorial for soapui
    for free videos refer

Post a Comment