How do I use a JMeter Variable declared in an extractor in a User Defined Variable config - jmeter

I have an http request that uses an extractor to set a JMeter variable (lets call it test) from the body. When I look at the debug controller I can see this works fine. Next I want to append something to the beginning of the variable so I add a user defined variable node and add a variable with the name new and I set the value to ${test}. However when I look in the debug response I see ${test} instead of the value.
I tried the same thing setting the value manually in 2 different UDV nodes and it works fine, so how do I append to a JMeter variable declared in an extractor?

As per JMeter Documentation:
The User Defined Variables element lets you define an initial set of variables, just as in the Test Plan.
Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.
So the User Defined Variables element will be read only once and only when the Test Plan is started.
If you need to overwrite the current variable with a new value you can go for i.e. __groovy() function, the relevant syntax would be something like:
${__groovy(vars.put('foo'\, 'some_prefix_' + vars.get('foo')),)}
Demo:
vars is a shorthand for JMeterVariables class instance, it provides read-write access to all JMeter Variables in the current thread scope. Check out The Groovy Templates Cheat Sheet for JMeter to learn what else you can do with Groovy scripting in JMeter tests

UDVs can't be used in dynamic way because they are process once at the start of the test.
Don't use UDV, use JSR223 Sampler (or PostProcessor) with vars;
vars.put("new", "prefix"+ vars.get("test"))
Another option is to use Set Variables Action plugin

Related

Is it possible to add an object refernce User Defined Variable in JMeter?

I can see that User Defined Variables can populate vars with string values only. Is there a way to populate an object reference User Defined Variable in User Defined Variables element.
I have tried as UDV values something like ${__groovy(new java.util.concurrent.ConcurrentHashMap())}. But this way leads to some string retrieved from the variable later.
I have even tried to use side effects of User Defined Variables groovy scripts like ${__groovy(vars.putObject("key", new java.util.concurrent.ConcurrentHashMap(); "assigned")}. But this one leads to some JMeter compilation error while being syntactically OK groovy script in my opinion.
I have tried using scripting pre-processors put on top of the test plan like one of SO answers suggested but they work before each sampler in the tree and thus do not what is intended do.
I still can't find a way to initialize an object reference variable in JMeter at the test plan initialization phase hijacking the thread initializing UDVs.
I know of possibilities to populate vars with putObject(<key>, <reference value>) from within scripting elements like JSR223/BeanShell/some others sampler/pre-processors etc.
But I want to populate the vars for all my thread groups at the initialization of the test plan in order to avoid using props imposing synchronized lock penalty on each put/get call. And there is no possibility to add a sampler at the top of the test plan.
EDIT after accepting correct answer by Dmitri T (for those who want to know the answer right away in more clear form):
${__groovy(vars.putObject("<key>"\, new HashMap()); "virtually anything")}
is correct (while indirect) way to populate an object reference variable in UDV test plan element.
while being syntactically OK groovy script in my opinion.
it's only your opinion, look at jmeter.log file and you will see interpretation error there
As per JMeter Documentation:
If a function parameter contains a comma, then be sure to escape this with "\", otherwise JMeter will treat it as a parameter delimiter. For example:
${__time(EEE\, d MMM yyyy)}
the correct syntax would be something like:
${__groovy(vars.putObject('key'\,new java.util.concurrent.ConcurrentHashMap()),)}
Demo:
More information: Apache JMeter Functions - An Introduction

JMeter loop index always return 0 in Loop Controller

When I use the command ${__jm__myLoopControllerName__idx} in JSR223 Sampler, it always returns 0 as the index.
The sampler is in the Loop Controller. But I can see that the CSV file I'm looping is done for each line because in the listener "View Result Tree" I can see in the Request Headers, that the data is from each line. What am I doing wrong ?
Thanks for your help.
M.
Use vars to get variable in JSR223 script:
String index = vars.get("__jm__myLoopControllerName__idx");
variable inside ${} syntax is getting cached
ensure the script does not use any variable using ${varName} as caching would take only first value of ${varName}. Instead use : vars.get("varName")
As per JSR223 Sampler documentation:
The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:
Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
Or Use Script Text and check Cache compiled script if available property.>
When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.
So either put your ${__jm__myLoopControllerName__idx} to "Parameters" section and refer it as "Parameters" in your script:
or use vars shorthand for JMeterVariables class instance like:
vars.get('__jm__myLoopControllerName__idx')

How to use array object value in next thread group using bean-shell assertion?

I am using BeanShell assertion to passing value to next thread group. Now I wanted to use that value in next thread group. I am using set Property function and I can see in logs those values stored in variable.
Given you set some property value via __setProperty() function like:
${__setProperty(foo,bar,)}
you can read it using __P() function like:
${__P(foo,)}
If you want to fetch the value in a script - you can use props shorthand like:
String myValue = props.get("foo");
Also be aware that starting from JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating to JSR223 Assertion. If you're setting the property value from the script - don't inline JMeter Function, use the aforementioned props shorthand instead like:
props.put("foo", "bar"); // this creates "foo" property with the value of "bar"
You should be able to re-use the same code. Check out The Groovy Templates Cheat Sheet for JMeter article which covers your use case in particular and few more common tasks in general for more details.

Load properties file in jmeter then assign properties value to JMeter variable

I have condition where I need to load properties file and assign prop value to JMeter UDV.
I have been able to load property file successful, however I can not assign prop value to UDV
I have try the following:
Load prop file contain key value (i.e var_from_prop_file=1000)
Create UDV (with keyname "my.var" and value "${var_from_prop_file}")
Run debug script with debug sampler, I can see var_from_prop_file assigned to value 1000
However my.var still empty (no value).
my expectation when creating UDV with my.var = ${var_from_prop_file}, my.var value will be 1000 too, but it doesn't happen here.
I have try with __eval and __evalVar - no luck so far.
Is it possible to reference jmeter variable to properties file key?
and call variable in other place?
So far from debug sampler - looks like jmeter load UDV first and then jmeter properties not the way around.
All your tries are getting variables , you need to get property using different functions:
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.
In your case set my.var with the following value
${__P(var_from_prop_file)}
Or
${__property(var_from_prop_file)}
Similar result, just notice that __P have default value 1
The default value for the property. If omitted, the default is set to "1".
Another option is using JSR223 Sampler using vars to set variable and props to get property:
vars.put("my.var", props.get("var_from_prop_file"));
The fastest way to bulk convert JMeter Properties into JMeter Variables is using JSR223 Sampler and the following code:
SampleResult.setIgnore()
props.entrySet().each {entry ->
vars.putObject(entry.getKey(),entry.getValue())
}
Where:
SampleResult.setIgnore() - removes JSR223 Sampler from listeners and .jtl results file
props - shorthand for JMeter Properties (basically an instance of java.util.Properties)
vars - shorthand for the instance of JMeterVariables class

How to put value of User Defined Variable into Bean shell Sampler Variable - Jmeter

I try to move a User Defined variable to variable in beanshell sampler.
(I need the User defined variable to be part of a bigger string.)
When I try to move it, or make a copy of it I get error 500
can someone please advise how can I put the value of user defined field in bean-shell variable and than use it (not need to change the user defined variable just want it value)
In this script I Want to put the value of $Expected_Offer_ID to String variable Expected_Offer
There are (at least) 2 options:
Put ${Expected_Offer_ID} to "Parameter" section of the Sampler. You will be able to access it as Parameters in your script
Use vars.get("Expected_Offer_ID); where required. vars is a shorthand to JMeterVariables class instance, it provides read/write access to all JMeter Variables
Remember 2 things:
Never refer JMeter Variables and/or Functions in the Script body like ${myVar}, either use aforementioned "Parameters" section or code-based equivalents as they might resolve into something which can cause script interpretation failure or unexpected behaviour. Moreover, in case of Groovy language it prevents compiled scripts caching
Don't use Beanshell for scripting. Since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language as Groovy is more Java-compliant and performs much better. See Apache Groovy - Why and How You Should Use It article for more detailed explanation, benchmarks and some tips on Groovy JDK enhancements usage.
JMeter variables are accessed through vars object, use:
String Expected_Offer = vars.get("Expected_Offer");

Resources