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.

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.

Steps:

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

simple.xml

<?xml version=”1.0″?>
<person>
<name>
alok ranjan
</name>
</person>

simple.xsd

<?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>

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

</xs:schema>

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:

xml_schema1

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.

xml_schema2

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.

experimentation:

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″?>
<person>
<name>
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>
<job>Singer</job>
<gender>Male</gender>
</person>
<?xml version=”1.0″?>
<note>
<to>Alok</to>
<from>Ranjan
<heading>Reminder</heading>
<body>Call me today.<body>
</note>

Steps:

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.