Posted

Reading Time: 3 minutes

In order to define your own variables and reuse them in your tests, it’s easier to use the User Defined Variables from JMeter.

Here is how to create a variable and use it in an HTTP Request from your JMeter Test Plan.

 

Steps

1. Open JMeter (here is a more detailed post on how to install JMeter) and Add a Thread Group to your Test Plan

2. Add a HTTP Request Sampler to your Thread Group

3. Right-Click the Thread Group and add User Defined Variables Config Element in your JMeter test
1-add-user-defined-varibles-jmeter

 

4. Create a new variable: var1 with value www.example.com
2-add-variable-in-user-defined-variables-jmeter

 

5. Go to the HTTP Request and add the variable name where you want to be replaced with its value, use ${var1}
3-user-variable-in-http-request-jmeter

 

6. Add a View Results Tree Listener in order to easily see the results of your request.
4-view-results-jmeter

 

7. Make the request and you can see that the ${var1} was replaced with www.example.com in the HTTP Request
5-inspect-results-jmeter

 

8. Add the ${var1} also in the name of the HTTP Request sampler and you can see the request has the value of the user defined variable var1
6-use-user-defined-variable-second-time-jmeter
7-inspect-http-requests-jmeter

 

Notes:

  • suggestion: for simplicity use User Defined Variables only at the beginning of a Thread Group
  • all User Defined Variables from a test plan are processed at the beginning no matter where they are added or placed in the JMeter Test Plan
  • JMeter User Defined Variables should not be used with functions that generate different results each time they are called
  • use User Parameters for defining variables during a test run instead of User Defined Variables
  • User Defined Variables are processed in the order they are added in the test plan, from TOP to BOTTOM
  • If, in your Test Plan,you have more than one Thread Group, use different names for different values, as UDVs are shared between Thread Groups.
  • You can reference variables defined in earlier UDVs or on the Test Plan.


 





Thanks for reading. I hope you learned something interesting about React!
If you found this article useful, please share it with others. Don’t forget to subscribe to get notified for the upcoming articles.