javascriptseleniumhar

Selenium read network data using HAR Export xpi


I am using HAR Export XPI to get the network traffic for the pages traversed using selenium. I am adding the XPI to ffv46 (as cannot add XPI to latest ff browsers). I have used the below mentioned mentioned profile settings -

profile.setPreference("app.update.enabled", false);
profile.setPreference("extensions.netmonitor.har.enableAutomation", true);
profile.setPreference("extensions.netmonitor.har.contentAPIToken", "true");
profile.setPreference("extensions.netmonitor.har.autoConnect", true);
profile.setPreference("devtools.netmonitor.enabled", true);
profile.setPreference("devtools.netmonitor.har.pageLoadedTimeout", "50000");
profile.setPreference("devtools.netmonitor.har.defaultLogDir", "FOLDER_ON_SYSTEM");
profile.setPreference("devtools.netmonitor.har.enableAutoExportToFile", false);

And I am injecting the following javascript to get the HAR file.

function triggerExport() {

  var options = {
    token: "true",
    getData: true,
    fileName: "Export_%y%m%d_%H%M%S"
  };
  HAR.triggerExport(options).then(result => {
      console.log(result);
  });
};

if (typeof HAR === 'undefined') 
{
    console.log("Calling Undefined");
    addEventListener('har-api-ready', event => {
        console.log("har api ready");
        console.log(event);
        triggerExport();
    }, false);
} 
else 
{
    console.log("Calling defined");
    triggerExport();
}

But the HAR file is not getting generated. Is there something that I am missing. Also, if i try to type HAR in the FF console I am getting the message as undefined, which is causing the function to fail.

Is there any other setting that I am missing.

Thanks in advance !!!!


Solution

  • Since I had no luck solving this using other suggested solutions (AFAIK browsermob proxy and XPI weren't updated in quite a while), I created a small npm module to capture selenium tests as HAR files - link

    I used the Chrome Dev Tools Protocol to capture Fetch events, you can see my logic here

    Adding here my full writeup about how to convert selenium tests to API tests.