Reputation: 17553
I need to set and get variables in Jmeter for API automation.
I am using the groovy script for same.
I have achieved same using code as below:
import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("PC_CREATED_PROMO_CODE", "shubhamvalue");
log.info("will it work? ="+JMeterUtils.getProperty("PC_CREATED_PROMO_CODE"))
Now the problem is I am not able to see the value in any contanier where I can set my hardcode values like token, baseURL, Headers. it should be similar we do in SOAP-UI or postman tests.
Please let me know if I can see these setProperty values in file/section/container in Jmeter.
Or suggest me any other workaround which is more feasible for same.
Any workaround will be helpful and appreciated in advance.
Upvotes: 4
Views: 16928
Reputation: 1
In JMeter, you can set and get environment variables using the built-in functions and properties. Environment variables in JMeter are often referred to as JMeter properties.
1.Setting Variables in JMeter GUI:
2.Setting Properties in JMeter Script:
${__setProperty(myVar, 123)}
1.Getting Variables in JMeter GUI:
${variable_name}
2.Getting Properties in Beanshell or JSR223 Script:
String myVarValue = props.get("myVar");
Upvotes: -1
Reputation: 168002
If you need to get and set variables I would recommend using vars
shorthand
As per documentation
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads, and need to be referenced using the __P or __property function.
So I would suggest setting variables as: vars.put('foo', 'bar')
and accessing them as ${foo}
where required as my expectation is that you will be getting different PC_CREATED_PROMO_CODE
for each thread (virtual user)
Also be aware that it is also recommended to avoid scripting where possible so consider going for JSON Extractor instead.
Upvotes: 0
Reputation: 17553
1) In JMeter GUI mode, under WorkBench, create Property Display by WorkBench > Add > Non-Test Elements > Property Display. Then select JMeter Properties checkbox to view all the exist properties
props.put("shubhamKey", "shubhamValue")
When you execute this code the property will set in a property file and you can see it in below location:
WorkBench > Add > Non-Test Elements > Property Display.
2) Now if you are want to use User Defined Variables in your scripts you can call value like below:
vars.get("shubhamUserKey")
Still looking to set the value from code in User Defined Variables
Upvotes: -1