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.
Are Your APIs as Reliable as You Think?
Don’t let hidden issues disrupt your service. With LoadFocus’s advanced API Monitoring, catch problems before they impact your users. Ensure flawless performance and avoid costly outages—monitor, test, and optimize your APIs effortlessly.
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
Think your website can handle a traffic spike?
Fair enough, but why leave it to chance? Uncover your website’s true limits with LoadFocus’s cloud-based Load Testing for Web Apps, Websites, and APIs. Avoid the risk of costly downtimes and missed opportunities—find out before your users do!
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
LoadFocus is an all-in-one Cloud Testing Platform for Websites and APIs for Load Testing, Apache JMeter Load Testing, Page Speed Monitoring and API Monitoring!
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.