JMeter – Capture Dynamic Value present in Request Body

In the correlation article of JMeter, you got detailed knowledge on:

  • What is Correlation?
  • Why correlation is required?
  • How to capture dynamic values?

The purpose of this article is to elaborate on the process of finding out the dynamic value when it is present in the request body. This is a general scenario because most of the time dynamic value is available in the request body and you can easily identify it.

Steps to identify the dynamic value present in the request body:

  1. Add a ‘View Results Tree’ listener under the thread group
Figure 01
  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 02: 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.
JMeter - Capture Dynamic Value present in Request Body
Figure 03: 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.

JMeter - Capture Dynamic Value present in Request Body
Figure 04: 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.  

JMeter - Capture Dynamic Value present in Request Body
Figure 05

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 06

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 07

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 08

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

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

Now, the next 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 09: 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:
JMeter - Capture Dynamic Value present in Response Body
Figure 10
  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.

You may be interested: