JMeter - XPath Assertion Usage With Example

XPath Assertion is useful to assert the page element's xpath. You can learn more about XPath and how to find it guide on THIS PAGE. Earlier we learnt different assertions of jmeter to use them in software load test plan so i think now you are well aware about assertion. Let's learn XPath assertion with example to know how to use it in your practical software load test plan using jmeter.

Add XPath Assertion In Software Load Test Plan
Mostly XPath assertion is being added under request. To add it under request,
  • Right click on request -> Add -> Select Assertions -> XPath Assertion. See bellow given image.

It will add XPath assertion under request.

Example : Assert XPath Of Element Using XPath Assertion
I have prepare very simple software load test plan to assert the xpath of http://jmeter.apache.org/ website's logo(See bellow given image) in jmeter load test plan. Function of XPath assertion is very simple. It will check if element with given xpath is present on page or not. Assertion will pass if given xpath is present on page. Else it will fail. Using xpath assertion you can confirm that targeted element is displayed on requested page.


I have added 2 request and xpath assertion under both requests in my jmeter software load test plan. 1st request's xpath assertion has valid xpath(//img[@alt='Logo ASF']) and 2nd request's xpath assertion has invalid xpath(//img[@alt='Logo ASF1']) as shown in bellow image. Actual XPath of logo is "//img[@alt='Logo ASF']".


Configuration of 1st request's XPath assertion is as bellow.

  • Apply to = Main sample only.
  • XML Parsing options : Selected options -> Use Tidy (tolerant parser), Quiet
  • XPath Assertion = //img[@alt='Logo ASF']

Configuration of 2nd request's XPath assertion is as bellow.
  • Apply to = Main sample only.
  • XML Parsing options : Selected options -> Use Tidy (tolerant parser), Quiet
  • XPath Assertion = //img[@alt='Logo ASF1']
Only difference in configuration of both xpath assertions is xpath syntax. Everything else is same.

HTTP Request configuration for both requests is same as bellow.


Thread group config is as bellow.


Now if you run above test plan, result will looks like bellow.


In result you can see that 1st request is pass. That means nodes with given xpath syntax is found on page. 2nd request's xpath assertion is failed and display Assertion failure message: "No Nodes Matched //img[@alt='Logo ASF1']".

This way you can use xpath assertion in your software load test plan to assert xpath of any element.

No comments:

Post a Comment