Foxdriver is a Node library which provides a high-level API to control Firefox over the Remote Debugging Protocol. Version 0.6.0 now supports adding custom profile files (i.e. cert9.db) before launching the browser
To use Foxdriver in your project, run:
$ npm i @benmalka/foxdriver
The Firefox Remote Debugging Protocol consists of multiple actors that provide different methods. The Foxdriver API allows you to launch a Firefox instance and connects to the protocol interface automatically. From there you can access the methods of all actors.
Example - opening page and get console.logs
import Foxdriver from '@benmalka/foxdriver'
(async () => {
const { browser, tab } = await Foxdriver.launch({
url: 'https://www.mozilla.org/en-US'
});
// enable actor
await tab.console.startListeners();
// wait until page is loaded
await new Promise((resolve) => setTimeout(resolve, 3000));
// receive logs and page errors
const logs = await tab.console.getCachedMessages();
console.log(logs);
// close browser
browser.close();
})()
You can also attach yourself to an already running Firefox browser. This requires to start the browser with the -start-debugger-server=<port>
cli argument and have the following settings set:
devtools.chrome.enabled: true
devtools.debugger.prompt-connection: false
devtools.debugger.remote-enabled: true
To attach yourself to the browser you then need to create a Foxdriver instance with the correct port and host and call the connect()
method:
import Foxdriver from '@benmalka/foxdriver'
(async () => {
const { browser, tab } = await Foxdriver.attach('localhost', 9222);
const preferences = await browser.preference.getAllPrefs();
// ...
})()
Attaches client to an already running instance.
host
<String>
host where Firefox instance was launchedport
<Number>
port on which the Firefox instance was launched- returns:
<Promise<Object>>
tab
<[Tab]>
list of opened tabsbrowser
<Browser>
browser instance
Attaches client to an already running instance.
options
<Object>
port
<Number>
port on which the Firefox instance should get launchedbin
<String>
path to Firefox binary (default: OS default path)args
<[String]>
list of arguments pass tofs.spawn
(default:[]
)customPrefs
<Object>
you may set your own preferences before the browser is being launched by setting key value pairs of prefs. Note that it is not possible to override the required prefs, i.e.devtools.chrome.enabled: true
devtools.debugger.prompt-connection: false
devtools.debugger.remote-enabled: true
customProfileFiles
<[String]>
A array of absolute file paths to be copied into the randomaly generated profile folder. This is very helpful when working with self-signed certificates in firefox. See more here How to use self-signed certificate in Firefox
- returns:
<Promise<Object>>
tab
<Tab>
opened tabbrowser
<Browser>
browser instance
Disconnects from the browser instance and closes browser if launched via launch()
method
Attaches to this tab
- returns:
<Promise>
fulfills once request was sent
Detaches from this tab
- returns:
<Promise>
fulfills once request was sent
Reloads current page url.
- returns:
<Promise>
fulfills once request was sent
Disable cache.
disable
<Boolean>
if true, caching is disbled- returns:
<Promise>
fulfills once request was sent
Navigates to a certain url
url
<String>
url to navigate to- returns:
<Promise>
fulfills once request was sent
Event fired on tab navigation end
callback
<Function>
to be called on event
For more information please see API docs.