How to Turn a Selenium Test into a Load Test
Intro
How do we load test our application with Selenium? We don't: a browser per user uses a lot of CPU and memory, so a few machines can't simulate many users. Load testing tools like k6, JMeter and Gatling send the HTTP requests without a browser, and simulate thousands of users.
The hard part with these tools is writing the scripts: which requests does a page send, in which order? Our Selenium tests already walk through the main journeys of our users, so let's reuse them.
A Solution
Record the requests of a Selenium test in an HTTP Archive (HAR) file, and turn the HAR file into a load test.
Older versions of this tip used a proxy server (BrowserMob Proxy) to record the requests. That project is no longer maintained. Today, WebDriver BiDi sends an event to the test for each request of the browser. The test keeps the method and the URL of each request, and writes them in a HAR file when the journey is done. It skips the requests to third parties (analytics, ads, ...), since we only want to load our own servers.
Then har-to-k6 turns the HAR file into a k6 script, and k6 runs it with as many virtual users as we want. Other tools import HAR files too (e.g., the Gatling Recorder and har2locust).
Let's continue with an example.
Example
Expected Behavior
- Chrome starts with WebDriver BiDi turned on
- The test listens to the requests of the browser
- The test goes from the home page to the second Dynamic Loading example, and waits for "Hello World!"
- The test writes the requests to the site in
journey.har, and checks the pages are in it - Chrome closes
Run the Load Test
With k6 installed, convert the HAR file and run it with 10 virtual users for 30 seconds:
npx har-to-k6 journey.har -o loadtest.js
k6 run --vus 10 --duration 30s loadtest.js
k6 prints the response times and the failed requests at the end. The script is plain JavaScript: we can add pauses
between the pages (sleep), checks on the responses, or read test data from a file.
Only load test servers that we own, or that we have permission to load. The-internet is a shared demo site, so the example above uses a few users for a short time. Point the load test at a test environment of our own.
Summary
A Selenium test knows the journeys of our users. Recording its requests gives us a starting point for a load test, without writing the requests by hand. Record the HAR file again when the pages change, so the load test stays in sync with the application.
Happy Testing!
Authors

