Definition of JMeter Variables
JMeter provides the different properties to the user, in which that variable is one of the properties that are provided by the JMeter. Basically, variables are used to thread and this is a very basic property of JMeter that can be applied to all threads. If we need to call values from any other sampler or any other 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 is JMeter variables?
JMeter string factors will have not been completely set up when the capacities are handled, so factor names passed as boundaries won’t be set up and variable references won’t work. Thus, split() and regex() and the variable assessment capacities won’t work. The threadNum() capacity won’t work and it doesn’t seem OK at the test plan level.
Simply consider one situation where we have various HTTP demands that have various boundaries set, however 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. For this situation, 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.
1. First we need a JMeter on our local machine and add the Thread Group for our respective Test Plan.
2. After that we need to add the HTTP Request sampler to the already created Thread Group.
3. Now in this step we need to add the variable, so right-click on the Thread Group and click on the user-defined variable.
4. After assigning the name of the variable go to the HTTP request and add the variable name that we created.
So this is the very basic step we need to follow. If we want to use variables with functions then we can also use them as per our requirement.
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 how to use 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.
Now we also need to add the HTTP request sampler as shown in the following screenshot.
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 that we can 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 into the HTTP request as shown in the following screenshot as follows.
In the above screenshot, we can see the user-defined variables, here we first need to click on the add button and after that add the name of the variable and value as shown in the above screenshot.
Now access that name into the HTTP request sampler as shown in the following screenshot as follows.
Now add the tree to view the result and save Test Plan 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 as follows.
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 field of the sampler 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 basically, we use two parameters such as function name that we want and 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 is used to return the current date of the system as per our required format.
As well as information type provides the log, logn, 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: If we require reading the entire file then we can use this function.
- 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 the random number.
- RandomSring: By using this function we can generate the random string.
4. Scripting
Under scripting, JMeter provides the different types of functions as follows.
• BeanShell: It is used to run 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 we also see the representation and example of the JMeter variables. From this article, we learned how and when we use the JMeter variables.
Recommended Articles
This is a guide to JMeter Variables. Here we discuss the definition, What is JMeter variables, How to use JMeter variables? Examples with code implementation. You may also have a look at the following articles to learn more –