How To… SoapUI: Parsing With Groovy

This guide is on how to solve a particular problem with SOAP UI with guest blogger Brendan Connolly. Here, we look at Parsing With Groovy.

 

Parsing with Groovy SOAP UI will be the focus of this post. Besides communicating with an endpoint a major aspect of API testing is getting data into requests and out of responses. Out of the box SoapUI supports XPath, XQuery and JsonPath for both property transfers and assertions. There is no doubt that those are some powerful tools, but but for complex objects or if namespaces are involved they can be challenging to create, troubleshoot, or maintain.

Groovy to the Rescue

Built in to Groovy are two classes XmlSlurper and JsonSlurper that make parsing xml and json for logging, transfers and assertions clean and easy. These classes will parse the response contents and allow you to easily access (using the dot or bracket syntax) even deeply nested properties.

//log the category name using bracket syntax
 log.info(parsedResponse["category"]["name"])

//log the category name using dot syntax
 log.info(parsedResponse.category.name)

If you want to play along we’ll be using the Swagger Petstore API

 

Property Transfer

The easiest way to do this is to use a Groovy Test Step in conjunction with a property to act as a placeholder for the dynamic value.

In this example I have added a property called petName to the testcase.

Then right click on the testcase and and add a Groovy Script step.

AddGroovyStep

In the test step window we’ll enter the following script:

import groovy.json.JsonSlurper

def slurper = new JsonSlurper()

//get the response data from a test step
 responseJson = testRunner.testCase.getTestStepByName("getPetByIdRequest").getPropertyValue("response")
 parsedResponse = slurper.parseText(responseJson) 
//log the value
 log.info(parsedResponse.name)

//set testcase property
 testRunner.testCase.setPropertyValue("petName",parsedResponse.name)

In case you aren’t familiar with Groovy, let’s unpack that script some. In the first two lines, the import statement lets our script know that we will be using the JsonSlurper and the second line creates and stores an instance of the slurper for us to use later in the script.

SoapUI injects some variables automatically for us to use inside our scripts. To get access to the response data we will use one of those variables testRunner to get access to the response data. We then pass the response data to the slurper to parse for us.

In the last two lines we use the parsed result to log (another variable injected by SoapUI) and set the value of the property we created on the testcase.

 

Assertions

For assertions we will use a Script Assertion

AddAssertion

Script Assertions are not invoked with the testRunner variable so we will use the messageExchange variable for this script. For this example you can see both Json and Xml scripts.

import groovy.json.XmlSlurper

def slurper = new XmlSlurper(false,false)

//SoapUI injects the messageExchange variable
 responseJson = messageExchange.responseContent
 parsedResponse = slurper.parseText(responseJson)

//log the name
 log.info(parsedResponse.name)

// assert name is doggie
 assert parsedResponse.name=="doggie"

ScriptAssertion

 

Working With Xml Namespaces

Depending on your needs XmlSluper can be aware of namespaces or ignore them. By default namespaces will be required.

//default namespace aware slurper
 def slurper = new XmlSlurper()

//to ignore namespaces
 def nameSpaceIgnoringSlurper = new XmlSlurper(false,false)

I hope that helps with some of the aspects of Parsing With Groovy. If you have any questions, comment below.

About the Author

Brendan

I am a Software Design Engineer in Test based out of Santa Barbara, California. Working in a variety of testing roles since 2009. I am responsible for creating and executing testing strategies and using his coding powers for developing tooling to help make testers lives easier. He writes tests at all levels from unit and integration tests to API and UI tests. I blog on testing and automation at Brendanconnolly.net or follow me on Twitter @theBConnolly
Find out more about @brendanconnolly