Using Script Assertions

This page contains information for SoapUI Pro that has been superseded by SoapUI NG Pro in the Ready! API platform.
To try the new functionality, feel free to download a Ready! API trial.

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;

Add script assertion dialog

which brings up the following configuration dialog

Empty script assertion configuration

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;

Add script assertion wizard

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

Generated script assertion

Pressing the Run button applies the assertion, which gives you

Passed script assertion

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