Defining scope based user defined variables under different thread group - jdbc

I have multiple thread group for a test plan in jmeter. I want to define user defined value's to a particular thread group, the variable name can be repeated in another thread group for its user defined values.
When I tried doing the above it was picking up the last value of the same variable name across thread groups and not using it scope wise.
Using for jdbc requests with queries

I answere this "old" question for everyone who has the same Problem and is looking for a working solution. At least in JMeter 5.11 the answer of Dmitri T is not always true.
From the JMeter Documentation:
Note that the values defined by the Test Plan and the User Defined
Variables configuration element are made available to the whole test
plan at startup. If the same variable is defined by multiple UDV
elements, then the last one takes effect. Once a thread has started,
the initial set of variables is copied to each thread. Other elements
such as the User Parameters Pre-Processor or Regular Expression
Extractor Post-Processor may be used to redefine the same variables
(or create new ones). These redefinitions only apply to the current
thread.
A good and working solution for using local variables, that are only visible in the actual Thread Group, is to use the User Parameters Pre-Processor instead of the User Defined Variables configuration element.
Happy load testing ...

JMeter Variables have scope limited to current Thread Group only. To make JMeter variables visible for all Thread Groups you need to convert them to JMeter Properties. See How to Use Variables in Different Thread Groups guide for details on how to do it.

I got the solution. Example: If you have some test cases under a random order controller and like that you have multiple random controllers. The test case's contain a variable name for a jdbc request connection then you can add user defined variable inside the random order controller which will be used by only those test case's under it.
Like this you can scope the variable name to a particular random order controller

Related

JMeter : Define Test Fragment Inputs

I'm trying to define fragment inputs, which are variables that should be defined to have the fragment execute successfully (I'm trying to achieve something similar to method arguments in programming languages)
I have tried to use test plan variables in the fragment like so :
But when the fragment is included in another test plan and with looking to the Debug PostProcessor output, those variables are completely ignored.
Please is there official JMeter documentation that mention test plan variables are ignored in this case ?
What is the cleanest way to define variables for a fragment with a scope local to the fragment or at least local to current thread ? My purpose to make a fragment clearly defined what are the variable it uses, so it can be easily reused by other developers
Thank you
If you need to declare the variables to be used in the test fragment - go for User Defined Variables configuration element like:
When you call a Test Fragment from a separate Test Plan the variables defined in that Test Plan will be available for usage.
Alternative option is using JMeter Properties which are global for the whole JVM

How to Store Regular Expression Extractor reference name as User defined variable (Global variable to test plan)

In Regular Expression Extractor I have stored reference name as prasad.
If I give my reference name to further scripts ${prasad} where ever I want it is working fine.
But I want to store these reference name as user defined variables (Global variables).
I am using only one thread group.
I am doing performance automation scripts so I want to store that reference name as user defined variable.
I didn't not understand properly,(Below Answer)..Can any one help me with brief explanation please...
This is stated in JMeter's best practices or mailing list answer:
Variables are local to a thread; a variable set in one thread cannot
be read in another. This is by design. For variables that can be
determined before a test starts, see Parameterising Tests (above). If
the value is not known until the test starts, there are various
options:
Store the variable as a property - properties are global to the JMeter
instance
To store in JMeter's "global variable"/property add JSR223 Sampler
props.put("a", vars.get("a"));
Now you can see you "global" variable ${a}

What is the best way to pass variables among tests from data entry till validation?

I have some test cases for a web application in Robot Framework. In some cases I define a unit and then validate this action by checking database and GUI. The variables which I use in define, should be available in validation in order to check details; keep in mind they are randomly generated in the test case. I have three approaches in mind to pass variables from define to validation:
Make variables global and use their global names further; it makes the scenarios ambiguous since the reader can't detect where did this variable come from without checking inner steps.
Pass variables to both define and validation keywords; scenarios look weird when vast number of parameters are required.
Save variables in a dictionary and pass it to both define and validation keywords.
Which one is the best? Are there any other ways to do the process? Are there any other pros and cons which I've forgotten?
3rd option is better to storing variables in dictionary.
There is similar way out also
Consider following is keyword
My Keyword
[Argument] #{data}
// get respective values from keys and use further for validation
${value1}= Get Template Value From List ${Key1} #{data}
${value2}= Get Template Value From List ${Key2} #{data}
Call above keyword as follows
*** Test Cases ***
Test data
My Keyword
... key1=value1
... key2=value2
So above code will increase readability as you know what kind of data your test case is using
As you are passing data at your test case level, you don't need to go anywhere else to find data.
I finally use a combination of #2 & #3. I used #3 in the top level of test cases and #2 for some inner keywords which don't need all the data and a subclass of that is sufficient for them.

In JMeter, where is the best place to define variable?

In JMeter, I have added a Config Element of User Defined Variables to state my variable data. However, I see I can do the same thing up in the main Test Plan element.
When should you define your variables within the Test Plan and when should you define them within the Config Element? What's the pro/con of each?
Thanks.
They are the same. Take a look at User Defined Variables description:
The User Defined Variables element lets you define an initial set of variables, just as in the Test Plan
But using separate config elements makes sense for 2 reasons:
Script organization. For example if you have many variables, instead of dumping them all in main Test Plan element, you could define several config elements with the name that suggests what kind of variables they store.
Ability to use variables defined in previous User Defined Variables elements. According to the same reference:
UDVs are processed in the order they appear in the Plan, from top to bottom. <...> the variables are not available for use until after the element has been processed, so you cannot reference variables that are defined in the same element. You can reference variables defined in earlier UDVs or on the Test Plan.
So having several elements provides you with an ability to use previously defined variables in the further ones. One of the use cases is, for instance, definition of some reusable function(s) in one User Defined Variables element, and using them in the following User Defined Variables elements.

How to pass a non-string value between two JSR223 (groovy) samplers in JMeter?

In JMeter I need to pass a value from one JSR223 sampler (groovy) to another one within the same Thread group. For now I use a User Parameter (vars.put(...), vars.get(...)) but it has a disadvantage that extra conversions to and from string are required to pass non-string data. Is there a way to pass an object (e.g. Integer or Date) between two groovy samplers in JMeter?
As per How to use BeanShell: JMeter's favorite built-in component guide:
vars
vars is the most frequently used component which represents JMeter Variables. It’s an instance of org.apache.jmeter.threads.JMeterVariables class and provides read/write access to current variables, capable of enumerating/changing existing, creating new ones and obtaining nested properties.
If you look into JMeterVariables class JavaDoc by following above link you'll be able to see putObject(String key, Object value) method which seems to be what you're looking for.
So in the first sampler:
Date now = new Date();
vars.putObject("now", now):
And in the second sampler:
Date then = vars.getObject("now");
Alternatively you can use props.put(String, Object) and props.get(String, Object) - in that case you will be able to access the values from different Thread Groups.

Resources