How to handle dynamic value (correlation) in JMeter?

JMeter - How to do correlation in JMeter

‘Correlation’ term refers to the handling of dynamic values coming from the server. These dynamic values are the unique values which are generated by the server for security purpose like the session ID, authorization token etc. In some cases, dynamic values also refer to the web content like values in a drop-down list, calendar date, item ID, product ID, order number etc. Through correlation, you can capture these dynamic values and pass in the subsequent requests. This is the basic concept of ‘Correlation’ in JMeter.

Why do we need to correlate the dynamic values?

To get the answer to this question, firstly you need to understand what exactly happens at the time of script recording, script replay and after correlating the dynamic values.

Let’s see, how the client and server act when some dynamic values are exchanged between them? This is the scenario while you record a script:

Figure 01: Recording Scenario

When you replay the script without any changes then the script fails because of the dynamic value (sessionid) generated by the server does not match with the value return back by the client. Refer to the given Figure 02, while replaying the script, the server-generated the session id as 222, but JMeter script sent the recorded value i.e. 111 which was captured during recording (Figure 01). Hence the server refused to serve the request and threw an error.

Figure 02: Replay without correlation Scenario

Now, when you correlate the dynamic value (sessionid) and replay the script then the JMeter script captures and saves the latest value generated by the server and sends back to the server in the next request. The server validates the returned value with generated value and gives the proper response. Hence the request is marked as passed at user end.

Figure 03: Replay after Correlation

Hope you understood, why correlation is so important in JMeter scripting.

Now, the second thing that you should know is:

What are the common values which require correlation?

  1. Session ID
  2. Access Token
  3. Customer Name / ID
  4. Order Number
  5. Bill Number
  6. Number of records displayed on a page
  7. Current Date and Time

There could be more values which depend on the type of the application and the term used to denote them. Keep in mind your ultimate goal must be to find out all the dynamic values which cause the failure of the script and correlate them. Now, the next question arises ‘HOW?’

How to handle dynamic value (correlation) in JMeter?

There are 3 major steps in correlation which are:

  1. Scripting (How to record JMeter script?)
  2. Identification (of dynamic value)
  3. Implementation (of correlation logic)

We have already discussed the first step in the previous article, so now starting with the second step i.e. identification of dynamic values. In JMeter, two elements are very much required to identify the dynamic values and capture it in a variable. These elements are Postprocessor and Listener. Below is the list of postprocessors. You can use any of the postprocessors as per your convenience and requirement:

  • Postprocessor
    • Regular Expression Extractor
    • Boundary Extractor
    • JSON Extractor
    • JSON JMESPath Extractor
    • CSS Selector Extractor
    • XPath Extractor XPath2 Extractor
  • Listener
    • View Results Tree (Used during script recording)

Steps for dynamic value identification:

  1. Add a ‘View Results Tree’ listener under thread group
Figure 04
  1. In the Thread Group, select the ‘Stop Thread’ option for ‘Action to be taken after a sampler error’.
  2. Make sure the input value of Number of Threads (users), Ramp-up period (seconds), Loop Count should be 1.
Figure 05: Thread Group
  1. Click the ‘Run’ button
  2. The script may fail at a particular sampler where correlation is required.
  3. Refer to the script and click on the same sampler which is failed during replay.
  4. You will get some parameters and their values which are passing in the request
  5. Copy the parameter value present under the ‘Value’ column.
Figure 06: Sampler with Dynamic Value

Step 1: Refer to the recorded ‘View Results Tree’ listener

Step 2: Paste the copied value in the search field of recorded View Results Tree listener and press ‘Search’ button.

Step 3: JMeter will highlight the request where this value is available. Now select the very first highlighted request.

Figure 07: View Results Tree

Step 4: Click ‘Response Data’ tab.

Step 5: Under ‘Response Data’ tab, click ‘Response Body’ tab.

Step 6: Again paste the same value in the search field of Response Body.

Step 7: Click the’ Find’ button. Check the place where this dynamic value appeared in the response body. The searched value will be highlighted.  

Figure 08

Step 8: Once JMeter finds out the value in the response body, then copy the string in such a way so that you can write a unique regular expression.

Learn – How to write the regular expression?

Step 9: Prepare a regular expression statement which can capture the dynamic value. In this example, the regular expression statement will be:

domainID=([0-9a-z-]+)&

Figure 09

Step 10: Verify the correctness of regular expression.

  1. To verify the correctness of regular expression statement, write the same regular expression in the search box of the response of the sampler (Recorded View Results Tree Listener).
  2. Checkmark the ‘Regular exp.’ option.
  3. Press ‘Find’ button
Figure 10

OR

  1. You can also select the ‘RegExp Tester’ from the drop-down of view results tree listener
  2. Paste the regular expression statement
  3. Click ‘Test’.
Figure 11

The result in both the cases should highlight the correct dynamic value.

  1. Repeat these steps until you find all the dynamic values.

Now, the third step is implementation.

Steps to implement the correlation:

  1. For this purpose, add a regular expression extractor under the same sampler whose response contains the dynamic value and paste the regular expression statement (from step 9) in the ‘Regular Expression’ field. Also, provide the Name of created variable along with the Template, Match No. and Default Value.

Read about ‘Regular Expression Extractor’ postprocessor

Figure 12: Regular Expression Extractor
  1. The last step is to replace all the occurrence of dynamic values in the subsequent requests by ${<RegExVaribaleName>} syntax. Refer to the below screenshot:
Figure 13
  1. Re-run the script and verify the correctness of correlation by referring the view results tree listener or by adding a debug sampler.

There could be 3 possible cases of the availability of dynamic values. Each case has a slightly different approach than others. Click the link to get full detail on each approach:

  1. Dynamic value in the request body
  2. Dynamic value in the request URL
  3. Dynamic value in the redirected request

Leave a Reply

Your email address will not be published. Required fields are marked *