The Script assertion runs a groovy script to perform custom checks on the message. Use it to verify the message content, headers, properties and other components.
Availability
This assertion is available in multiple ReadyAPI applications. Depending on the application, it validates the following data:
In... | Checks... | To learn more... |
---|---|---|
Functional tests | The request or response. | See Working With Assertions in Functional Tests. |
Security tests | The response. | See Security Assertions. |
Virtual services | The request. | See Assertions in Virtual Services. |
Create an assertion
Setting up properties
-
Write a script verifying the value you need. Some script examples are available below.
Tip: If the font of the editor is not comfortable for you, change it by using Ctrl + mouse wheel. Assertion scripts have access to the following objects:
Object Description log
An Apache Logger
object that you can use to post messages to the test log.
For information on methods, see the Apachelog
object documentation.context
A TestCaseRunContext
object that provides scripting interface to test steps and the test runner.
For information on methods, see the Context object documentation.messageExchange
A MessageExchange
object that provides scripting interface for request and response data, and for information on request execution.
For information on object methods, see the MessageExchange object documentation. -
Click to test your script.
Make sure you have the value to assert before testing the script.
Examples
-
The following script validates the response’s HTTP header:
// Check for the Amazon ID header
assert messageExchange.responseHeaders["x-amz-id-1"] != null -
The following script verifies that the response was received within the specified time limit:
// Check whether the response time is less than 400 ms
assert messageExchange.timeTaken < 400 -
The following script verifies that the attachment exists:
// Check whether there are 2 attachments available
assert messageExchange.responseAttachments.length == 2 -
The following script verifies that the
RequestId
XML element exists:// Import the XmlHolder namespace
import com.eviware.soapui.support.XmlHolder
// Check for the RequestId element in the response
def holder = new XmlHolder( messageExchange.responseContentAsXml )
assert holder["//ns1:RequestId"] != []