Run massively scalable performance tests on web, mobile, and APIs

Request a Demo
Sep. 23rd, 2014

Using the XPath Extractor in JMeter

When you’re writing JMeter scripts, the XPath Extractor prove to be very useful in cases where data from the response body is needed and when it becomes overly difficult to extract information using the Regular Expression Extractor. (For example, you may have encountered scenarios where there are some similar tags with no attributes – but different values.)

 

Xpath Extractor in JMeter

 

If you use the Regular Expression Extractor, your RegEx pattern will match all three values, as there are no attributes in tag div. But with the XPath Extractor, it’s easy.

 

ADDING THE XPATH EXTRACTOR

 

To use the XPath Extractor add it as a child element to the HTTP Request sampler (or any other sampler, for that matter). (As the target server we'll be using our favorite site, blazemeter.com.)

 

Xpath Extractor as a child element to HTTP Request sample

 

Add the XPath Extractor as child element of the HTTP Request:

 

Add XPath Extractor as child element of HTTP Request:

 

After running the test, the response body of the parent element will be sent to the XPath Extractor to parse. Now we can extract value(s) from the structured response - XML or (X)HTML, using XPath query language. Let’s create a simple XPath query that extracts the title of the page.

 

Xpath query which will extract title of the page

 

Run your script as though there are no errors in the log. This means you did everything correctly. Note: You won’t see any parsed results, which is fine. We'll come across them in the debugging block.

 

main elements which where used to parse Xpath Extractor

Definitions of 'elementus majorus'
 

Click here to get more detailed info on the other XPath Extractor elements.

 

DEBUGGING

 

To debug our XPath query and see the results, let’s add two samplers:

 

Add -> Sampler -> Debug Sampler

Add -> Listener -> View Results Tree

 

Now that we see the following results, we are ready to debug the XPath query.

 

 View Results Tree is ready for us to debug our Xpath query.

 

Let’s re-run our test script and open the “View Results Tree.” In the “Response data” tab, you can see results of query. 

 

Open “View Results Tree” to see the results.

 

Parsed information can be used as a variable in the next samplers as ${Title} or ${Title_1}. It doesn’t matter, since the query matched only one value. So what if more values are matched?

 

Let’s change the XPath query to /html/head/meta/@name and re-run the test.

 

Here, the debug info will be little bit different:

 

  • Title=generator
  • Title_1=generator
  • Title_2=generator
  • Title_3=description
  • Title_4=OBKey
  • Title_5=keywords
  • Title_6=MobileOptimized
  • Title_7=HandheldFriendly
  • Title_8=viewport
  • Title_matchNr=8

 

Title_matchNr=8 means that we have eight matches and can use any of them as ${Title_numberFrom1To8}.

 

Recall that, in our first example, we couldn’t parse with the RegEx extractor. In XPath, we can match any value of a tag by specifying the number of divs. Here, the query: .//*[@id='numbers']/div[2] - matches the second div and .//*

[@id='numbers']/div[3] matches the third one. IMPORTANT: When you copy and paste XPath queries, be sure that no curly apostrophes are copied.

 

PARSING JSON

Use the JMeter Plugins Manager and add JSON plugins.

To ensure that everything is installed correctly, add the OAuth Sampler in a JMeter test plan (JMeter menu -> Edit -> Add -> Sampler -> OAuth Sampler). Now try to parse some JSON data. To do this, we don’t need to search for any public JSON API. We can just use BlazeMeter's API.

 

  1. Add the HTTP Request sampler.
  2. Insert this URL-> https://a.blazemeter.com/api/rest/blazemeter/testGetStatus/
  3. Add two JSON Path Extractors as the child/ren.
  4. Insert any name in Name field and response_code in JSON path field.
  5. Insert any name in Name field and error in JSON path field.
  6. Add Debug sampler and View Results Tree listener.
  7. Run it.

 

Insert any name in “Name” field and response_code in “JSON path” field

 

In the “View Results Tree” listener, you will see that the JSON data was parsed successfully.

 

In “View Results Tree” listener you will see that JSON data was parsed successfully.

 

THE DIFFERENCES BETWEEN XPATH AND JSONPATH

 

JSON Path uses queries that are similar to XPath to match values. However, the syntax is a little bit different.

 

The differences between XPath and JSONPath.

 

Click here to get more detailed information about JSONPath syntax.

 

Click here to learn more about easily debugging your JSONPath queries.

 

XPATH EXTRACTOR VS. REGEX EXTRACTOR

 

The following compares Regexp Extractor and XPath Extractor.

 

The Pros of XPath Extractor

 

  • Easy to parse HTML tags without attributes.
  • Easy to parse RSS feeds.
  • The ability to use Tidy (tolerant parser).
  • You can parse JSON with same attribute names.

 

The Cons of XPath Extractor

 

  • Builds DOM tree to parse HTML code, it consumes CPU and memory

 

When you have a choice of XPath Extractor or Regexp Extractor, we recommend the latter, which works faster than XPath Extractor. If possible, rewrite your XPath queries to Regexp queries when you want to tune your script and the JMeter server’s capabilities are very limited. But that doesn’t mean that you shouldn’t use XPath Extractor. If you have some XPath Extractor samplers in the test tree, it’s fine.

 

HOW TO WRITE XPATH QUERIES

 

Check out some additional XPath documentation to master some of the basics. But you needn’t remember everything. This blog post can be a reference and you can always ease your work with some tools such as Firebug and FirePath add-ons.

 

After opening the page from which you want to extract the XPath, and then launch Firebug. Select FirePath and click the Analyze button. Move the mouse to any element of the page and you will see XPath queries in the FirePath field.

 

How to write XPath queries

 

We got the query .//*[@id='edit-title'] , which will match the selected elements. Using this add-on, you can analyze any element of the page without needing to check the XPath reference each time to remember syntax.

 

 

More exaqmples-HTML Code

 

HTML Code

 

XPath Query
 

If you are new to JMeter, and you’d like to learn more, please sign up for our free online JMeter training course.


For more experienced JMeter users, you'll want to view the on-demand webcast, How to Create Advanced Load Testing Scenarios with JMeter.

 

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