Dynamic value (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 purposes 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 them 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 the dynamic value (sessionid) generated by the server does not match the value returned back by the client. Refer to the given Figure 02, while replaying the script, the server generated the session id as 222, but the 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, the JMeter script captures and saves the latest value generated by the server and sends it back to the server in the next request. The server validates the returned value with generated value and responds properly. Hence the request is marked as passed at the user end.

Figure 03: Replay after Correlation

Hope you understood, why correlation is so essential 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 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 significant 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 them 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 the 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 the Number of Threads (users), Ramp-up period (seconds), and 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 the replay.
  4. You will get some parameters and their values which are passed 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 the recorded View Results Tree listener and press the ‘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 the ‘Response Data’ tab.

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

Step 6: Again paste the same value in the search field of the 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 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 the 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 the ‘Find’ button
Figure 10

OR

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

The result in both 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 the 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 occurrences 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 to 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 Comment