XPath Jmeter Example

In this article we will learn how to use XPath in JMeter. For this example we will use JMeter 3.2 and Java 8. The Apache JMeter™ application is open source software, a 100% pure Java application designed to load test functional behavior and measure performance. It was originally designed for testing Web Applications but has since expanded to other test functions.

1. Introduction

Apache JMeter may be used to test performance both on static and dynamic resources, Web dynamic applications. It can be used to simulate a heavy load on a server, group of servers, network or object to test its strength or to analyze overall performance under different load types. JMeter is not a browser, it works at protocol level. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages. Nor does it render the HTML pages as a browser does.

XPath is a W3C recommendation. XPath is a syntax for defining parts of an XML document and it uses path expressions to navigate in XML documents. It contains a library of standard functions and is a major element in XSLT and in XQuery.

2. Install JMeter

In this section we will learn how to install JMeter. You can install JMeter from Download JMeter. For this example I am using JMeter 3.2. You might need to choose a different version based of the Java version you have install. Click on the ‘apache-jmeter-3.2.zip’ file and the download will start. Unzip the file. To run JMeter, run the jmeter.bat (for Windows) or jmeter (for Unix) file. These files are found in the bin directory. After a short time, the JMeter GUI should appear. GUI mode should only be used for creating the test script, NON GUI mode must be used for load testing.

Figure 1. JMeter GUI

3. XPath Extractor

When you’re writing JMeter scripts, the XPath Extractor is proved 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 (e.g. <div>) with no attributes but different values. If you use the Regular Expression Extractor, your RegEx pattern will match all similar values, as there are no different attributes in the tag. To use the XPath Extractor add it as a child element to the HTTP Request sampler.

3.1 Add 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). Add the XPath Extractor as a child element of the HTTP Request. Select the HTTP Request on the left side and right click on it. Go to Add => Post Processor => XPath Extractor

Figure 2. XPath Extractor

Figure 3. 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.

Figure 4. XPath Extractor

Fields description:

  • Apply To: Is used with samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources, Mail Reader or samples generated by the Transaction Controller.
  • Main sample only: Only applies to the main sample.
  • Sub-samples only: Only applies to the sub-samples.
  • Main sample and sub-samples: Applies to both.
  • JMeter Variable: Assertion is to be applied to the contents of the named variable.
  • XPath matching: Is applied to all qualifying samples in turn, and all the matching results will be returned.
  • Use Tidy (tolerant parser): If checked, use Tidy to parse HTML response into XHTML. Use Tidy should be checked for HTML response. Such response is converted to valid XHTML (XML compatible HTML) using Tidy. Use Tidy should be unchecked for both XHTML or XML response (for example RSS)
  • Use Namespaces: If checked, then the XML parser will use namespace resolution.
  • Validate XML: Checks the document against the schema.

4. Conclusion

In this article we learned how to install JMeter and how to use it to perform XPath extraction. JMeter is a very popular tool which can be used for various purpose. We can also use JMeter for extracting information from the response using the XPath query.

Mohammad Meraj Zia

Senior Java Developer
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
5 years ago

How to add Xpath Extractor and JSR223 PostProcessor with Test Plan Using Java Code.

Back to top button