XPath Assertion in Jmeter

The XPath Assertion tests a document for well formedness, has the option of validating against a DTD, or putting the document through JTidy and testing for an XPath. If that XPath exists, the Assertion is true. Using “/” will match any well-formed document, and is the default XPath Expression. The assertion also supports Boolean expressions, such as “count(//*error)=2”.

You can also go through the previous tutorial: xml assertions .

Steps:

1. We record a file shiporder.xml (http://tomhanks.9ix.net/xml/shiporder.xml) and will assert if the elements and attributes are correct. The shiporder.xml file is like this.

Shiporder.xml

<shiporder orderid=”889923″ xsi:noNamespaceSchemaLocation=”shiporder.xsd”>

<orderperson>John Smith</orderperson>

<shipto>

<name>Ola Nordmann</name>

<address>Langgt 23</address>

<city>4000 Stavanger</city>

<country>Norway</country>

</shipto>

<item>

<title>Empire Burlesque</title>

<note>Special Edition</note>

<quantity>1</quantity>

<price>10.90</price>

</item>

<item>

<title>Hide your heart</title>

<quantity>1</quantity>

<price>9.90</price>

</item>

</shiporder>

2. Add an XPath Assertions Element to the sample. (Add->Assertions->XPath assertions). Add Assertion results to the sample. (Add->Listener->Assertion Results).Also add a view results tree.

xpath1

Now to assert an XPath we need to provide path expressions in the XPath Assertion field. Here we put

/shiporder/item/note

Run the script. The result will be shown in green in the View Listener tree if the assertion is true.xpath2

Now put the XPath as: /shiporder/item[2]/note

Run the script and the assertion will fail since there is no note in second item. The following message will come.

Assertion failure message: No Nodes Matched /shiporder/item[2]/note

xpath3

In Location Path Expression you can also use XPath Axes, functions and operators.

Advertisements

One Response

  1. Hi,

    I’m using JMeter and have the following XML which I need to extract the Id from.

    ….

    I want to extract 001 from the Id.

    I’m using JMeter XPath Extractor and so far I have tried…

    /Customer@Id and /Customer/@Id

    What am I missing??

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: