SoapUI – tips & tricks – part1

I’ve decided to group and share all the helper scripts/methods etc. that I’m using in SoapUI.
This time there are only 3 short hints but I hope they can be useful 🙂

Verify Content-Type of response message using script assertion:

String actual = new String(messageExchange.responseHeaders["Content-Type"].getAt(0).toString())
String expected = "application/json";
assert actual.equals( expected ), "Actual Content-Type: '" + actual + "' does not match expected: '" + expected + "'";

To access body of the response message as XML in script assertion (this method will also transform JSON into XML):

def response = messageExchange.getResponse().getContentAsXml();
def groovyUtils = new context );
def holder = groovyUtils.getXmlHolder( response );
holder.namespaces["ns"] = holder.getNodeValue("//namespace::*[2]"); // set the namespace
def valuesYouWantToGetUsingXPath = holder.getNodeValues( "//ns:list/ns:item/ns:description" ); // get all descriptions
def valueYouWantToGetUsingXPath = holder.getNodeValue( "//ns:list/ns:item[id=1]/ns:description" ); // get specific description

To quickly access the response from different test step as XML and using XPath:


2 thoughts on “SoapUI – tips & tricks – part1

  1. when i use these methods in my script assertion I get error “Cannot invoke method getContentAsXmla() on null object

    Cannot invoke method getAt() on null object

    What is missing?

    • Is the response you’re getting a valid xml or json?

      Ps. What system are you running and what version of SoapUI are you using?

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s