BrowserMob records traffic that actually passes through its listener. Starting the proxy beside a Selenium browser does not put that browser on the route.

For BrowserMob's documented 2.1.5 embedded API, an existing Java Selenium setup can use this order:

BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
try {
    ChromeOptions options = new ChromeOptions();
    options.setProxy(ClientUtil.createSeleniumProxy(proxy));
    WebDriver driver = new ChromeDriver(options);
    try {
        proxy.newHar("example-page");
        driver.get("http://www.example.com/");
        Har capture = proxy.getHar();
        capture.writeTo(new java.io.File("page.har"));
    } finally {
        driver.quit();
    }
} finally {
    proxy.stop();
}

Port 0 asks for an available port. ClientUtil builds the Selenium proxy setting from that listener. The capture starts before navigation and is retrieved while the proxy is still available.

The fragment assumes the project's BrowserMob, HAR and Selenium types are already available. Detailed request or response body capture is an additional CaptureType choice. HTTPS interception also needs its intended certificate-trust arrangement.

For a remote Grid browser, advertise an address reachable from that browser's machine. A loopback address on the test runner identifies another machine's loopback there.

This captures the configured browser HTTP route. It does not establish an operating-system rule blocking every other outgoing connection.

Related articles