Selenium 使用 HAR Export xpi 读取网络数据

Selenium read network data using HAR Export xpi

我正在使用 HAR Export XPI 获取使用 selenium 遍历的页面的网络流量。我正在将 XPI 添加到 ffv46(因为无法将 XPI 添加到最新的 ff 浏览器)。我使用了下面提到的配置文件设置 -

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);

我正在注入以下 javascript 来获取 HAR 文件。

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();
}

但是没有生成 HAR 文件。有什么我想念的吗? 此外,如果我尝试在 FF 控制台中键入 HAR,我会收到未定义的消息,这会导致函数失败。

是否还有其他我遗漏的设置。

提前致谢!!!!

因为我没有运气使用其他建议的解决方案解决这个问题(AFAIK browsermob proxy 和 XPI 已经有一段时间没有更新了),我创建了一个小的 npm 模块来将 selenium 测试捕获为 HAR 文件 - link

我使用了Chrome Dev Tools Protocol来捕获Fetch事件,你可以看到我的逻辑here

在这里添加我关于如何convert selenium tests to API tests的完整文章。