Applies to ReadyAPI 2.8, last modified on August 16, 2019

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...
SoapUI functional tests The request or response. See Working With Assertions in SoapUI.
SoapUI security tests The response. See Security Assertions.
ServiceV The request. See Assertions in ServiceV.

Create an assertion

Follow these steps:

SoapUI: The Assertions panel

Click the image to enlarge it.

  1. Open a test step.

  2. Click Add assertion.

In the New Assertions dialog, search for the Script Assertion assertion or select it manually in the Script category.

Follow these steps:

Secure: The Assertions panel

Click the image to enlarge it.

  1. Open a security test.

  2. Select Response Assertion.

  3. Click in the Assertions panel

In the New Assertions dialog, search for the Script Assertion assertion or select it manually in the Script category.

Follow these steps:

ServiceV: The Assertions panel

Click the image to enlarge it.

  1. Open a virtual service.

  2. Open the Assertions drop-down panel.

  3. Click in the Assertions panel

In the New Assertions dialog, search for the Script Assertion assertion or select it manually in the Script category.

Setting up properties

  1. Write a script verifying the value you need. Some script examples are available below.

    ReadyAPI: Configuring the Script assertion

    Click the image to enlarge it.

    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 Apache log 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.
  2. Click Run to test your script.

    Make sure you have the value to assert before testing the script.
  • 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:

    // Check for the RequestId element in the response
    def holder = new XmlHolder( messageExchange.responseContentAsXml )
    assert holder["//ns1:RequestId"] != null

See Also

Validating Response Against XSD Schema

Highlight search results