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 .


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 orderid=”889923″ xsi:noNamespaceSchemaLocation=”shiporder.xsd”>

<orderperson>John Smith</orderperson>


<name>Ola Nordmann</name>

<address>Langgt 23</address>

<city>4000 Stavanger</city>




<title>Empire Burlesque</title>

<note>Special Edition</note>





<title>Hide your heart</title>





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.


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


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


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


XML Schema assertions-Jmeter Tutorial

The XML Schema Assertion allows the user to validate a response against a XML Schema.

XML Schema is an XML-based alternative to DTDs. An XML Schema describes the structure of an XML document. The XML Schema language is also referred to as XML Schema Definition (XSD).Before going through this tutorial please have a brief idea on XML and XML schema.


1. We record the simple.xml file form the site (www.tomhanks.9ix.net/scripts) and validate it against simple.xsd.


<?xml version=”1.0″?>
alok ranjan


<?xml version=”1.0″ encoding=”UTF-8″ ?>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”><xs:element name=”name”>
<xs:complexType mixed=”true” />

<xs:element name=”person”>
<xs:element ref=”name” />


Copy the contents of simple.xsd to a notepad and save it as simple1.xsd.Place it under bin directory. You can record your own XML file and validate it against its schema. For generating the schema for a XML file we can use XSD generator tools .This can also be done online here:


2. Add schema assertions to the simple.xml sample. (Add->assertions->Xml Schema assertions)

3. Now provide the path and filename of schema in XML Schema Assertion element. Here we put simple1.xsd.


Now run the script and the view the result in View results tree . If assertion is passed the result is shown in green otherwise red.To view the results add an assertions results listener to the xml sample. (Add->listener->assertions Results)

By default all the failure details are shown in the assertions result tab in the view results tree.


we can tweak the schema to see what errors can be thrown by jmeter.

1. Remove the xmlns part from second line in the schema file sipmle1.xsd. The line is shortened to: <xs:schema>.Now run the script.An error comes like this:

Assertion failure message : fatal: line=2 col=12 The prefix “xs” for element “xs:schema” is not bound.

2.Lets remove the fifth line i.e </xs:element>.Running the script gives following error.

Assertion failure message : fatal: line=16 col=3 The element type “xs:element” must be terminated by the matching end-tag “</xs:element>

XML Assertions in Jmeter

Before going through this tutorial please have a brief idea about XML.

The XML Assertion tests the response data whether it consists of a formally correct XML document.So it validates the response to be a valid XML type.It does not validate the XML based on a DTD or schema.To validate the response against any schema you can use XML Schema Assertions.

To use XML Assertions we have to record the XML file from the server.In this example we will be recording these files:

1. Correct XML document
2. Invalid XML document

File1:Valid File4:Invalid
<?xml version=”1.0″?>
<?xml version=”1.0″?>
<body>Call me today.<body>


1. Record file1 (Valid XML) and file4 (invalid XML) from this link: http://www.alokranjan.uuuq.com.Or you can can record any file of your own choice.xmlassn1

Now add XML assertions to both the sample. (Add->assertions->XML assertions).also add a View Results tree Listener. Now run the script. The result in listener will look like this.The failure is shown in red.

The Assertion result is like this.

Error on line 7: The element type “body” must be terminated by the matching end-tag “</body>”.

To view the assertion results we can add an assertions results listener. (Add->Listener->assertions results)

If you try to assert the response of any normal html request with XML assertions it will though a mark up error,like this:

The markup in the document preceding the root element must be well formed.