2

I need to set and get variables in Jmeter for API automation.

I am using the groovy script for same.

I am able to store key-values in Property Files using below code:

props.put("shubhamKey", "shubhamValue")

I do not want to use the main property as it contains so many other keys which may use by Jmeter and we can't place any hardcode key in property directly.

But I am not an able set similar thing in any of below containers :

  • User Defined Variables
  • User Parameters

The code I am trying is as below:

vars.put("shubhamUserKeyagain","shubhamUservalue")

Use vars.put and execute the script and click on User Defined Variables. The new key should reflect there but it's not. That is my main issue

I have tried to place User Parameters just below in TestPlan, With-in the request where JSR223 Assertion is present, also tried to place it before and after JSR223 Assertion

My get value is working fine from these containers but the set is not reflecting over the containers. Below code, I am using to get the value

vars.get("shubhamLocalVariable")

Another code I have tried :

vars.putAll("myNormalValue","changeho")

Is there any other container in which I can set and get key-values using the script as groovy.

Environment :

  • OS : Window 10
  • Jmeter Version: 3.2 r1790748
  • Running bat file as an administrator

I have added my project in below URL:

https://ufile.io/7r6tk

Please refer and let me know if I missed anything

Reference Screenshots:

User Parameter

enter image description here

JSR233 Assertion

enter image description here

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

1 Answers1

2

This one works for me - I have cleared up your script

Zeburek
  • 25
  • 3
  • Thanks for your efforts @Zeburek ... Voting you up but still the question is same.. The values is not showing in User Defined Variables even after using vars.put("MyNewKey","ShubhamNewKey"). – Shubham Jain Jan 30 '18 at 12:09
  • Use vars.put and execute script and click on User Defined Variables .. The new key should refect there but it's not .. That is my main issue – Shubham Jain Jan 30 '18 at 12:16
  • @shubham-jain why are you looking for new line in User defined variables? if you create new variable, it would not add in offline, it's only created in runtime. So you wouldn't be able to watch for all variables parameters in User Defined Variables. If you want to monitor variables in runtime - use Debug Sampler (http://jmeter.apache.org/usermanual/component_reference#Debug_Sampler) – Zeburek Jan 30 '18 at 13:06
  • If you observe property file has ability to show value after execution. Example : props.put("shubhamKey", "shubhamValue") ... It will helpful in next iteration. Suppose my first thread has an id 1001 now I want that next iteration to pick up 1002 then I just increment it and save it to the variable container. – Shubham Jain Jan 30 '18 at 13:27
  • Now suppose next time execution I want to variable to pick the last incremented value set by the script in my previous execution time than saving the execution result is the last resort. that's, why I need a container which like same which is properties is doing currently. If nothing helps properties is my last resort – Shubham Jain Jan 30 '18 at 13:27
  • so, you're able to save file, e.g. csv, and load it every time when it needed. I think that it's impossible to update jmx file at runtime. – Zeburek Jan 30 '18 at 14:35