4

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.

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125

3 Answers3

1

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.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • vars saves key-pair in which container.. means in property or User Defined Variables – Shubham Jain Jan 25 '18 at 12:43
  • additionally which container I should use for putting my variables .. property , user defined variables or any other option. variables needs to be updated time time as per script logic so what you will suggest in that case – Shubham Jain Jan 25 '18 at 12:45
  • I have posted the new question. refer here : https://stackoverflow.com/questions/48443621/which-container-is-suitable-to-add-update-remove-variable-which-can-we-shared-wi – Shubham Jain Jan 25 '18 at 13:15
0

To view properties in file/section you can use 2 functions __property or__P while the second will return 1 as default. in your case

${__property(PC_CREATED_PROMO_CODE)}

${__P(PC_CREATED_PROMO_CODE)}

For example you can change next sampler name to Post2 ${__property(PC_CREATED_PROMO_CODE)}

BTW you can set property use props instead

 props.setProperty("PC_CREATED_PROMO_CODE", "shubhamvalue" )

Example of use in HTTP Header Manager, adding cotentType from property:

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

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

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • You can set variable with property value as vars.put("shubhamKey", props.get("shubhamUserKey")) – Ori Marko Jan 25 '18 at 12:01
  • vars.put("shubhamUserKeyagain","shubhamUservalue") is not working to add values in User Defined Variables – Shubham Jain Jan 25 '18 at 12:08
  • User Defined Variables is not the best way to update variables. please open a new question because it's too broad to answer here – Ori Marko Jan 25 '18 at 12:09
  • I have posted the new question. refer here : https://stackoverflow.com/questions/48443621/which-container-is-suitable-to-add-update-remove-variable-which-can-we-shared-wi – Shubham Jain Jan 25 '18 at 13:07