JMeter : Define Test Fragment Inputs - jmeter

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

Related

JMeter Custom Plugin Variable Substitution

Context
I am developing a custom JMeter plugin which generates test data dynamically from a tree like structure.
The editor for the tree generates GUI input fields as needed, and therefore I have no set of defined configuration properties which are set in the respective TestElement. Instead, I serialize the tree as a whole in the GUI class, set the result as one property and deserialize it in the config element where it is processed further during test execution.
Problem
This works just fine, except that JMeter variable/function expressions like ${foo} or ${_bar(..)} in the dynamic input fields are not evaluated. As far as I understand the JMeter source code, the evaluation is triggered somehow if the respective property setters in org.apache.jmeter.testelement.TestElement are used which is not possible for my plugin.
Unfortunately, I was not able to find a proper implementation which can be used in my config element to evaluate such expressions explicitly after deserialization.
Question
I need a pointer to JMeter source code or documentation for evaluating variable/function expressions explicitly.
After I manages to setup the JMeter-Project properly in my IDE, I found org.apache.jmeter.engine.util.CompoundVariable which can be used like this:
CompoundVariable compoundVariable = new CompoundVariable();
compoundVariable.setParameters("${foo}");
// returns the value of the expression in the current context
compoundVariable.execute();

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.

Defining scope based user defined variables under different thread group

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

Resources