Skip to main content
blog title image

2 minute read - Selenium WebDriver FAQs Test Automation

How to Capture HTTP Messages with WebDriver

Jul 1, 2020

To capture HTTP Messages and network traffic with Selenium WebDriver we need to use an HTTP proxy, and configure the browser to direct all traffic through the proxy.

For this example I’m using an in code library as the HTTP proxy because that makes control of the test eaasier.

But this will work with any external proxy, you just have to start the proxy before you start the test.

HTTP Proxy

I’m using BrowserMob Proxy.

Which I add to my pom.xml as a dependency:


Page Using XHTTP Requests

For this example I am using:

Which, if you look in the Network traffic tab of your Browser Dev Tools, makes some XHTTP Requests (XHR) to update the page.

With a proxy we should be able to capture that traffic and extend the reach of our assertions.

BrowserMobProxy configuration

Start the proxy:

    proxyServer = new BrowserMobProxyServer();

    // capture content as a HAR (HTTP Archive)
    // to process when test is complete

And create the proxy config:

    final Proxy proxyConfig = ClientUtil.createSeleniumProxy(proxyServer);

General Proxy configuration

To configure a proxy in general we configure the proxy details:

    String proxyDetails = "";
    final Proxy proxyConfig = new Proxy().

Configuring the Driver for the Proxy

And use those to configure the driver:

    final FirefoxOptions firefoxOptions = new FirefoxOptions();
    driver = new FirefoxDriver(firefoxOptions);

For Chrome I set the additional config for insecure certs:

    final ChromeOptions options = new ChromeOptions();
    driver = new ChromeDriver(options);

Then do your ’test’ stuff

The test should run as normal, but now all traffic is going through the proxy.

Checking HTTP Traffic

For an external proxy you will then review the traffic externally in the proxy.

For BrowserMobProxy, because it is running in memory we can process the HTTP messages as part of the test.

    final Har httpMessages = proxyServer.getHar();
    for(HarEntry httpMessage : httpMessages.getLog().getEntries()){

        // check no errors on the XHR requests
        if(httpMessage.getRequest().getUrl().contains("/messageset")) {
            Assertions.assertEquals(200, httpMessage.getResponse().getStatus());

BrowserMobProxy has a lot more configuration and usage approaches, but the above is the simplest use of gathering traffic as you test.

Closing the BrowserMobProxy


Full Source

The full source for this is in my Webdriver Java FAQs project:


If you want to learn how to use Selenium WebDriver with Java then check out our online courses.