Apache JMeter Correlation Using the Regular Expression Extractor in Apache JMeter


Example

If you need to extract information from a text response, the easiest way is to use Regular Expressions. The matching pattern is very similar to the one used in Perl. Let’s assume we want to test a flight ticket purchase workflow. The first step is to submit the purchase operation. The next step is to ensure we are able to verify all the details by using the purchase ID, which should be returned for the first request. Let’s imagine the first request returns a html page with this type of ID that we need to extract:

<div class="container">
  <div class="container hero-unit">
    <h1>Thank you for you purchse today!</h1>
       <table class="table">
           <tr>
             <td>Id</td>
             <td>Your purchase id is 1484697832391</td>
           </tr>
           <tr>
             <td>Status</td>
             <td>Pending</td>
           </tr>
           <tr>
             <td>Amount</td>
             <td>120 USD</td>
            </tr>
         </table>
    </div>
</div>

This kind of situation is the best candidate for using the JMeter Regular Expression extractor. Regular Expression is a special text string for describing a search pattern. There are lots of online resources that help writing and testing Regular Expressions. One of them is https://regex101.com/.

enter image description here

To use this component, open the JMeter menu and: Add -> Post Processors -> Regular Expression Extractor

enter image description here

The Regular Expression Extractor contains these fields:

  • Reference Name - the name of the variable that can be used after extraction
  • Regular Expression - a sequence of symbols and characters expressing a string (pattern) that will be searched for within the text
  • Template - contains references to the groups. As a regex may have more than one group, it allows to specify which group value to extract by specifying the group number as $1$ or $2$ or $1$$2$ (extract both groups)
  • Match No. - specifies which match will be used (0 value matches random values / any positive number N means to select the Nth match / negative value needs to be used with the ForEach Controller)
  • Default - the default value which will be stored into the variable in case no matches are found, is stored in the variable.

The “Apply to” checkbox deals with samples that make requests for embedded resources. This parameter defines whether Regular Expression will be applied to the main sample results or to all requests, including embedded resources. There are several options for this param:

  • Main sample and sub-samples
  • Main sample only
  • Sub-samples only
  • JMeter Variable - the assertion is applied to the contents of the named variable, which can be filled by another request

The “Field to check” checkbox enables choosing which field the Regular Expression should be applied to. Almost all parameters are self descriptive:

  • Body - the body of the response, e.g. the content of a web-page (excluding headers)
  • Body (unescaped) - the body of the response, with all HTML escape codes replaced. Note that HTML escapes are processed without regard to context, so some incorrect substitutions may be made (*this option highly impacts performance)
  • Body - Body as a Document - the extract text from various type of documents via Apache Tika (*also might impact performance)
  • Body - Request Headers - may not be present for non-HTTP samples
  • Body - Response Headers - may not be present for non-HTTP samples
  • Body - URL
  • Response Code - e.g. 200
  • Body - Response Message - e.g. OK

After the expression is extracted, it can be used in subsequent requests by using the ${purchaseId} variable.

enter image description here

This table contains all the contractions that are supported by JMeter Regular Expressions:

enter image description here enter image description here