JMeter HTTP Request Sampler
JMeter HTTP Request Sampler
The HTTP Request Sampler in JMeter allows us to send HTTP or HTTPS requests to a web server. If we need to send multiple requests to the same server, we can use an HTTP Request Defaults Configuration Element. This avoids entering the same details for each request manually.
Add a Thread Group
A Thread Group controls the execution of test components. We configure it to define the number of users, ramp-up time, and loop count.
Steps to Add a Thread Group:
- Right-click on the Test Plan
- Navigate to: Add → Threads (Users) → Thread Group
- Configure Thread Group Properties:
- Set the Number of Threads (Users) – Defines how many virtual users will send requests.
- Set the Ramp-Up Period (in seconds) – Determines how quickly users are added to the test.
- Set the Loop Count – Defines how many times the test will repeat.
Once the Thread Group is set up, we can proceed to add an HTTP Request Sampler to send requests to the web server.
Add an HTTP Request Sampler
After adding a Thread Group, we need to include an HTTP Request Sampler to send HTTP/HTTPS requests.
Steps to Add an HTTP Request Sampler:
-
Right-click on the Thread Group
- Navigate to: Add → Sampler → HTTP Request
Configure HTTP Request Properties:
After adding the HTTP Request Sampler, define the following properties:
-
- Name: Provide a meaningful name for the request.
- Server Name or IP: Enter the target web server’s domain name or IP address. Example:
example.com
- Protocol: Choose
HTTP
orHTTPS
as needed. - Port Number: If required, specify the port (e.g.,
80
for HTTP,443
for HTTPS). - Path: Define the endpoint/path for the request (e.g.,
/api/login
). - Method: Select the HTTP method (GET, POST, PUT, DELETE, etc.).
- Parameters: (Optional) Add request parameters if needed.
- Body Data: (For POST/PUT requests) Enter the request payload.
- Content-Type: Set the request content type (e.g.,
application/json
).
Now, the HTTP Request Sampler is ready to send requests to the web server.
Add a Listener (View Results Tree)
After setting up the Thread Group and HTTP Request Sampler, we need to add a Listener to analyze the test results.
Steps to Add a Listener (View Results Tree):
Right-click on the Thread Group
-
- Navigate to: Add → Listener → View Results Tree
Purpose of View Results Tree:
-
- Displays a tree structure of all HTTP request responses.
- Helps in debugging by showing detailed request and response data.
- Shows response time, HTTP response code (e.g.,
200 OK
), and response body.
After adding the View Results Tree, we can run the test and analyze the responses to ensure the requests are working correctly.
Add an HTTP Request Defaults (Optional)
If you are sending multiple requests to the same web server, you can configure HTTP Request Defaults to avoid entering the same server details repeatedly.
Steps to Add HTTP Request Defaults:
- Right-click on the Test Plan
- Navigate to: Add → Config Element → HTTP Request Defaults
- Configure Default Values:
- Server Name or IP: Enter the target web server (e.g.,
example.com
). - Port Number: Specify the port if required (
80
for HTTP,443
for HTTPS). - Protocol: Set as
HTTP
orHTTPS
. - These settings will apply to all HTTP requests in the test unless overridden.
- Server Name or IP: Enter the target web server (e.g.,
Add an Assertion
Assertions help validate responses to check if the test passed or failed.
Steps to Add a Response Assertion:
- Right-click on the HTTP Request Sampler
- Navigate to: Add → Assertions → Response Assertion
- Configure the Assertion:
- Select “Response Text” or “Response Code” in the “Response Field to Test” section.
- In the Patterns to Test, enter expected response values like
"200"
(for successful responses). - This ensures the test fails if the response does not match the expected result.
Run the Test
- Click the Start (▶️) button in the JMeter toolbar.
- Monitor results in the View Results Tree or other Listeners.
- If errors occur, check request configurations and response details.
Analyze and Save Test Results
- Use Listeners like Summary Report or Aggregate Report for detailed performance metrics.
- Save the test plan (
.jmx
file) for future use by going to File → Save Test Plan As.
Now, your JMeter test plan is fully set up!