How to put value of User Defined Variable into Bean shell Sampler Variable - Jmeter - 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");

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 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

what container we can use for global and environment variables in Jmeter for API testing

I am trying to use Jmeter for API automation testing.
But Jmeter hasn't provided any separate containers for global and environment variables similarly like SOAP-UI and Postman.
I tried to use property file which also shared among all the JSR223 throughout the project but property having many others keys too and I haven't got any option by which I can delete key which been initiated once. Moreover, I can update the value too which seems most preferable to use
Below code I am using to set and get the values
props.put("shubhamKey", "shubhamValue")
props.get("shubhamKey")
I can also update it using same key
props.put("shubhamKey", "shubhamNewValue")
But as I said we can't delete key once initiated and it seems dangerous to delete from properties as it has many other keys too which may be required by Jmeter internally
After seeing too many things I have seen "User Defined Variables" where I can specify my key-value pairs. I am able to get the value using below code:
vars.get("shubhamLocalVariable")
But I am not able to set the value using below code:
vars.put("shubhamUserKeyagain","shubhamUservalue")
neither I got any option by which I further update it or delete it.
so Is there any feasible thing to store variables in Jmeter which can be easily created/deleted/updated using the code and can even call by other containers like HTTP Request.
Moreover I also want a container which preserve the values so the next iteration and next time (any time like other day) it will start with latest values
User Parameters
Jsr223
Any workaround will be helpful and appreciated in advance
As for me, I'm using a JSR223 Sampler at the begining of every Thread.
And for me it works:
vars.put("checksums_1","")
vars.put("checksums_2","")
vars.put("checksums_3","")
Firstly you can delete properties by using:
props.remove("shubhamKey");
And if you use specific prefix/suffix it shouldn't effect JMeter internally.
User Defined Variables isn't the best way to handle dynamic values, it's used for static variables
Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.
But you can set variable by string or variable or function, e.g:
For defining variables during a test run, see User Parameters which use similar assignments.

how to change the value of a variable in Jmeter

I am trying to use the variables captured in the Debug Sampler in Jmeter and then convert those variables into some other value. And then use it somewhere in the script.
I have added a BeanShell Sampler along with the Debug Sampler and tried to get the variables displayed in the Debug Sampler.
Below is the piece of code I have written in Jmeter.
Jmeter
Is my approach correct? Am completely new to Jmeter and have little Java knowledge. So please help me here and let me know how can I convert or use a variable through a Custom code in Jmeter.
It is almost correct, you have a couple of syntax errors (missing closing bracket and undefined SomeCharacter)
Also it is better to use JSR223 Elements and Groovy language rather than Beanshell, as Groovy performance is much better and it is more Java-compliant, see Groovy Is the New Black article for detailed explanation.
Final code should look something like:
def myVariable = vars.get("Corr_ContextN")
if (myVariable.equals("002056653")) {
vars.put("myvariable1", "SomeCharacter")
}
Keep in mind that you are not changing original Corr_ContextN, you are creating new variable myvariable1. Also in order to see new variable you need to move the Debug Sampler after the Beanshell Sampler
Your got the concept but your code has the following errors:
imports are wrong and useless. You only need to import Classes for unbound variables, ie the ones that are advertised in the component.
There's a missing ')' in the if clause
SomeCharacter is not defined
And you should avoid Beanshell and favor JSR223 Test Element with Groovy as per those recommandations:
http://www.ubik-ingenierie.com/blog/jmeter_performance_tuning_tips/
Note that there is also a __groovy function for your use case.

Resources