WebDriver:在 Selenium 测试用例中使用 Firefox 开发者工具
WebDriver: Use Firefox Developer Tools in Selenium test case
我想知道是否可以在 Selenium/WebDriver 测试用例中使用 Firefox 开发人员工具的功能。具体来说,我想将网站发送的 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 文件导出功能。
我阅读了很多类似问题的评论和答案,归结为:
- 使用其他测试工具(Sikuli)
- 使用其他浏览器(Chrome)
- 使用另一个插件(Firebug+Netexport,不错example)
无论如何,我想知道使用 Firefox Developer Tools 的方法。
此解决方案使用 Firefox 的本机开发工具和名为 HAR Export Trigger 的插件。虽然是测试版,但它适用于:
- Firefox: '43.0.4'
- 硒:“2.48.2”
- os.name: 'Linux'
- os.version: '3.13.0-74-通用'
- java.version: '1.7.0_91'
这是一个简单的示例代码:
/**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
added
/home/test/HAR_Demo/target: 10
我想知道是否可以在 Selenium/WebDriver 测试用例中使用 Firefox 开发人员工具的功能。具体来说,我想将网站发送的 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 文件导出功能。
我阅读了很多类似问题的评论和答案,归结为:
- 使用其他测试工具(Sikuli)
- 使用其他浏览器(Chrome)
- 使用另一个插件(Firebug+Netexport,不错example)
无论如何,我想知道使用 Firefox Developer Tools 的方法。
此解决方案使用 Firefox 的本机开发工具和名为 HAR Export Trigger 的插件。虽然是测试版,但它适用于:
- Firefox: '43.0.4'
- 硒:“2.48.2”
- os.name: 'Linux'
- os.version: '3.13.0-74-通用'
- java.version: '1.7.0_91'
这是一个简单的示例代码:
/**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
added
/home/test/HAR_Demo/target: 10