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:

  1. Open the test.

  2. Go to the group or click edit_button.

  3. Click edit_button for the required request and go to the Extract from response tab:

    ../_images/um_extract_from_response.en.png
  4. Select the value from the drop-down list:

    • Regex. Regular expression.
      1. 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.

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

      3. 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.
      1. Fill in the fields:

      2. 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.
      1. Fill in the fields:

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

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

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

  5. Click Save.

To delete values extraction, click delete_button.

See also

Preview of the response#

For the convenience of configuring data extraction, use the preview of the response:

  1. Select a data source, for example, HAR.

  2. Go to the required tabs: Headers, Body, Cookies:

    ../_images/um_preview_response.en.png

Examples of using extractors#

Regular expression#

Example 1#

Task: get a list of currencies in the online store https://ecommerce.pflb.us/.

Solution:

  1. Open the site in a browser, for example, in Google Chrome.

  2. Open DevTools.

  3. Go to the Networks panel and in the Name column select /ecommerce.pflb.us/.

  4. Go to the Response tab and analyze the response:

    ../_images/um_devtools.png
  5. Generate a regular expression, for example, in RegEx101:

    1. Copy the response to the Text String field.

    2. 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>
      
    3. 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>
      
    4. Escape the special characters [ ] \ ^ $ . | ? * + ( ) using the \ symbol. In our case: <\/button>.

    5. Add the g flag at the end of the expression. The flag returns all matches from the response.

    6. Make sure that the expression extracts the required values.

  6. Open a test and add the GET-request https://ecommerce.pflb.us/.

  7. Go to the Extract from response tab.

  8. In the drop-down list, select Regex.

  9. Enter a variable in the Variable name field, for example, currency.

  10. Specify the regular expression:

    /<button class="currency-select btn btn-link btn-block" type="button" name="(.*?)">(.*?)<\/button>/g
    
  11. In the Group’s number field, enter 2.

  12. In the drop-down list, select Response headers.

  13. In the drop-down list, select All matches.

Example 2#

Task: get the cookie value when testing the site https://ecommerce.pflb.us/.

Solution:

  1. Record a HAR file and import it into a test or a group.

  2. Open the request, in response to which the server returns the unique Cookie value.

  3. Go to the Extract from response tab.

  4. Analyze the cookies received from the site https://ecommerce.pflb.us/:

    1. Open preview response.

    2. Select the HAR data source.

    3. Go to the Cookies tab and get the value of the OCSESSID Cookie:

      ../_images/um_preview_cookies.en.png

    Note

    You can also analyze the cookie in the response header using the developer tool in the browser, Fiddler or Charles.

  5. On the Extract from response tab, in the drop-down list, select Regex.

  6. Enter a variable in the Variable name field, for example, OCSESSID.

  7. Specify the regular expression: /Set-Cookie: OCSESSID=(.*?);/. Before running the test, check your expression in online services, for example RegEx101.

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

  9. In the drop-down list, select Response headers.

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

  1. In the drop-down list, select jSONPath.

  2. Enter a variable in the Variable name field, for example, price.

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

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

  1. In the drop-down list, select XPath.

  2. Enter the name of the variable, for example item.

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

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

  1. In the drop-down list, select Boundary.

  2. Enter the name of the variable, for example item.

  3. In the Left border field, enter "name": " .

  4. In the Right border field, enter ", .

  5. Select the Response message field.

  6. Select the Specific match value and enter 1.