XPath Match Assertion

Applies to ReadyAPI 3.56, last modified on November 21, 2024

The XPath Match assertion allows you to use an XPath expression to select content from the target request or response node and compare it with the value you expect.

Important notes:

  • This assertion applies only to requests and responses with XML data in the body.

  • ReadyAPI cannot parse XML documents that contain the byte order mark (BOM) character.

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 body. See Working With Assertions in Functional Tests.
Security tests The response body. See Security Assertions.
Virtual services The request body. 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 XPath Match assertion or select it manually in the Property Content 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 XPath Match assertion or select it manually in the Property Content 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 XPath Match assertion or select it manually in the Property Content category.

Setting up properties

  1. In the XPath Expression edit box, specify the XPath expression you want to use for this search or use the toolbar controls.

    To use the toolbar controls, run the request you want to validate at least once.

    ReadyAPI: Configuring the XPath Match assertion

    Click the image to enlarge it.

    To create the expression automatically, click one of the following icons on the toolbar:

    Option Description
    Select node Call the Select XPath dialog. Use it to select a node from the last request or response and create an XPath expression for this node. See below.
    Namespace Get the last request or response namespace and declare it.
  2. In the Expected Result edit box, specify the value you are expecting to get. You can use the property expansion to point at a specific node.

    Use these commands to select the value from the existing request or response:

    Option Description
    Select Content Add a property expansion to the result.
    Select from Current Get a value from the last response you have received (or the last request the virtual service has received).
  3. If needed, use additional options to enhance the results you will get:

    Option Description
    Allow Wildcards Use an asterisk wildcard (*) for varying parts of the value.
    Note: If you do not select this option, the assertion will fail on dynamic values.
    Ignore Namespace Prefixes

    To avoid name conflicts, XML elements use prefixes. A prefix points to a namespace that is associated with an element. Typically, the namespaces are represented by URIs. When this option is disabled, the assertion compares both prefixes and corresponding URIs. If this option is enabled, the assertion compares actual URIs that are declared for each prefix, but ignores the prefix names.

    Example

    Ignore XML comments Skip XML comments during the comparison.
  4. To test your assertion on the last response you have received (or the last request the virtual service has received), click Test.

Tips

To avoid typing the desired XPath expression manually, click Select XPath and select the desired XML element or attribute in the subsequent dialog.

ReadyAPI: Selecting an element for XPath expression

ReadyAPI will form an XPath expression that points to the selected item, and will set the result of the last request or response as the expected result.

If a node you are selecting contains a single child, ReadyAPI will try to get its text instead of the object itself.

Example

  • The following expression gets the value of the type attribute of the password element in the login request:

    //sam:login[1]/password[1]/@type
  • The following expression gets the string containing the session ID from the loginResponse response. It uses the concat() XPath function:

    //sam:loginResponse[1]/concat('The user session ID is ',sessionid[1])
  • The following expression gets the value of the description subelement of the item element (with the id attribute equal to 1) in the Response response.

    //sam:Response[1]/item[@id="1"]/description[1]

Add more assertions that validate XML content:

See Also

Property Content Assertions
XQuery Match Assertion

Highlight search results