{"id":96,"date":"2016-05-14T13:49:48","date_gmt":"2016-05-14T13:49:48","guid":{"rendered":"http:\/\/loadfocus.com\/blog\/tech\/?p=96"},"modified":"2024-04-25T20:53:35","modified_gmt":"2024-04-25T20:53:35","slug":"how-to-use-user-defined-variables-in-apache-jmeter","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/tech\/2016\/05\/how-to-use-user-defined-variables-in-apache-jmeter","title":{"rendered":"How to use User Defined Variables in Apache JMeter"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><p class=\"lead\">In order to define your own variables and reuse them in your tests, it&#8217;s easier to <strong>use the User Defined Variables from JMeter<\/strong>.<\/p>\n<p>Here is how to create a variable and use it in an <em>HTTP Request<\/em> from your <em>JMeter Test Plan<\/em>.<\/p>\n<h3>Steps<\/h3>\n<p>1. Open JMeter (here is a more detailed post on <a href=\"https:\/\/loadfocus.com\/blog\/2020\/09\/how-to-install-jmeter-on-mac\/\">how to install JMeter<\/a>) and Add a Thread Group to your Test Plan<\/p>\n<p>2. Add a HTTP Request Sampler to your Thread Group<\/p>\n<p>3. Right-Click the Thread Group and add User Defined Variables Config Element in your JMeter test<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-98 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2016\/05\/1-add-user-defined-varibles-jmeter.png\" alt=\"\" width=\"2000\" height=\"1082\" \/><\/p>\n<p>4. Create a new variable:\u00a0<em>var1\u00a0<\/em>with value <em>www.example.com<\/em><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-99 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2016\/05\/2-add-variable-in-user-defined-variables-jmeter.png\" alt=\"\" width=\"1978\" height=\"656\" \/><\/p>\n<p>5. Go to the HTTP Request and add the variable name where you want to be replaced with its value, use <em>${var1}<\/em><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-100 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2016\/05\/3-user-variable-in-http-request-jmeter.png\" alt=\"\" width=\"1096\" height=\"356\" \/><\/p>\n<p>6. Add a View Results Tree Listener in order to easily see the results of your request.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-101 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2016\/05\/4-view-results-jmeter.png\" alt=\"\" width=\"1700\" height=\"1310\" \/><\/p>\n<p>7. Make the request and you can see that the <em>${var1}<\/em> was replaced with <em>www.example.com\u00a0<\/em>in the HTTP Request<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-102 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2016\/05\/5-inspect-results-jmeter.png\" alt=\"\" width=\"2034\" height=\"654\" \/><\/p>\n<p>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 <em>var1<\/em><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-103 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2016\/05\/6-use-user-defined-variable-second-time-jmeter.png\" alt=\"\" width=\"1096\" height=\"348\" \/><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-104 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2016\/05\/7-inspect-http-requests-jmeter.png\" alt=\"\" width=\"1774\" height=\"848\" \/><\/p>\n<h3>Notes:<\/h3>\n<ul>\n<li>suggestion: for simplicity use User Defined Variables only at the beginning of a Thread Group<\/li>\n<li>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<\/li>\n<li>JMeter User Defined Variables should not be used with functions that generate different results each time they are called<\/li>\n<li>use User Parameters for defining variables during a test run instead of User Defined Variables<\/li>\n<li>User Defined Variables are processed in the order they are added in the test plan, from TOP to BOTTOM<\/li>\n<li>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.<\/li>\n<li>You can reference variables defined in earlier UDVs or on the Test Plan.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>In order to define your own variables and reuse them in your tests, it&#8217;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&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/tech\/2016\/05\/how-to-use-user-defined-variables-in-apache-jmeter\" class=\"more-link\" title=\"Read How to use User Defined Variables in Apache JMeter\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[16,15],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-apache-jmeter-tutorials","tag-jmeter-udv","tag-use-user-defined-variables-in-jmeter"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":11,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":419,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/96\/revisions\/419"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}