JMeter - xpath extractor not writing to user-defined variables outside thread group - jmeter

I am having a problem with JMeter. I have a thread group with a web service request and an xpath exctractor. I set the reference name field in the xpath extractor to a user-defined variable that's defined outside the thread group. However, that user-defined variable is never set. I know because any subsequent web service request that references that user-defined variable fails.
Note that this is NOT a problem when I place the user-defined variables within the same thread-group as the web request. Then, the user-defined variable gets set.
How do I work around this bug? I need a way for one thread group to set a user-defined variable through the xpath extractor, so the user-defined variable can be used by another thread group.

Greetings,
This is actually a feature of Jmeter. (I know..I cringed just writing that). Variables are local to the thread group. The only way to share variable values between threadgroups is to use properties.
There are two functions you'll need to use:
${_setProperty} and
${_property}

Related

How to pass parameter (token) to another thread group , i need to use the parameter pass from another threadgroup as dynamic url

How to pass parameter (token) to another thread group , i need to use the parameter pass from another threadgroup as dynamic url ( as the image above , i need to use Survey Token as parameter on the next threadgroup)
i already try some tips , but still get stucked
the token passed from the result GETSurveyToken is not successful place after /svap/survey/[token]
hope anyone can help ??
thanks
i try using : regular expression extractor, json extractor, dan using Preprocessor to get the parameter/variable
As per documentation:
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.
What Regular Expression Extractor gives to you is a JMeter Variable so in order to be able to use it in other thread in this or other thread group you need to convert it into a JMeter Property using __setProperty() function. Once done you can access the value using __P() function
If your logic is more complex, i.e. you need to "wait" in 2nd thread group until the token is available consider using Inter-Thread Communication Plugin

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

Use regular expression extractor variable in Tear Down Thread Group

we have to save regular expression extractor variable in one file --> "GJC_NUMZCAISSE" Type="Integer" Value="(.+?)"/>
Data Used for StoreClosing Transaction : Closing - cbrauth,store,baid,GJC_NUMZCAISSE
GJC_NUMZCAISSE variable will fetch from Login and save into one file and use corresponded data for closing as written above.
As per JMeter Documentation:
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.
Regular expression extractor produces a JMeter Variable which is visible only to the Thread Group where it's declared.
If you want to access the variable value in the tearDown Thread Group you need to convert it into a JMeter Property using __setProperty() function.
The property value can be read using __P() function where required
More information: Knit One Pearl Two: How to Use Variables in Different Thread Groups

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.

Resources