Using Script Assertions

User rating:
2.9 (30 ratings)

The Script-Assertion allows for arbitrary validation of a message, which included message content, HTTP headers, etc. Validation scripts are written in the project scripting language (Groovy or JavaScript) and are executed whenever the assertion-status of the containing sampler TestStep is updated. Add a Script Asssertion to your TestStep with the standard "Add Assertion" button;


which brings up the following configuration dialog


The top area is a standard soapUI script editor, with corresponding popup menu and actions. The Run button executes the assertion script against the last received response and displays the result in a popup window. The bottom shows the output of the log variable available in the script.

The messageExchange object available in the script exposes a bunch of properties related to the last request/response messages (check out the Javadoc), lets look at some simple examples:

1. Validate the content of an HTTP Header in the response

// check for the amazon id header
assert messageExchange.responseHeaders["x-amz-id-1"] != null

2. Validate a certain response time

// check that response time is less than 400 ms
assert messageExchange.timeTaken < 400

3. Validate the existence of an attachment in the response

// check that we received 2 attachments
assert messageExchange.responseAttachments.length == 2

4. Validate the existence of a specific XML element in the response

// check for RequestId element in response
def holder = new XmlHolder( messageExchange.responseContentAsXml )

assert holder["//ns1:RequestId"] != null

This last example is greatly simplified in soapUI Pro which adds a corresponding wizard to the Outline Editor;


For example selecting the sessionid node above will create the following assertion for you:


Pressing the Run button applies the assertion, which gives you


See soapUI Tips & Tricks to read how to get and set properties from a Script Assertion.