Capturing network traffic using Selenium WebDriver/Firebug

I wanted to figure out a way to verify that the tracking beacon for Google Analytics was correctly being sent from the web page I was testing.
I figured out a couple of ways exist if you want to inspect http traffic going out from your web page is

  • Setup a internal proxy server that will route your http traffic whose logs you could then inspect to verify the http request you wanted to verify.
  • Use the Firebug extension along with netExport to log the traffic coming from the webpage.

Since setting up an internal proxy server was not really an option, I decided to try out the Firebug extension route and wanted to document it if it turns out to useful for somebody else trying to do the same

The first thing you will need to do is to setup the Firefox profile using the Selenium WebDriver to launch Firefox with the Firebug/Net extension enabled. You will also have to setup the profile in such a way that it dumps the http traffic log to an external file. The dumped file would be of the ‘har’ format which seems similar to json. So you can look for the traffic you want then using gson or jackson libraries.

So lets get started

Getting the Firebug/Net extension
I want to enable the extension every time my test runs, either in CI or locally. So I have committed both the extensions to source control (Our internal CI does not allow for any external communication). I then add the extensions to the Firefox profile

File firebug = new File(System.getProperty("user.dir") + "\\resources\\firebug-1.10.6.xpi");
File netExport = new File(System.getProperty("user.dir") + "\\resources\\netExport-0.9b3.xpi");

FirefoxProfile profile = new FirefoxProfile();
try {
profile.addExtension(firebug);
profile.addExtension(netExport);
} catch (IOException e) {
e.printStackTrace();
}

Update Firefox Profile
Here we will be setting other preferences to ensure that the firebug extension is enabled, is capturing HTTP traffic and will dump them in a log file


//Setting Firebug preferences
profile.setPreference("extensions.firebug.currentVersion", "2.0");
profile.setPreference("extensions.firebug.addonBarOpened", true);
profile.setPreference("extensions.firebug.console.enableSites", true);
profile.setPreference("extensions.firebug.script.enableSites", true);
profile.setPreference("extensions.firebug.net.enableSites", true);
profile.setPreference("extensions.firebug.previousPlacement", 1);
profile.setPreference("extensions.firebug.allPagesActivation", "on");
profile.setPreference("extensions.firebug.onByDefault", true);
profile.setPreference("extensions.firebug.defaultPanelName", "net");

// Setting netExport preferences
profile.setPreference("extensions.firebug.netexport.alwaysEnableAutoExport", true);
profile.setPreference("extensions.firebug.netexport.autoExportToFile", true);
profile.setPreference("extensions.firebug.netexport.Automation", true);
profile.setPreference("extensions.firebug.netexport.showPreview", false);
profile.setPreference("extensions.firebug.netexport.defaultLogDir", "C:\\workspace\\CaptureNetworkTraffic");

You can probably get away with setting just a subset of these preferences but I have not gone into doing that yet.

Launch Firefox with the desired capabilites

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
capabilities.setPlatform(org.openqa.selenium.Platform.ANY);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);

WebDriver driver = new FirefoxDriver(capabilities);

Capture traffic

try {
Thread.sleep(10000);
driver.get("http://www.google.com");
Thread.sleep(10000);
} catch(InterruptedException ie) {
ie.printStackTrace();
}
driver.quit();

This will dump traffic into a ‘.har’ extension file at the location you have specified in the ‘extensions.firebug.netexport.defaultLogDir’ firefox preference above. The file reads similar to a json file and jackson even includes harlib which can be used to read/write har files.

Complete Java Program


import java.io.File;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class Example {
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));

File firebug = new File(System.getProperty("user.dir") + "\\resources\\firebug-1.10.6.xpi");
File netExport = new File(System.getProperty("user.dir") + "\\resources\\netExport-0.9b3.xpi");

FirefoxProfile profile = new FirefoxProfile();
try {
profile.addExtension(firebug);
profile.addExtension(netExport);
} catch (IOException e) {
e.printStackTrace();
}

