The User Defined Variables element lets you define an initial set of variables, just as in the Test Plan.

Note that all the UDV elements in a test plan – no matter where they are – are processed at the start.

In your test plan, there are something which you need to use in the whole plan, such as the Server Name or IP, the Port Number, or the Protocol of HTTP Request. Imagine your test plan contains 100 HTTP Requests and has the same information as above. Then on a nice day, the dev team move the environment to another place, which new Server Name, new Port, etc. And you must edit all 100 HTTP Requests. Hmm, that’s not good, right. We need a place where can define some general information of Test Plan like that. And the best way to do this is User Define Variable.

1. User Defined Variables:

There are two places which you can see the User Defined Variables, that is:

  • Under Test Plan element
    UDVs-under-test-plan.png
  • Under Config Element (Add > Config Element > User Defined Variables)
    UDVs-under-config-element.png

If you create an independence UDVs, it will have one more column, that’s Description.

Now let see what inside this element and how to use it:

Name: It’s the name of the variable which you will use in another place. The syntax to call that variable in JMeter is:

${VARIABLE_NAME}

Value: It’s the value corresponding to the variable in the Name column. The whole ${VARIABLE_NAME} will then be replaced by the string in the “Value” column.

Example: (Download here) I’ll create Test Plan as below:

UDVs-sample-1.png

In the User Defined Variables, I created the variable

Name Value Description
SITE_HOST_NAME jmetervn.wordpress.com The domain of site under tested

Next, I will call the variable SITE_HOST_NAME in the Name field of HTTP Request.And also call this variable in the Server Name of IP of HTTP Request, using syntax ${SITE_HOST_NAME}

UDVs-sample-3.png

Now, let’s run and see how it work

UDVs-sample-4.png

Change the value of this variable to jmeter.apache.org, and run again

Name Value Description
SITE_HOST_NAME jmeter.apache.org The domain of site under tested

UDVs-sample-5.png

For more detail, please view the gif file below:

UDVs-sample-gif.gif
Click here to view original gif

So, you can see that, no matter where the place is, as long as you use the syntax ${VARIABLE_NAME}, then it will replace by the value which is predefined in User Defined Variables element.

Description: It’s just a brief of that variable so that we can review it later. For above example, it’s “The domain of site under tested”

2. Tips

2.1 All the UDV elements in a test plan – no matter where they are – are processed at the start. For simplicity, it is suggested that UDVs are placed only at the start of a Thread Group (or perhaps under the Test Plan itself).

UDVs-start-of-test-plan.png

Therefore, UDVs cannot reference variables which are defined as part of a test run, e.g in a Post-Processor

Example: In the JSR223 Post-Processor, create a variable vars.put("TEST","ABC123")

Then refer it in the UDVs like

Name Value Description
TEST_STRING ${TEST} Refer the variable TEST from Post Processor and store into TEST_STRING

–> This case will not work because UDVs are processed at the start, even the JSR223 Post-Processor might be placed before UDVs, it still does not work, please remember this.

2.2 UDVs should not be used with functions that generate different results each time they are called. Only the result of the first function call will be saved in the variable.

Example: (Download here) Using Dummy Sampler to display the variable in the UDVs as below

Name Value Description
TEST ${__Random(1,10,)} Random a number from 1 to 10 and store to TEST variable

Note: ${__Random(1,10,)} is a built-in function which will return a random number from 1 to 10.

Setup Thread Group with 3 Threads and Loop 2 times. Let see the result

UDVs-sample-6.png
It returns only one number because only first call will be saved

Then, don’t use with functions that generate different results each time they are called in UDVs.

However, UDVs can be used with functions such as __P(), for example:

Name Value Description
SITE_HOST_NAME ${__P(host,localhost)} The site is under tested

which would define the variable SITE_HOST_NAME to have the value of the JMeter property “host“, defaulting to “localhost” if not defined.

2.3 UDVs are processed in the order they appear in the Plan, from top to bottom.

If a runtime element such as a User Parameters Pre-Processor or Regular Expression Extractor defines a variable with the same name as one of the UDV variables, then this will replace the initial value, and all other test elements in the thread will see the updated value.

In another word, if the variables are duplicated, might be due to Processor, or Regular Expression Extractor, or another UDVs, then the last value will be used, and overwrite the initial value.