Passing Variable Value to another Thread Group in Jmeter
In Jmeter, under a Test Plan, multiple Thread Groups can be added. 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. Sometimes 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.
Example:
Consider, there are two thread groups. The value comes in the response to 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
- Thread Group_01
- 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 CreateContact (Fetcher) will be passed in the request of CreateCustomer (Consumer).
Step to implement the logic for passing the variable value to another Thread Group:
1. Add a ‘Jason Extractor‘ post-processor as a child element of Http Request CreateContact (Fetcher) and fetch the value in a variable (say “primary_id”)
2. Add a ‘BeanShell Assertion’ and write ${__setProperty(valueToPass,${primary_id})}
3. Use ${__property(valueToPass)} where you want to pass the value in the different Thread Group i.e. Consumer.
Before running the test make sure to checkmark the ‘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.
BACKGROUND LOGIC
Properties can be 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 Jason Extractor variable and assigns it to a property variable which can be read by another thread group using property().