Updated July 10, 2023
Definition of JMeter Variables
JMeter provides different properties to the user, in which that variable is one of the properties provided by the JMeter. All threads can utilize variables, as it is a fundamental feature of JMeter that can be applied universally. If we need to call values from any other sampler or element at that time, we can use the JMeter function with the variable. We can create variables as per our requirement that we call user-defined variables, as well as JMeter, which provides the by default variable to the user to complete their execution.
What are JMeter Variables?
JMeter does not fully set up the string factors when handling the capacities, so it does not establish factor names passed as boundaries and does not make variable references work. Thus, split() and regex() and the variable assessment capacities won’t work. The threadNum() capacity won’t work and doesn’t seem OK at the test plan level.
Simply consider one situation where we have various HTTP demands with various boundaries set but similar URLs. We realize that later on, to alter the URL. In this situation, we should alter each URL, and assuming we have many of them, that will be blunder inclined and time escalated. Utilizing User Defined Variables (UDV) is a superior methodology. This usefulness permits changing boundaries in various spots of the test plan.
How to Use JMeter Variables?
Now let’s see how we can use variables in JMeter as follows.
- First, we need a JMeter on our local machine and add the Thread Group for our respective Test Plan.
- After that, we must add the HTTP Request sampler to the already created Thread Group.
- Now in this step, we need to add the variable, so right-click on the Thread Group and click on the user-defined variable.
- After assigning the variable’s name, go to the HTTP request and add the variable name we created.
So this is the very basic step we need to follow. If we want to use variables with functions, we can also use them per our requirements.
Create JMeter Variables
Now let’s see how we can create variables in JMeter with examples as follows.
In the above point, we already discussed using variables in JMeter. Now let’s see in detail how we can use it as follows.
First, we need to create the new Test Plan that contains the Test Plan as shown in the following screenshot.
Now create a user-defined variable; here, we created a SampleDemo user-defined variable, as shown in the following screenshot.
The following screenshot shows that we must also add the HTTP request sampler.
In this step, we can add multiple samplers as per our requirements.
We know that there are two ways to access the API; the first way we already discussed in the above point is that we can directly send the URL of the API, and another way is to use a user-defined variable to access the HTTP request.
In our Test Plan, we already created one variable that SampleDemo, as shown in the above screenshot, now adds the name and access to the HTTP request as shown in the following screenshot.
In the above screenshot, we can see the user-defined variables. Here we first need to click on the add button and then add the variable’s name and value, as shown in the above screenshot.
Access that name into the HTTP request sampler, as shown in the following screenshot.
Add the tree to view the result and save and run Test Plan. After execution of the Test Plan, we can see the result in the view tree, as shown in the following screenshot.
Functions and JMeter Variables
Now let’s see functions and variables in Jmeter as follows:
JMeter provides a special kind function to the user to populate the sampler field as per our requirements. The basic syntax of the function is as follows.
${___Specified Function Name (specified variable name1, specified variable name2, specified variable nameN)}
Explanation:
In the above syntax, we use two parameters, such as the function name that we want and the variable name as per our requirement; inside the function, we can declare multiple variables as per our requirement, as shown in the above syntax.
JMeter provides different types of functions as follows.
1. Information
Under the information type, JMeter provides the different functions as follows.
- thradNum: If we want to get the thread number, then we can use this number.
- samplerName: It is used to get the name of a sampler.
- Time: It returns the system’s current date per our required format.
As well as information type provides the log, login, machinelp, and machineName functions.
2. Input
Under input type, JMeter provides different functions as follows.
- StringFromFile: It is used to read the text from the file.
- FileToString: We can use this function to read the entire file.
- XPath: It is used to read the XPath expression from a file.
- CSDVRead: It is used to read the CSV file.
3. Calculation
Under calculation, JMeter provides the different types of functions as follows.
- counter: If we want to increment the number at that time, we can use this function.
- intSum: It is used to add the integer to the number.
- longSum: It is used to add the long integer number.
- Random: By using this function, we can generate a random number.
- RandomSring: By using this function, we can generate a random string.
4. Scripting
Under scripting, JMeter provides the different types of functions as follows.
- BeanShell: It is used to run the BeanShell script.
- javaScript: By using this function, we can work on JavaScript.
These are some sample types and their function, as well as JMeter also provides some additional functions such as Properties, Variables, String, etc.
Conclusion
We hope from this article; you learn more about JMeter variables. From the above article, we have taken in the essential idea of JMeter variables and see the representation and example of the JMeter variables. This article taught us how and when to use the JMeter variables.
Recommended Articles
We hope that this EDUCBA information on “JMeter Variables” was beneficial to you. You can view EDUCBA’s recommended articles for more information.