我想知道是否可以在Selenium / WebDriver测试用例中使用Firefox Developer Tools的功能。具体来说,我想将网站发送的HTTP请求导出为HAR文件。
我知道使用Firefox扩展并将其添加到这样的配置文件中(在Java中):
FirefoxProfile.addExtension(java.io.File extensionToInstall)
FirefoxProfile.setPreference(java.lang.String key,
java.lang.String value)
但是为此,我需要DevTools作为XPI文件,我找不到任何相关信息。
我也知道设置这样的所需功能(在Java中):
FirefoxDriver(Capabilities desiredCapabilities)
Capabilities.setCapability(java.lang.String capabilityName, java.lang.String value)
但我不知道要使用哪种功能来使用HAR文件导出功能。
我已经阅读了很多类似问题的评论和答案,这些问题归结为:
无论如何,我想知道使用Firefox开发者工具的方法。
答案 0 :(得分:1)
此解决方案使用Firefox的本机开发工具和名为HAR Export Trigger的插件。虽然是测试版,但它适用于:
这是一个简单的示例代码:
/**Test class for exporting HTTP requests of a web page to HAR files.*/
public class HarExportTest {
/**Absolute path needed because FF profile for Selenium is temporary.*/
private final static String HARDIR = "/home/test/HAR_Demo/target";
public static void main(String[] args) {
WebDriver driver = null;
try {
// Init
driver = new FirefoxDriver(buildNetmonitorProfile());
final File harDir = new File(HARDIR);
final int numFiles = harDir.listFiles().length;
System.out.println(harDir.getPath() + ": " + numFiles);
// Load test page
driver.get("https://ixquick.com");
// Wait for new file
for (int c=0; c<180; c++) {
if (harDir.listFiles().length > numFiles) {
System.out.println("added");
break;
}
Thread.sleep(1000L);
}
System.out.println(harDir.getPath() + ": " + harDir.listFiles().length);
}
catch (Exception exc) {
System.err.println(exc);
}
if (driver != null) {
driver.quit();
}
}
private static FirefoxProfile buildNetmonitorProfile() throws IOException {
FirefoxProfile profile = new FirefoxProfile();
// Load extensions
File harExport = new File("harexporttrigger-0.5.0-beta.7.xpi"); //adjust path as needed
profile.addExtension(harExport);
// Enable the automation without having a new HAR file created for every loaded page.
profile.setPreference("extensions.netmonitor.har.enableAutomation", true);
// Set to a token that is consequently passed into all HAR API calls to verify the user.
profile.setPreference("extensions.netmonitor.har.contentAPIToken", "test");
// Set if you want to have the HAR object available without the developer toolbox being open.
profile.setPreference("extensions.netmonitor.har.autoConnect", true);
// Enable netmonitor
profile.setPreference("devtools.netmonitor.enabled", true);
// If set to true the final HAR file is zipped. This might represents great disk-space optimization especially if HTTP response bodies are included.
profile.setPreference("devtools.netmonitor.har.compress", false);
// Default name of the target HAR file. The default file name supports formatters
profile.setPreference("devtools.netmonitor.har.defaultFileName", "Autoexport_%y%m%d_%H%M%S");
// Default log directory for generate HAR files. If empty all automatically generated HAR files are stored in <FF-profile>/har/logs
profile.setPreference("devtools.netmonitor.har.defaultLogDir", HARDIR);
// If true, a new HAR file is created for every loaded page automatically.
profile.setPreference("devtools.netmonitor.har.enableAutoExportToFile", true);
// The result HAR file is created even if there are no HTTP requests.
profile.setPreference("devtools.netmonitor.har.forceExport", true);
// If set to true, HTTP response bodies are also included in the HAR file (can produce significantly bigger amount of data).
profile.setPreference("devtools.netmonitor.har.includeResponseBodies", false);
// If set to true the export format is HARP (support for JSONP syntax that is easily transferable cross domains)
profile.setPreference("devtools.netmonitor.har.jsonp", false);
// Default name of JSONP callback (used for HARP format)
profile.setPreference("devtools.netmonitor.har.jsonpCallback", false);
// Amount of time [ms] the auto-exporter should wait after the last finished request before exporting the HAR file.
profile.setPreference("devtools.netmonitor.har.pageLoadedTimeout", "2500");
return profile;
}
}
输出是:
/ home / test / HAR_Demo / target:9
加入
/ home / test / HAR_Demo / target:10