Dmitri Tikhanski is a Contributing Writer to the BlazeMeter blog.

Become a JMeter and Continuous Testing Pro

Start Learning
Slack

Test Your Website Performance NOW! |

arrowPlease enter a URL with http(s)
Nov 03 2020

The JMeter XPath2 Extractor: How to Achieve Better Correlations

 

When it comes to modern web applications and API testing, in the majority of cases you cannot replay a recorded test scenario without performing correlation. Correlation is the process of detecting dynamic parameters, extracting them using a suitable JMeter Post-Processor and storing them into a JMeter Variable. This blog post will explain how you can use the XPath2 Extractor from JMeter 5.0 for correlations. I will present some use cases that demonstrate the extractor’s abilities and show why it’s more powerful than the XPath Extractor.

Which Post-Processor to Choose

To identify the appropriate post-processor for your response type, use the following table:

 

Content Type

JMeter Post-Processor

HTML

CSS Selector Extractor

JSON

JSON Extractor/JMESPath Extractor

XML

XPath Extractor / XPath2 Extractor

YAML 

YAML Path Extractor

Everything else

Regular Expression Extractor or Boundary Extractor

 

Introducing the XPath2 Extractor

Since JMeter 5.0 a new Post-Processor for working with XML/XHTML data is available: the XPath2 Extractor.

XPath2 Extractor Advantages

 

  • More powerful - the number of potentially useful functions in XPath 2 is much higher comparing to XPath 1.0
  • Better performing compared to the “normal” XPath Extractor
  • More flexible in terms of data extraction criteria. It can cover more use cases which are not possible with the XPath Extractor

 

For illustration purposes I’ll be using a books.xml file, just in case here are its contents:

 

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>



<book category="cooking">

  <title lang="en">Everyday Italian</title>

  <author>Giada De Laurentiis</author>

  <year>2005</year>

  <price>30.00</price>

</book>



<book category="children">

  <title lang="en">Harry Potter</title>

  <author>J K. Rowling</author>

  <year>2005</year>

  <price>29.99</price>

</book>



<book category="web">

  <title lang="en">XQuery Kick Start</title>

  <author>James McGovern</author>

  <author>Per Bothner</author>

  <author>Kurt Cagle</author>

  <author>James Linn</author>

  <author>Vaidyanathan Nagarajan</author>

  <year>2003</year>

  <price>49.99</price>

</book>



<book category="web" cover="paperback">

  <title lang="en">Learning XML</title>

  <author>Erik T. Ray</author>

  <year>2003</year>

  <price>39.95</price>

</book>



</bookstore>

 

If you want to replicate the scenarios I will present, the easiest way is by using the Dummy Sampler. You can use it to set arbitrary request/response data, elapsed time, response code, etc. It’s extremely useful for tests debugging as you don’t have to re-run your script to test a post-processor. 
 

 
 

XPath2 Extractor Use Cases

A couple of examples demonstrating the reasons for switching to the XPath2 Extractor:

Use Case 1: The ends-with() function

 

Use XPath 2 when you need to match a string that ends with a character or a characters sequence. For example, to find all the prices that end with 99, the XPath2 query would be something like: 

//book[ends-with(price, '99')]/price

 

 

You can achieve the same with the XPath Extractor using XPath 1.0 syntax. However, XPath doesn’t provide the ends-with() function. So your query will be:

//book[substring(price, string-length(price) - string-length('.99')+ 1, string-length(price))= '.99']/price

 

 

As you can see, it is not that readable and it takes much more effort to implement and maintain. 

 

Use Case 2: Providing Default Value When There is No Match


 

In the above XML example, only the last book on the list- “Learning XML” - has a “cover” tag:

 

book category="web" cover="paperback">

  <title lang="en">Learning XML</title>

 

Others don’t:

 

<book category="cooking">

  <title lang="en">Everyday Italian</title>

  

<book category="children">

  <title lang="en">Harry Potter</title>

  

<book category="web">

  <title lang="en">XQuery Kick Start</title>

 

You might want to set some default value when the tag is missing. With the XPath2 Extractor it can be done as simple as:

 

/*/*/(string(@cover)[.], string('foo'))[1]
 

 

Now you can use the ForEach Controller or any other looping solution easily.

Use Case 3: Higher Loads

A Performance Comparison Between the XPath Extractor and the XPath2 Extractor

 

Let’s use a single simple XPath query returning the authors of all “children” books. It can be:

 

//book[@category='children']/author

 

 

In the Dummy Sampler, reduce connect time, latency and response time to 0.

 

 

Since JMeter will be executing the Dummy Sampler as fast as it can, the only difference in test results will be the time for the XPath Extractor to execute.

 

For this demo I ran the test with 100 threads (virtual users) and 100 iterations. 

 

The second test plan is exactly the same except for using the XPath2 Extractor used instead of the XPath 1.

 

I ran these 2 test plans in command line non-GUI mode and compared the throughput for both executions:

 

 

As you can see, for the test plan with the XPath2 Extractor, there were 9469 requests per second and for the XPath Extractor there were only 4014 - 2x times more. This means that you will be able to run more of a load using the XPath2 Extractor ( given the same hardware).

 

That’s it! As you can see the XPath2 Extractor provides more features and flexibility compared to the XPath Extractor, In addition, it’s more efficient and less resources consuming. Once you’re done creating your JMeter script, you can run it in BlazeMeter for free and achieve scale, continuous integration agility, and advanced reporting.

 

   
arrowPlease enter a URL with http(s)

Interested in writing for our Blog?Send us a pitch!