Sauce Labs is pleased to announce that it is open-sourcing one of its internal projects called Foxdriver. It is a Node.js remote debugging client for Firefox that allows developer to boot a fresh Firefox instance or attach themselves to an already-running instance, e.g. while running a WebDriver test. It provides access to the Firefox Remote Debugging Protocol, which is a comprehensive interface to remote-control the browser.
As part of our internal effort to surface as much information as possible about the state of a customer’s application, we built this tool to capture, for example, network data, and other useful metrics during WebDriver tests in our cloud. It also allows certain browser emulations that would not be possible using just the WebDriver protocol.
The following script demonstrates how to listen to network events during your WebDriver test. It uses WebdriverIO as the WebDriver client. In addition to that, the script creates a specific Firefox profile that is required in order to allow 3rd party clients, like Foxdriver, to connect to the browser:
1import Foxdriver from ‘foxdriver'2import FirefoxProfile from 'firefox-profile'3import { remote } from 'webdriverio'45(async () => {6const fp = new FirefoxProfile()7fp.setPreference('devtools.debugger.remote-enabled', true)8fp.setPreference('devtools.chrome.enabled', true)9fp.setPreference('devtools.debugger.prompt-connection', false)1011const zippedProfile = await new Promise((resolve, reject) => {12fp.encoded((err, zippedProfile) => {13if (err) return reject(err)14return resolve(zippedProfile)15})16})1718const browser = remote({19desiredCapabilities: {20browserName: 'firefox',21'moz:firefoxOptions': {22args: ['--start-debugger-server', '9222'],23profile: zippedProfile24}25}26})2728// start browser29await browser.init()3031// attach to browser32const { tabs } = await Foxdriver.attach('localhost', 9222)3334// listen to network events35await tabs[0].network.startListeners()36await tabs[0].network.on('request',37({url, method}) => console.log("new request:", method, url)38)3940// open page await41browser.url('https://www.mozilla.org/en-US')4243// close session44await browser.end()45})()
Once you’ve attached yourself to a tab, you can access various so-called “Actors”. Each Actor addresses a certain domain within the browser and allows you to listen to its events or manipulate its behavior. Some of the currently supported actors are Network, Profiler, Performance and Emulation, just to name a few. These can be very useful if you want to access functionality beyond the WebDriver protocol.
Have a look at more of such examples at https://github.com/saucelabs/foxdriver.
The first release of Foxdriver already implements most of the common actors provided by the Firefox Remote Debugging Protocol. Our team is working on covering more in order to make the whole protocol accessible over a simplified API. We are looking for your feedback and appreciate any kind of contribution. We love Open Source!
The project was highly inspired by Heather Arthur’s initial project called firefox-client.
We <3 Open Source at Sauce Labs and encourage Saucers to contribute to projects like Appium, Selenium and their own initiatives.