JMeter – Passing Variable Value to another Thread Group

Under a Test Plan, you can add multiple Thread Groups. But these Thread Groups are independent of each other. The child elements of these Thread Groups have limited scope and do not share any information outside the Thread Group scope. Sometime in JMeter, there is a requirement of passing the value of the variable from one Thread Group to another Thread Group; especially when you test API.

To handle this unique situation, JMeter has a function called __setProperty that helps to share the data (information) between thread groups. Hence this function solves the issue of Thread Group intercommunication. How? Let’s see.

Example:

Consider, there are two thread groups. The value comes in the response of the request of the First Thread Group needs to be passed in the Second Thread Group.

Refer to the below figure (Figure 01); you can see 2 Thread Groups

  1. Thread Group 01
  2. Thread Group 02

Each Thread Group has 1 request. To make them more understandable, let’s call them fetcher and consumer. It means the value that comes in the response of 1.1 HTTP Request (Fetcher) will be passed in the request of 2.1 HTTP Request (Consumer).

JMeter - Passing Variable Value to another Thread Group - Test Plan
Figure 01

Step to implement the logic for passing the variable value to another Thread Group:

  1. Add a ‘Regular Expression Extractor‘ post-processor as a child element of 1.1 HTTP Request (Fetcher) and fetch the value in a variable (say employeeID)
  2. Add a ‘BeanShell Assertion’ and write ${__setProperty(valueToPass,${employeeID})}
JMeter - Passing Variable Value to another Thread Group - BeanShell Assertion
Figure 02
  1. Use ${__property(valueToPass)} where you want to pass the value in the different Thread Group.
Figure 03

Before running the test make sure to checkmark ‘Run Thread Groups consecutively (i.e. one at a time)’ option in ‘Test Plan’ so that value can be fetched by Thread Group 01 prior to the execution of Thread Group 02.

Refer to the below screenshot, ‘1’ is passed as a value of the ‘valueToPass’ variable coming from Thread Group 01.

Figure 04

Logic Behind The Tip

Properties are shared between all JMeter threads, so if one thread sets a property, another thread can read the updated value. __setProperty() gets the value from the RegEx variable and assigns it to a property variable which can be read by another thread group using __property().


You may be interested:



Leave a Reply

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