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
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.
thread group >> Add >> Assertions >> Bean Shell Assertion
${__setProperty(Global_variable_Name,${Variable_name_whose_Value_to_be_Passed})}
and then in the other thread you can to call this global variable and can use it
below is the function you need to use to call the stored value:
${__property(global_variable_name)}
https://medium.com/@priyank.it/jmeter-passing-variables-between-threads-a4dc09903b59
This is not possible in JMeter, because it is not normal client behavior (sharing parameters between Threads). Instead of this use one Thread-Group with Controllers:
Thread Group
+ Create inventory
+ + XPath
+ Loop
+ + Purchase inventory
Another solution is to use the Simple Table Server to manage the dataset. This feature was add the 1.2 JMeter Plugins.
"The main idea is to use a tiny http server in JMeter Plugins to manage the dataset files with simple commands to get / add rows of data in files"
Look at the documentation : http://jmeter-plugins.org/wiki/HttpSimpleTableServer/
Regards.
Let' give a topic a second life :) One more way to transfer variables between threads is to write/read to file. Passing variables between threads
Well this is one way to do it; follow these steps and it will work, later you can adjust it to your needs! Variables are not shared among threads (JMeter calls this a feature probably :) ). But properties are! So set your variable as a propery like so:
1) Click your testplan and enable 'Run Thread Groups consecutively' -> this makes the thread groups run ordered and not randomly. (you can later adjust it, but for now to get it to work..)
2) create a threadgroup called 'setup' for instance; in that thread group add a BeanShell Sampler with the following code:
import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("theNameOfYourNewProperty", "theValueOfYourPropery");
So now the property has been set! If the value you want to store as a propery is a variable allready (User definded variable or reqex variable for instance) you can do:
JMeterUtils.setProperty("theNameOfYourNewProperty", vars.get("theNameOfYourVariable"));
3) add a testgroup 'actual test' for instance with a number of threads higher than 1; add a test and to that test add a BeanShell Preprocessor with the following code:
import org.apache.jmeter.util.JMeterUtils;
vars.put("theNameOfYourNewProperty", JMeterUtils.getProperty("theNameOfYourNewProperty"));
So now you've created a variable in that thread called theNameOfYourNewProperty which has the value of your system property theNameOfYourNewProperty. In your test you can now access it like:
${theNameOfYourNewProperty}
And it will work for each thread, not only just the first thread..
I found which I believe is the most simple way to get this done.
Use
Bean Shell PostProcessor
Set in one Thread Group
to set the variable (http://jmeter.apache.org/usermanual/best-practices.html#bsh_variables)
import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("PC_CREATED_PROMO_CODE", "value");
OR if you want to set it to a value contained in another variable
import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("PC_CREATED_PROMO_CODE", vars.get("Extracted_PC_CREATED_PROMO_CODE"));
Get in the other Thread Group
And then from the other thread group, read it via (http://jmeter.apache.org/usermanual/functions.html#__property)
${__property(PC_CREATED_PROMO_CODE)}