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>

Advertisements

One Response

  1. Simple and straight forward example.
    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: