I have a JMeter test with 2 Thread Groups - the first is a single thread (which creates some inventory) and the second has multiple threads (which purchase all the inventory). I use BeanShell Assertions and XPath Extractors to parse the returned value (which is XML) and store variables (such as the ids of the items to be purchased).
But, values that are created in the first Thread Group, whether extracted into standard ${jmeter}
type variables, or ${__BeanShell(vars.get("jmeter"))}
type vars, are not available in the second Thread Group. Is there anyway to create a variable in the first Thread Group and make it visible to the second?
Just add a bean shell assertion use a property function to assign the value to a variable (like a global variable) which will hold the value even after it goes to other thread. Thank you for the answer.
Note that variables cannot currently be nested; i.e. ${Var${N}} does not work. The __V (variable) function can be used to do this: ${__V(Var${N})}.
Here is the simple solution which I found for Running multiple Thread Groups in particular order: Check the option "Run Thread Groups consecutively" under "Test Plan" Order your "Thread Group/s" in the order you want to be executed using drag and drop approach.
All types of thread groups have a common option “Action to be taken after sample error”, used to specify what action to perform when an error occurs. It can be Continue , Start Next Thread Group, Stop Thread, Stop Test, Stop Test Now.
I was not able to do this with variables (since those are local to individual threads). However, I was able to solve this problem with properties!
Again, my first ThreadGroup does all of the set up, and I need some information from that work to be available to each of the threads in the second ThreadGroup. I have a BeanShell Assertion in the first ThreadGroup with the following:
${__setProperty(storeid, ${storeid})};
The ${storeid} was extracted with an XPath Extractor. The BeanShell Assertion does other stuff, like checking that storeid was returned from the previous call, etc.
Anyway, in the second ThreadGroup, I can use the value of the "storeid" property in Samplers with the following:
${__property(storeid)}
Works like a charm!
According to JMeter documentation:
16.12 Sharing variables between threads and thread groups
Variables are local to a thread a variable set in one thread cannot be read in another. This is by design. For variables that can be determined before a test starts, see Parameterising Tests (above). If the value is not known until the test starts, there are various options:
- Store the variable as a property - properties are global to the JMeter instance
- Write variables to a file and re-read them.
- Use the bsh.shared namespace - see 16.8.2 Sharing Variables
- Write your own Java classes
Another way to pass variable between the threads is to use jmeter-plugins as mentioned by Andrey Botalov below.
But I found that it is a bit confusing to use it first time but it gives full control of variable during passing from thread to thread. Follow my example with BeanShell usage and you see how easy it is:
Next referring to sections in picture bellow:
(1.1) Here I created custom variable in User Defined Variables (or you can do it with BSF Proccessor - disabled in this example (1.2))
(2.1)(2.4)I successfully used variable in first thread - nothing special :)
(2.2)Added BeanShell PostProcessor and customized my variable
(2.3)Added it to queue
(3.1) In second thread - variable is taken from queue - with any name you want. But be careful, use wisely Timeout, because this thread will wait til previous finish so it can get modified variable (experiment with some long response)
(3.2)(3.3)(3,4)That repeated steps of using and modifying variable
(3.5) Variable is sent once again in new queue - so provide new name to it
(4.1)(4.2)(4.3) Grabbed modified variable from new queue works like charm
Warning
If you add more threads then add some Counter to Thread Group with variable and add this variable name to queue name - do the same in Thread Group where you try to catch queue so queue will have unique name for each thread (write a comment if you need some clearer explenation)
If you have more than one http Request in one Thread Group then add thread communication pre processor as a child of last (or other if you want to achieve some custom thing) http Request
Play, modify, customize to get best result :) Adding more threads can result in unwanted behavior so you need to be watchful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With