This is our most popular blog post and we’ve added more extractors. This post explains how to use the regular expression extractor to extract the key from the response of your first request and use the extracted key for subsequent requests. We call this JMeter Extract and re-use.
Now you can use additional extractors, the JSON extractor and Xpath extractor. We’ve added that to the end of this blog post.
You can use the regular expression extractor to extract the key from the response of your first request and use the extracted key for subsequent requests. We call this JMeter Extract and re-use. Here are the steps:
- Right click on the first request and add post processor: Regular Expression Extractor.
- Create your regular expression and provide values in other required fields. Please refer to JMeter component reference for more details.
- The extracted value will be saved in the variable given as reference name.
- You can use this variable in subsequent requests.
Example Test Plan – JMeter Extract and Re-use
We are using OpenWeather API for example and will extract the cityid
- Let’s suppose “Current weather by city” is your first request. In response of this request, it returns the city id. I have added a regular expression extractor which will extract the city id and extracted value will save in “cityid” variable name.
- “cityid” variable is sent in the subsequent requests. For example, it is used in “Current weather by city ID” request.
We’ve now done JMeter extract and reuse.
You can run a JMeter Load Test with your JMX script of any mobile application, web application, or API on RedLine13. Here are results to check out.
Or go and try your own load test.
More Extractors – JMeter Extract and Re-use
This new section will explain how you can also use a JSON extractor or Xpath extractor to extract the key from the response of your first request and use the extracted key for subsequent requests. This is also part of JMeter Extract and re-use.
Extract using JSON Extractor
JSON is an extremely simple data format, which has overtaken XML in popularity. An increasing number of REST APIs and servers are using JSON as their primary data exchange format. Since JMeter 3.0, it is far easier to extract data from JSON responses using the JSON post processor extractor. The JSON post processor enables you extract data from responses using JSON-PATH syntax. This post processor is very similar to Regular expression extractor. It must be placed as a child of HTTP Sampler or any other sampler that has responses. It will allow you to extract text content in a very easy way. See JSONPath syntax for more details.
To demonstrate the use of the JSON post processor, let’s use the https://jsonplaceholder.typicode.com/todos/1 rest service, which allows you to demo sending requests and getting responses.
If you click on this link or copy paste into a browser, you will see the response. The response is in JSON format:
{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }
From the above response, you can extract userId, id and title, completely using the JSON path and JSON post processor.
Follow the steps below to extract the values.
Add a HTTP Request sampler with the following parameters:
Protocol: https
Server Name or IP: jsonplaceholder.typicode.com
Method: GET
Path: /todos/1
Add a JSON post processor as a child of HTTP Request sampler:
Names of created variables: userID
JSON Path Expression: $.userId
Match no: 1
The above JSON extractor extracts userId and stores in userID. You can use ${userID} to reuse this variable.
To know more about writing JSON path expressions please refer to http://goessner.net/articles/JSONPath/index.html#e2
Extract using XPath Extractor
XPath is defined as XML path. It is a syntax or language for finding any element on the web page using an XML path expression. XPath is used to find the location of any element on a webpage using the HTML DOM structure. To demonstrate the use of the XPath extractor, let us assume the following response for one of my HTTP requests in JMeter:
<div class="container"> <h2>Choose your departure city:</h2> <form action="reserve.php" method="post"> <select name="fromPort" class="form-inline"> <option value="Paris">Paris</option> <option value="Philadelphia">Philadelphia</option> <option value="Boston">Boston</option> <option value="Portland">Portland</option> <option value="San Diego">San Diego</option> <option value="Mexico City">Mexico City</option> <option value="São Paolo">São Paolo</option> </select> <p> <h2>Choose your destination city:</h2> <select name="toPort" class="form-inline"> <option value="Buenos Aires">Buenos Aires</option> <option value="Rome">Rome</option> <option value="London">London</option> <option value="Berlin">Berlin</option> <option value="New York">New York</option> <option value="Dublin">Dublin</option> <option value="Cairo">Cairo</option> </select>
We can extract fromPort values and toPort values into two different variables and then pass it on to the next request. If you build a regex expression (value=”(.+?)”>), it captures all the values of fromPort and toPort in a single array. It is very difficult to extract such values using Regular expressions. You can use XPATH extractor to extract them instead.
To extract a random option value for fromPort, you can use //select[@name=’fromPort’]/* in the Xpath field and set Match No. to 0.
To extract toPort, you can use //select[@name=’fromPort’]/* in the Xpath field and set Match No. to 0.
You can run a JMeter Load Test with your JMX script of any mobile application, web application, or API on RedLine13. Here are example results to check out.
Or go and try your own load test.