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

4. Create a new variable: var1 with value www.example.com

5. Go to the HTTP Request and add the variable name where you want to be replaced with its value, use ${var1}

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

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

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

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.

How fast is your website? Free Website Speed Test