Shubham Jain
Shubham Jain

Reputation: 17553

How to set and get environment variables in Jmeter to test API's

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.

enter image description here

Any workaround will be helpful and appreciated in advance.

Upvotes: 4

Views: 16928

Answers (3)

Wenyang Lu
Wenyang Lu

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.

Setting Environment Variables (Properties):

1.Setting Variables in JMeter GUI:

  • Open your JMeter test plan in the JMeter GUI.
  • Right click the test plan, then select Add > Config Element > User Defined Variables
  • Add a new variable with the desired name and value.

2.Setting Properties in JMeter Script:

  • You can set properties in your JMeter script using the __setProperty function. For example, to set a property named myVar with a value of 123:
${__setProperty(myVar, 123)}
  • Place this in a BeanShell PreProcessor, JSR223 Sampler, or use it in a Beanshell assertion where needed.

Getting Environment Variables (Properties):

1.Getting Variables in JMeter GUI:

  • You can get the value of a variable in your JMete using ${variable_name}

2.Getting Properties in Beanshell or JSR223 Script:

  • In a Beanshell or JSR223 script, you can use the following to get the value:
String myVarValue = props.get("myVar");

Upvotes: -1

Dmitri T
Dmitri T

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

Shubham Jain
Shubham Jain

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

Related Questions