Requests correlation#
PFLB Platform lets you extract values from the response, for example, to get a session token. If the response contains values corresponding to the specified expression or boundaries, then the values are written to the variable. You can use the value of the variable as a parameter in the following test requests.
Data is extracted using extractors:
Regex extracts data using regular expressions. For more information about the syntax, see Regular expression syntax cheat sheet.
jSONPath extracts data from responses in jSON format, for example, when testing a REST API. It uses JSONPath query language. For more information about the syntax, see JSONPath expressions.
XPath extracts data from responses in HTML and XML formats, for example, when testing sites or applications that use SOAP. It uses XPath query language. For more information about the syntax, see XPath Syntax.
Boundary. JMeter Boundary Extractor allows you to extract values from a server response using left and right boundaries. For more information, see JMeter Documentation.
Let’s highlight the advantages and disadvantages of extractors:
Regex |
jSONPath |
XPath |
Boundary |
|
---|---|---|---|---|
Pros |
Search for data using complex templates |
Useful when testing OAuth authentication mechanisms, which uses jSON to send and receive access and refresh tokens |
Parsing HTML tags without attributes |
Easy to use. You don’t need to know the syntax of regular expressions, JSONPath, and XPath |
More performance than JSONPath and XPath |
Useful when testing Single Page Web Apps developed with React and AngularJS |
Parsing RSS feeds |
More performance than other extractors |
|
The ability to use Tidy parser |
||||
Parsing JSON with same attribute names |
||||
Cons |
Less performance than Boundary Extractor |
Less performance than Regex and Boundary Extractor |
Less performance than other extractors |
Doesn’t support complex search patterns |
Could be an overkill when a search is relatively simple |
It builds DOM tree to parse HTML code, it consumes CPU and memory |
Add an extractor#
To extract values from the response, follow these steps:
Open the test.
Go to the group or click .
Click for the required request and go to the Extract from response tab:
Select the value from the drop-down list:
Regex. Regular expression.
Fill in the fields:
Variable name. The variable into which the parsing results are saved.
Regular expression. For more information, see Regular expression syntax cheatsheet and Examples of using regular expressions.
Group’s number. The group to be extracted from the regular expression. For example, 1 value extracts the first group value. The default is 0 value, used to extract the whole regexp.
Select the field from which you need to extract the values:
Body. The response body without headers.
Request headers.
Response code. For example, 200.
Response message.
Url. Request URL.
Select the value from the drop-down list:
Random match. To select any of the values.
Specific match. To select a particular value.
All matches. To select all values. As a result, an array of values is formed: Param_1, Param_2, Param_n, where Param is the name of the variable, and n is the size of the array.
Note
When using the extracted parameter, manually specify the sequence number of the occurrence:
${param_n}
, where n is the sequence number of the occurrence. If you use such a parameter or header without specifying the occurrence number, an empty value is sent.
jSONPath. Request to JSON file.
Fill in the fields:
Variable name. The variable into which the parsing results are saved.
JSONPath expression. For more information, see JSON path syntax in detail and Example of using jSONPath.
Select the value from the drop-down list:
Random match. To select any of the values.
Specific match. To select a particular value.
All matches. To select all values. As a result, an array of values is formed: Param_1, Param_2, Param_n, where Param is the name of the variable, and n is the size of the array.
Note
When using the extracted parameter, manually specify the sequence number of the occurrence:
${param_n}
, where n is the sequence number of the occurrence. If you use such a parameter or header without specifying the occurrence number, an empty value is sent.
XPath. Request to XML document elements.
Fill in the fields:
Variable name. The variable into which the parsing results are saved.
XPath expression. For more information, see XPath Syntax and Example of using XPath.
Select the value from the drop-down list:
Random match. To select any of the values.
Specific match. To select a particular value.
All matches. To select all values. As a result, an array of values is formed: Param_1, Param_2, Param_n, where Param is the name of the variable, and n is the size of the array.
Note
When using the extracted parameter, manually specify the sequence number of the occurrence:
${param_n}
, where n is the sequence number of the occurrence. If you use such a parameter or header without specifying the occurrence number, an empty value is sent.
Boundary. JMeter Boundary Extractor allows you to extract values from a server response using left and right boundaries. For more information, see JMeter Documentation and Example of using boundaries.
Fill in the fields:
Variable name. The variable into which the parsing results are saved.
Left border. Left boundary of value to find. This is an optional field.
Right border. Right boundary of value to find. This is an optional field.
Select the field from which you need to extract the values:
Body. The response body without headers.
Request headers.
Response code. For example, 200.
Response message.
Url. Request URL.
Select the value from the drop-down list:
Random match. To select any of the values.
Specific match. To select a particular value. The occurrence numbers start with 1.
All matches. To select all values. As a result, an array of values is formed: Param_1, Param_2, Param_n, where Param is the name of the variable, and n is the size of the array.
Note
When using the extracted parameter, manually specify the sequence number of the occurrence:
${param_n}
, where n is the sequence number of the occurrence. If you use such a parameter or header without specifying the occurrence number, an empty value is sent.
Click Save.
To delete values extraction, click .
See also
Preview of the response#
For the convenience of configuring data extraction, use the preview of the response:
Select a data source, for example, HAR.
Go to the required tabs: Headers, Body, Cookies:
Examples of using extractors#
Regular expression#
Example 1#
Task: get a list of currencies in the online store https://ecommerce.pflb.us/.
Solution:
Open the site in a browser, for example, in Google Chrome.
Open DevTools.
Go to the Networks panel and in the Name column select
/ecommerce.pflb.us/
.Go to the Response tab and analyze the response:
Generate a regular expression, for example, in RegEx101:
Copy the response to the Text String field.
In the Regular Expression field, specify the string with the button tag:
<button class="currency-select btn btn-link btn-block" type="button" name="EUR">€ Euro</button>
Replace the value of the name attribute and the contents of the button tag with the expression
(.*?)
. The expression replaces any characters:<button class="currency-select btn btn-link btn-block" type="button" name="(.*?)">(.*?)</button>
Escape the special characters
[ ] \ ^ $ . | ? * + ( )
using the\
symbol. In our case:<\/button>
.Add the
g
flag at the end of the expression. The flag returns all matches from the response.Make sure that the expression extracts the required values.
Open a test and add the GET-request
https://ecommerce.pflb.us/
.Go to the Extract from response tab.
In the drop-down list, select Regex.
Enter a variable in the Variable name field, for example,
currency
.Specify the regular expression:
/<button class="currency-select btn btn-link btn-block" type="button" name="(.*?)">(.*?)<\/button>/g
In the Group’s number field, enter
2
.In the drop-down list, select Response headers.
In the drop-down list, select All matches.
Example 2#
Task: get the cookie value when testing the site https://ecommerce.pflb.us/.
Solution:
Record a HAR file and import it into a test or a group.
Open the request, in response to which the server returns the unique Cookie value.
Go to the Extract from response tab.
Analyze the cookies received from the site https://ecommerce.pflb.us/:
Open preview response.
Select the HAR data source.
Go to the Cookies tab and get the value of the OCSESSID Cookie:
On the Extract from response tab, in the drop-down list, select Regex.
Enter a variable in the Variable name field, for example,
OCSESSID
.Specify the regular expression:
/Set-Cookie: OCSESSID=(.*?);/
. Before running the test, check your expression in online services, for example RegEx101.In the Group’s number field, specify the number of the group to extract from the regular expression. In our example, specify the value
1
.Note
A group is a part of a regular expression template enclosed in parentheses. The group allows you to get a specific part of the string that satisfies the expression.
In the drop-down list, select Response headers.
In the drop-down list, select Specific match and enter
1
.
jSONPath#
Task: get prices for all products in the store from the response to the request in jSON format:
{
"store": {
"item": [
{
"category": "laptops",
"name": "Sony VAIO",
"brand": "Sony",
"availability": "In Stock",
"price": 1200,
"CPU": "Intel"
},
{
"category": "phones",
"name": "IPhone",
"brand": "Apple",
"availability": "In Stock",
"price": 150
}
]
}
}
Solution:
In the drop-down list, select jSONPath.
Enter a variable in the Variable name field, for example,
price
.In the JSONPath Expression field, enter $.store..price. Before running the test, check your expression in online services, for example JSONPath Online Evaluator or JSON Path Finder.
What else can you get from the response using JSONPath?
JSONPath
Result
$.store.item[*].name
The names of all items in the store
$..brand
All brands
$.store.*
All items in the store
$..item[1]
The second item
$..item[0,1]
$..item[:2]
The first two items
$..item[(@.length-1)]
$..item[-1:]
The last item
$..item[?(@.CPU)]
All items with CPU
$.store.item[?(@.price < 1000)]
All items cheaper than 1000$
$..*
All members of JSON structure
Select All matches.
XPath#
Task: get the names of products more expensive than $ 200 from the response to the request in XML format:
<?xml version="1.0" encoding="UTF-8"?>
<store>
<item category="laptops">
<name>Sony VAIO</name>
<brand>Sony</brand>
<availability>In Stock</availability>
<price>1200.00</price>
<CPU>Intel</CPU>
</item>
<item category="phones">
<name>IPhone</name>
<brand>Apple</brand>
<availability>In Stock</availability>
<price>150.00</price>
</item>
</store>
Solution:
In the drop-down list, select XPath.
Enter the name of the variable, for example
item
.In the XPath Expression field, enter
/store/item[price>200]/name
. Before running the test, check your expression in online services, for example xpather or XPath Tester.What else can you get from the response using XPath?
XPath
Result
/store/item/name
The names of all items in the store
//brand
All brands
/store/*
All items in the store
/store//price
The price of all items
//item[2]
The second item
//item[position()<3]
The first two items
//item[last()]
The last item
//item[CPU]
All items with CPU
//item[price<1000]
All items cheaper than 1000$
//*
All elements in XML document
Select All matches.
Boundary#
Task: get the name of the first item from the response:
{
"store": {
"item": [
{
"category": "laptops",
"name": "Sony VAIO",
"brand": "Sony",
"availability": "In Stock",
"price": 1200,
"CPU": "Intel"
},
{
"category": "phones",
"name": "IPhone",
"brand": "Apple",
"availability": "In Stock",
"price": 150
}
]
}
}
Solution:
In the drop-down list, select Boundary.
Enter the name of the variable, for example
item
.In the Left border field, enter
"name": "
.In the Right border field, enter
",
.Select the Response message field.
Select the Specific match value and enter
1
.