Script Assertion

Applies to ReadyAPI 3.55, last modified on October 09, 2024

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

Follow these steps:

Functional test: 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:

Security tests: The Assertions panel

Click the image to enlarge it.

  1. Open a security test.

  2. Click Response Assertion next to any scan.

  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:

Virtual service: 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:

    // 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"] != []

See Also

Validate Response Against XSD Schema

Highlight search results