Is it possible to add an object refernce User Defined Variable in JMeter? - 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

Related

Variable value not updated in each iteration in Jmeter

I am using timestamp as a value in my script to keep it unique and distinguishable in each of the iterations. I am using ${__time(/1)} function and storing it in the user defined variable inside a transaction controller and later using same variable in other transactions and json payload as well. This works well for single iteration.
I expected that in each iteration timestamp will be updated. but I can see that in each iteration same timestamp is being used. How could I make it updating timestamp in every iteration as I need to use only one timestamp value in an iteration and not repeating in successive iterations.
The User-Defined Variable config element is processed only once at the start. It is processed after the test plan. It is not suitable for your use case.
UDVs should not be used with functions that generate different results each time they are called. Only the result of the first function call will be saved in the variable.
There could be a number of options.
Using User Parameters Pre-processor
You may set the Update Once Per Iteration to suit your requirement.
Using Set Variables Action plugin
Set the value in JSR223 Pre-processor
You may place the controller below the Test Plan.
As per User Defined Variables documentation:
Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.
So the variable is evaluated only once, when the test starts, if you want it to return you the current timestamp each time it's called - just use __time() function directly where required, there is no need to declare it as the variable.
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction
I also don't think that /1 bit in your ${__time(/1)} function does anything meaningful, maybe it worth considering removing it completely
Adding value to a variable will save time only once on load, you can put the time function inside payload to get different times
Another option is to use pre processor as User Parameters over User Defined Variables
For defining variables during a test run, seeĀ User Parameters. UDVs are processed in the order they appear in the Plan, from top to bottom

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 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");

How to use response field in a new request in Jmeter

I have this issue that I want to solve.
I want to create a new http request using field from previous response
I send a request
I used Json extractor to move the response string to a variable (let call this string nurl)
I used Regular expression and move the field that I want to "Reference Name"
(meanning from nurl I just want tt_cid)
Now I want to make a new call, and use that field tt_cid in my new call
How I shall call tt_cid? since it is not passed as User Defined Variables,
when I use tt_cid, I do not think J meter know it, since it is not written there, I just pulled it from the response.
Provided a Pic of what I have done
Regards to you all
Short answer call it ${tt_cid}.
since it is not passed as User Defined Variables, when I use tt_cid,
I do not think J meter know it,
For your understanding add Debug Sampler after Regular expression,
You will see all your JMeter variables, including tt_cid, which can be called as other variables ${tt_cid} inside other Samplers.
It's called Reference Name and not Variable Name because it's more complicated, You should read JMeter's Regular Expression to understand how it works internally, But basically it saves more than just 1 Variable.

Resources