firefox ver 42+还有一个替代方案,有一个名为Firefox HarExport的插件。
File harExportApi = new File(System.getProperty("user.dir")
+ "/src/main/resources/firebug/harexporttrigger-0.5.0-beta.7.xpi");
netExportProfile.addExtension(harExportApi);
netExportProfile.setPreference("extensions.netmonitor.har.enableAutomation", true);
netExportProfile.setPreference("extensions.netmonitor.har.contentAPIToken", "test");
netExportProfile.setPreference("extensions.netmonitor.har.autoConnect", true);
cap.setCapability(FirefoxDriver.PROFILE, netExportProfile);
运行以下脚本将向我们提供所有请求响应
String getHarLogScript = "var options = {\n" +
" token: \"test\",\n" +
" getData: true,\n" +
" title: \"my custom title\",\n" +
" jsonp: false,\n" +
" };\n" +
"\n" +
" HAR.triggerExport(options).then(result => {\n" +
" var har = JSON.parse(result.data);\n" +
"\n" +
" // Use performance.timing to provide onContentLoad\n" +
" +
" +
" var t = performance.timing;\n" +
" var pageTimings = har.log.pages[0].pageTimings;\n" +
" pageTimings.onContentLoad = t.domContentLoadedEventStart - t.navigationStart;\n" +
" pageTimings.onLoad = t.loadEventStart - t.navigationStart;\n" +
"\n" +
" window.HarEntries=har.log.entries\n" +
"\n" +
" console.log(\"HAR log (\" + result.data.length + \") \", har.log);\n" +
" }, err => {\n" +
" console.error(err);\n" +
" });"
LOG.info("Loading HAR log entries object into browser HarEntries object");
SeleniumUtils.executeScript(driver, getHarLogScript);
harEntries = ((List<Object>) SeleniumUtils.executeScript(driver, "return window.HarEntries"));