Blibli Automation Journey —How to Capture Network Traffic with HAR Utility in Selenium 4

Argo triwidodo
Blibli.com Tech Blog
3 min readMar 15, 2023

If you are a web developer or tester, you might be familiar with Selenium, a popular tool for automating web browsers. Selenium 4 is the latest version of Selenium that supports Chrome DevTools Protocol (CDP), which allows you to communicate and control various aspects of the browser, such as network, security, performance, etc.

One of the features that CDP enables is intercepting network requests and responses, which can be useful for debugging, testing, or analyzing web applications. You can use CDP to capture network traffic and create HAR (HTTP Archive) files, which are JSON-formatted files that contain detailed information about each request and response.

Summary for how our har utils work

However, using CDP directly can be cumbersome and complex, as you need to write a lot of code to handle different scenarios and events. That’s why some developers have created utilities or libraries that simplify this process and provide a higher-level abstraction.

One of these utilities is selenium-har-util, which is an open-source project by us on GitHub. This utility is designed to work with Selenium 4 in Java and help you get network requests and write HAR files easily.

In this blog post, I will show you how to use selenium-har-util in your Selenium 4 project and create HAR files for your web automation tests.

Prerequisites

To use selenium-har-util, you need:

- Selenium 4 that supports CDP
- ChromeDriver
- Google Chrome or any browser that supports CDP Session (not yet tested in Geckodriver or Safari driver)
- Java 8
- Maven

Installation

To install selenium-har-util as a dependency in your Maven project, you need to add the following repository:


<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>

And then add the following dependency:


<dependency>
<groupId>com.github.blibli-badak</groupId>
<artifactId>selenium-har-util</artifactId>
<version>1.0.2</version>
</dependency>

If you are using another tool than Maven, you can check this link for more instructions: https://jitpack.io/#blibli-badak/selenium-har-util

Usage

To use selenium-har-util in your code, you need to follow these steps:

1. Create your driver instance as usual:


driver = new ChromeDriver(options);

2. Integrate with the network listener provided by selenium-har-util:


NetworkListener networkListener = new NetworkListener(driver,”har.har”);

The first argument is your driver instance, and the second argument is the name of the HAR file that will be created.

3. Start capturing your network requests:


networkListener.start();

This will enable the Network domain in CDP and add a listener to intercept requests.

4. Run your automation test as usual:


driver.get("https://example.com");
// do some actions on the web page

5. After finishing your test, create the HAR file by using this method:


driver.quit();
networkListener.createHarFile();

This will write all the captured network traffic into a JSON file named har.har (or whatever name you specified) in your project directory.

6. Open the HAR file with your favorite HAR viewer or inspect it via inspect element -> Network tab in your browser.

You should see something like this:

Conclusion

In this blog post, I showed you how to use selenium-har-util to capture network requests and create HAR files with Selenium 4 in Java. This utility can help you debug, test, or analyze web applications more easily by providing detailed information about each request and response.

I hope you found this blog post useful and interesting. If you have any question or feedback about selenium-har-util, feel free to leave a comment below, or contact us in our GitHub page.

Happy testing!

--

--