profile.setPreference("app.update.enabled", false);

//Setting Firebug preferences
profile.setPreference("extensions.firebug.currentVersion", "2.0");
profile.setPreference("extensions.firebug.addonBarOpened", true);
profile.setPreference("extensions.firebug.console.enableSites", true);
profile.setPreference("extensions.firebug.script.enableSites", true);
profile.setPreference("extensions.firebug.net.enableSites", true);
profile.setPreference("extensions.firebug.previousPlacement", 1);
profile.setPreference("extensions.firebug.allPagesActivation", "on");
profile.setPreference("extensions.firebug.onByDefault", true);
profile.setPreference("extensions.firebug.defaultPanelName", "net");

// Setting netExport preferences
profile.setPreference("extensions.firebug.netexport.alwaysEnableAutoExport", true);
profile.setPreference("extensions.firebug.netexport.autoExportToFile", true);
profile.setPreference("extensions.firebug.netexport.Automation", true);
profile.setPreference("extensions.firebug.netexport.showPreview", false);
profile.setPreference("extensions.firebug.netexport.defaultLogDir", "C:\\workspace\\CaptureNetworkTraffic");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
capabilities.setPlatform(org.openqa.selenium.Platform.ANY);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);

WebDriver driver = new FirefoxDriver(capabilities);
try {
Thread.sleep(10000);
driver.get("http://www.google.com");
Thread.sleep(10000);
} catch(InterruptedException ie) {
ie.printStackTrace();
}
driver.quit();
}
}

15 thoughts on “Capturing network traffic using Selenium WebDriver/Firebug

  1. Hi,
    I want to capture traffic log for requested API in my mobile app using selenium java code with Appium. Could please guide

  2. i first used selenium and firebug by first clicking on the gui controls that i wanted to remote control, then use xpath strings that it generates in c#. but of course this also works with python as I am doing again this time. however i am logging into WIFI access point to remote control the profile for automated test control

  3. Not working for me 😦 no file dumped.Nothing happened.Could you pls revisit the codes

    using

    firebug-2.0.1.xpi & netExport-0.9b7.xpi

  4. Hi,

    I am getting an issue while trying to login into the application, the requests after login are not captured by netexport in the.har file. However, I get all the req/response for the login page correctly.

    As an alternative, If I try to set the ‘netexport.alwaysEnableAutoExport’ property as true after logging in, the firefox profile is not updated for the running instance. As a result no file is generated.

    Could you please provide your suggestion on this?

    Thanks in advance!!

  5. I tried above code, it works fine with out any error but does not create a .har file under the location set in the path. how do I fix that?
    profile.setPreference(“extensions.firebug.netexport.defaultLogDir”, “C:\\workspace\\CaptureNetworkTraffic”);

  6. Hi

    I am able to run the above project but its not creating any log file in CaptureNetworkTraffic directory. Can you give me some pointers what I might be doing wrong.

  7. hi friend,

    I am trying to automate a Video stream player application something like netflix.
    What i do now:
    I have to tune to a couple of programs / channels manually and check the response in charles proxy to verify if the streaming comes from the right source and validate bitrate, response time and manifest information etc.

    What i need to do:
    I want to automate the above process. I have searched in the web where people have mentioned about getting the responses for certain browsers ( Firefox / chrome). We can get the responses from firefox using the web console i guess.

    I want to write an automation script that is generic enough, so that i can run the tests on different web browsers and compare the responses to one source i.e charles proxy.

    Below is what i am thinking of.

    cucumber (my test cases) + selenium Web driver (automate things) + Charles proxy ( to validate responses)

    Kindly help me if you have done this before. Sort of interesting and challenging. Your help is highly appreciated.

  8. For New version of Firefox 49 i am unable to generate HAR file , could some tell how to generate HAR filr for New version of Firefox

Leave a comment