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

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

Related

Declare a global variable in JMeter property file

I have a custom JMeter property file with some config of each thread group, and I read them by __P function. The example as following:
I have user count for each thread group. How can I set a global variable and point all group number to this number?
You can copy property value in JSR223 component using props:
props.put("group1.usercount",props.get("globle.usercount"));

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.

JMeter JSON Path extractor

I use JMeter 4, I have a problem in a JMeter JSON Path Extractor.
I have done an HTTP Request (POST) to a service that I wan't to test.
Response data from the service is
{"rid":"661ff2d7-12e8-e811-8110-00215a9b9851",
"participation":{"rid":"ed8cfced-0063-4fda-92fd-b23f50197797"}}
I wan't to extract the first GUID witch is allocated to the first rid.
As JSONPath Expression I have used $.rid and wan't to assign that value to a JMeter variable. Therefore I have checked the radio button JMeter variable and entered my JMeter variable which is int_rid.
When I execute my JMeter testplan the int_rid variable will be null.
In JSON Extractor If you check the radio button JMeter variable it expect to search inside the contents of JMeter variable, which you don't want, usually Main sample only is enough
Main sample only - only applies to the main sample
Sub-samples only - only applies to the sub-samples
Main sample and sub-samples - applies to both.
JMeter Variable Name to use - assertion is to be applied to the contents of the named variable
Put int_rid in Names of created variables field and chose Match No. 1 to find the first value

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

logging property value in jtl log file

I have been using "sample_variables" to print custom variables in jtl file.
I would also like to print custom parameter using this approach.
But specifying property as -
-Jsample_variables=prop1,prop1
does not log corresponding value in jtl file. And I see only null values in log file.
Is there a way to log properties in jtl file
UPDATE -
I converted the variable into property using User Parameters and test runs good but yet the variable value is not printed in jtl log. There is always null value printed for sample where variable is used. Test Plan looks as -
And I added following line to run script -
-Jsample_variables=verifytoken \
But verifytoken is always null for sample - getTokenInfo
Did I miss any configuration?
Convert properties to variables using __V() and __P() functions combination or __property() function like:
${__V(${__P(prop1,)})}
or
${__property(prop1,prop1,)}
Functions can be called in any place of your JMeter script.
References:
Functions and Variables
How to Use JMeter Functions

Resources