XPath Match Assertion

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

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

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.

    Important

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

    ReadyAPI: Configuring the XPath Match assertion

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

    Option

    Description

    Declare button

    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.

    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.png 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.

Important

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

Examples

  • 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]

Other assertions for XML data

Add more assertions that validate XML content:

See Also

Publication date: