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

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.

Related

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 variables within JSR223 preprocessors in jmeter

I am using two JSR223 preprocessors,in which I want to use variable from one preprocessor into another preprocessor.
how can I use this in Jmeter??
Please assist me with an example.
In the first PreProcessor store some value into a foo JMeter Variable using vars shorthand for JMeterVariables class instance like:
vars.put('foo', 'some value')
In the second PreProcessor you can read the value using the same vars shorthand like:
String myString = vars.get('foo')
See Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this vars shorthand and other JMeter API shortcuts available for the JSR223 Test Elements
The variable can be seen using Debug Sampler and View Results Tree listener combination.

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

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

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

Put value in parameter using beanshell and jmeter

I have a question about a very basic script that I wrote,
I just want to get data from DB, put it in a variable using beanshell sampler.
and in the end of the thread group to create another bean shell and check the value of this variable.
the problem is at saving the data to a variable (var name is before), when I use the props.put command but the value still null.
Can someone please advise?
If you want to store a value into JMeter Variables you should amend your code like:
vars.put("before", String.valueOf("budgetInt"))
System.out.println("Before is " + vars.get("before"));
Once done you should be able to access the defined variable as ${before} or ${__V(before)} where required.
If you amend your property setting like:
props.put("Before", String.valueOf(budgetInt))
You should be also able to access the value as ${__P(Before,)}
Also consider switching to JSR223 Sampler and Groovy language.
var before isn't assign and therefore it's null.
You should get value as:
var before = props.get("Before");

Resources