Reputation: 4820
I wonder if it is possible to use functionality of Firefox Developer Tools from within a Selenium/WebDriver test case. Specifically, I want to export HTTP requests sent by a web site as HAR file.
I am aware of using a Firefox extension and adding it to a profile like this (in Java):
FirefoxProfile.addExtension(java.io.File extensionToInstall)
FirefoxProfile.setPreference(java.lang.String key,
java.lang.String value)
But for this I would need DevTools as XPI file and I couldn't find any info on that.
I am also aware of setting desired capabilities like this (in Java):
FirefoxDriver(Capabilities desiredCapabilities)
Capabilities.setCapability(java.lang.String capabilityName, java.lang.String value)
But I don't know which capabilities to set in order to use the HAR file export functionality.
I have read a lot of comments and answers to similar questions which boiled down to:
Anyway, I would like to know a way to do that using Firefox Developer Tools.
Upvotes: 1
Views: 6354
Reputation: 4820
This solution uses Firefox's native devtools and an addon called HAR Export Trigger. Although a beta, it works with:
This is a simple example code:
/**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;
}
}
Output is:
/home/test/HAR_Demo/target: 9
added
/home/test/HAR_Demo/target: 10
Upvotes: 2