Modify JMeter Script using AI

Modify JMeter Script using AI

In performance testing with Apache JMeter, you might often need to modify an existing test plan due to changes in the application’s flow or functionality, and these modifications can be done using AI. Instead of re-recording the test using the JMeter HTTP(S) Test Script Recorder, there is a faster and cleaner method: using a browser-exported HAR file. This method lets you convert real user interactions into JMeter test scripts with minimal effort.

This guide explains how to convert a HAR file into a JMeter script compatible with JMeter version 5.6.3 using ChatGPT. As an example, we will refer to a sample HAR file named Select_Fish_Page.har.

What is a HAR File?

A HAR (HTTP Archive) file is a JSON-formatted archive file that stores a record of network requests and responses captured by the browser’s developer tools. It includes all necessary data such as request URLs, HTTP methods, headers, response codes, timing, and request/response bodies.

Why use HAR instead of Re-recording?

  • No need to set up proxy recording in JMeter
  • Captures real browser behavior and sequence
  • Ensures you only capture what you interact with
  • Easy to update when front-end changes occur
  • Saves time for iterative testing

Step-by-Step Instructions

Step 1: Browse and Navigate Through the UI

  1. Open your browser (Google Chrome, Firefox, or Edge)
  2. Open Developer Tools (press F12 or right-click on the page and choose “Inspect”)
  3. Navigate to the Network tab
  4. Enable the “Preserve log” option
  5. Perform the series of actions you want to capture for testing

For example, if you are testing a product selection feature, navigate through the product catalog, select an item, and proceed to the product detail page.

Step 2: Export the HAR File

  1. After completing your navigation, return to the Network tab
  2. Right-click anywhere in the request list
  3. Choose “Save all as HAR with content”
  4. Save the file as Example.har or any other name

This file now contains a full record of all HTTP requests that occurred during your browsing session.

Step 3: Convert HAR to JMeter Script Using ChatGPT

You can now upload the HAR file to ChatGPT and ask it to generate a JMeter 5.6.3 (or your specific JMeter version) compatible script. To get an optimal result, include the following instructions or prompt in your message:

1. Convert HAR file to JMeter 5.6.3 script (JMX).
2. All the requests should come under a transaction name - Click <Link Name> 
3. Add request body for all GET and POST requests (if available).
4. Add header manager to all the requests.
5. Use a variable ${BASE_URL} in the Server Name or IP field. //Host URL variable name
6. Use a variable ${protocolHost} in the Protocol [http]. //Host protocol variable name
7. Add response code assertion to all requests.

Upload the Example.har file to ChatGPT along with these prompts. ChatGPT will process the file and return a downloadable JMX script.

Step 4: Open the JMX Script in Apache JMeter

  1. Launch Apache JMeter version 5.6.3 (or the JMeter version that you have)
  2. Open the generated JMX file (File > Open)
  3. Inside the Thread Group, you will find a Transaction Controller named “Click <Link Name>”
  4. All HTTP Requests from the HAR file will appear inside this controller

Each request will include the following:

  • The request method (GET or POST)
  • The request path (URL path)
  • The request body (if applicable)
  • A Header Manager with headers from the HAR
  • A Response Assertion to validate HTTP response codes

Step 5: Customize and Extend the Test Plan

Once the base script is imported into JMeter, you can modify it further:

  • Replace the existing transaction with the newly generated transaction in the original script.
  • Add correlation (extractors) to capture dynamic values like session IDs or tokens (if any)
  • Use the CSV Data Set Config variable name in the newly generated transaction to simulate user inputs

Example Based on Example.har

In the case of the Example.har file, the HAR was generated from a user interaction. The converted JMeter script included:

  • A Transaction Controller grouping all navigation steps
  • Multiple HTTP Request samplers with properly set paths and methods
  • Parameterization using ${BASE_URL}
  • Automatic inclusion of all headers and response bodies where relevant
  • HTTP response code assertions to validate successful navigation

Reusing This Approach

When the UI or API changes:

  1. Generate a new HAR by repeating the updated navigation in your browser
  2. Upload the HAR to ChatGPT with the same prompt
  3. Download the updated JMX script
  4. Replace or merge it with your existing JMeter plan

This approach eliminates the need for re-recording, saving setup time and minimizing errors.

What You Need

  • A modern browser with developer tools (Chrome, Firefox, or Edge)
  • ChatGPT (preferably with file upload support)
  • Apache JMeter 5.6.3 or the version you have (you need to specify to ChatGPT)

Conclusion

Using a HAR file is a quick and efficient way to update or generate JMeter test scripts. It ensures accuracy by capturing real browser traffic, eliminating the need for manual recording. With ChatGPT’s ability to convert HAR to JMX, your performance testing workflow becomes faster and more reliable.


You may be interested: