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