Showing posts with label Assertions in Jmeter. Show all posts
Showing posts with label Assertions in Jmeter. Show all posts

xpath assertion in jmeter example

Apahe JMeter assertions is very useful and amazing components in JMeter to validate whether request is pass or fail ,in my previous post you learned about Transaction Controller in JMeter to create nested HTTP Samples.In this post you will learn how to use XPath assertion in JMeter with real example to verify the response of HTTP request as per designed Test plans.Please visit w3c tutorials for XPath tutorials.

What is XPath Assertion?

Xpath Assertion in Jmeter tests a response of particular request which will validate the xpath as defines,in case xpath exists in response then that Asseertion is true,if that xpath does not exist's then Assertion is false means fails.

xpath assertion in jmeter example

XPath Assertion examples:

//title[text()='Selenium']- Which will matches the Selenium text anywhere in the response for that request.
/return/message='Success' - Which will matches the Success message in a root level response.

XPath Assertion in Jmeter example

XXPathassertion is useful in validating the response in case unable to find the root element directly in the response or document.Please follow below steps to create Xpath Assertions in your test plan for requests.
1.Open Jmeter from bin folder.
2.Add ThreadGroup.
3.Add Samples may be it could be HTTP Samples/SOAP Request Samples.

xpath assertion in jmeter example

4.Now add Xpath Assertion from Assertions menu to validate the response.
For SOAP response you can write xpath assertion as below to validate particular tag text as <message>Success</message>


Response is


<soap Envelope xmlns:soap="http://schemes:xmlsoap.org/soap/etc"/>
<soap:body>
<ns2:customerdetails xmlns:ns2="http://xmlns:...">
<return>
<code>1</code>
<message>Success</message>
<customerdetails>
<custName>Rajesh kuchana</custName>
<dob>10-01-1986</dob>
<title>Mr</title>
</customerdetails>
</return>
</ns2:customerdetails>
</soap:Body>
</soap:Envelope>

You can write Xpath Assertion to validate the response of request is as below.

First Example:

//retunr/message='Success'

Second Example:

//return/code='1'
/return/customerdetails/title='Mr'

Third Example:

//return[text()='Success']
//return/customerdetails/custName[text()='Rajesh Kuchana']

For Web Applications:

You can prepare Xpath Assertions to validate the web applications also ,please check below examples to clear understansing.

<html>
<head>
<title>Learn Jmeter Tutorials</title>
</head>
<body></body>
</html>

You can write xpath assertion as below to verify title text.
//head/title/text()='Selenium Tutorials'


xpath assertion in jmeter example


Please check Use Tidy(tolerant parser) check box on,it will automatically hide the error while running your script to identify the response and validate it with xpath assertion as below.


xpath assertion in jmeter example



Now check your Summary Report and View Results Tree.

View Result Tree

xpath assertion in jmeter example

Summary Report


What have you learned?

1.XPath Assertion in Jmeter
2.How to write XPath Assertion for Soap Response and Web applications.
3.How to check results using Assertion Results in Listeners.
4.How to prepare XPath assertion to Particular Response.