Capturing Network Traffic Using BrowserMob Proxy with Selenium Java
In modern web testing, it's not enough to simply validate the UI—you also need to verify the underlying network activity. For example, you might want to capture API calls, check for third-party requests, or inspect headers and response times. One powerful way to achieve this in Selenium tests is by using BrowserMob Proxy.
BrowserMob Proxy is a proxy server that works with Selenium WebDriver to capture HTTP/HTTPS traffic. In this blog, we'll explore how to capture network traffic using BrowserMob Proxy with Selenium in Java, and how it can enhance your end-to-end testing.
What is BrowserMob Proxy?
BrowserMob Proxy is a tool that sits between your browser and the internet. It captures and logs HTTP traffic, allowing testers to:
Monitor API calls made by the web application
Intercept and modify requests/responses
Record performance metrics
Simulate network conditions like latency or bandwidth throttling
It integrates seamlessly with Selenium WebDriver, making it ideal for automated testing scenarios.
Why Use BrowserMob Proxy with Selenium?
While Selenium is great for UI automation, it lacks built-in capabilities for network-level validation. That’s where BrowserMob Proxy comes in. Together, they enable:
Capturing AJAX requests
Validating API status codes and response times
Detecting third-party tracker or ad calls
Debugging network errors during test execution
Setting Up BrowserMob Proxy with Selenium Java
Step 1: Add Dependencies
You need the following Maven dependencies:
xml
<dependencies>
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
Step 2: Start BrowserMob Proxy and WebDriver
java
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class NetworkCaptureTest {
public static void main(String[] args) {
// Start the proxy
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
// Get Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// Configure ChromeDriver to use the proxy
ChromeOptions options = new ChromeOptions();
options.setProxy(seleniumProxy);
WebDriver driver = new ChromeDriver(options);
// Enable HAR (HTTP Archive) capture
proxy.newHar("test-har");
// Navigate to the target website
driver.get("https://example.com");
// Capture the HAR data
System.out.println(proxy.getHar().log.entries.size() + " requests captured.");
// Stop everything
driver.quit();
proxy.stop();
}
}
Analyzing the HAR Data
You can retrieve and analyze the captured HAR (HTTP Archive) data:
Loop through each entry to inspect URLs, methods, status codes
Validate response times and headers
Export HAR to a file for debugging with tools like Chrome DevTools or HAR Viewer
java
proxy.getHar().writeTo(new File("network.har"));
Use Cases in Real Testing
API Validation: Ensure expected API calls are made during certain actions.
Performance Metrics: Track time taken by network requests.
Security Testing: Monitor external domains or data leaks.
Error Handling: Log failed network responses during test failures.
Final Thoughts
Combining Selenium with BrowserMob Proxy equips your automated tests with deeper visibility into network behavior. It goes beyond UI validation, enabling more comprehensive and accurate testing. Whether you're testing single-page apps, API-driven UIs, or complex workflows, capturing network traffic is a crucial step in your QA toolkit.
Learn Selenium with Java Training
Read More: Using Robot Class in Selenium Java for Keyboard and Mouse ActionsRead More: Automating Drag and Drop Actions in Selenium WebDriver Java
Read More: Testing Responsive Web Design with Selenium Java
Visit Our IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment