Node.js API to control Firefox.
- uses a built-in Marionette through remote protocol
- no Selenium WebDriver is needed
- works with Headless mode
- compatible subset of Puppeteer API
At this point Foxr is more a proof of concept, work is pretty much in progress.
Run a locally installed Firefox:
/path/to/firefox -headless -marionette -safe-mode
Or a dockerized version:
docker run -it --rm --shm-size 2g -p 2828:2828 deepsweet/firefox-headless-remote:68
import foxr from 'foxr'
// const foxr = require('foxr').default
(async () => {
try {
const browser = await foxr.connect()
const page = await browser.newPage()
await page.goto('https://example.com')
await page.screenshot({ path: 'example.png' })
await browser.close()
} catch (error) {
console.error(error)
}
})()
yarn add --dev foxr
# or
npm install --save-dev foxr
Connect to the Marionette endpoint.
type TConnectOptions = {
host?: string,
port?: number,
defaultViewport?: {
width?: number,
height?: number
}
}
foxr.connect(options?: TConnectOptions): Promise<Browser>
host
โ'localhost'
by defaultport
โ2828
by defaultdefaultViewport
width
โ800
by defaultheight
โ600
by default
type TLaunchOptions = {
args?: string[],
dumpio?: boolean,
executablePath: string,
headless?: boolean
} & TConnectOptions
foxr.launch(options?: TLaunchOptions): Promise<Browser>
args
โ array of additional args,['-marionette', '-safe-mode', '-no-remote']
by defaultdumpio
โ print browser process stdout and stderr,false
by defaultexecutablePath
โ path to Firefox executable, requiredheadless
โ whether to run browser in headless mode,true
by default
browser.close(): Promise<void>
browser.disconnect(): Promise<void>
browser.newPage(): Promise<Page>
browser.pages(): Promise<Page[]>
browser.install(extensionPath: string, isTemporary: boolean): Promise<string | null>
browser.install(extensionId: string): Promise<void>
browser.getPref(pref: string, defaultBranch: boolean = false): Promise<any>
browser.setPref(pref: string, value: string | number | boolean, defaultBranch: boolean = false): Promise<void>
page.$(selector: string): Promise<ElementHandle | null>
page.$$(selector: string): Promise<ElementHandle[]>
page.$eval(selector: string, func: TSerializableFunction, ...args: TEvaluateArg[]): Promise<TJsonValue | void>
page.$$eval(selector: string, func: TSerializableFunction, ...args: TEvaluateArg[]): Promise<Array<TJsonValue | void>>
page.bringToFront(): Promise<void>
page.browser(): TBrowser
page.close(): Promise<void>
page.content(): Promise<string>
page.evaluate(target: string): Promise<TJsonValue | void>
page.evaluate(target: TSerializableFunction, ...args: TEvaluateArg[]): Promise<TJsonValue | void>
page.evaluate(target: string): Promise<JSHandle>
page.evaluate(target: TSerializableFunction, ...args: TEvaluateArg[]): Promise<JSHandle>
page.focus(selector: string): Promise<void>
page.goto(url: string): Promise<void>
page.screenshot(options?: { path?: string }): Promise<Buffer>
page.setContent(html: string): Promise<void>
page.title(): Promise<string>
page.url(): Promise<string>
page.viewport(): Promise<{ width: number, height: number }>
โฆ
elementHandle.$(selector: string): Promise<ElementHandle | null>
elementHandle.$$(selector: string): Promise<ElementHandle[]>
type TOptions = {
button?: 'left' | 'middle' | 'right',
clickCount?: number
}
elementHandle.click(options?: TOptions): Promise<void>
elementHandle.focus(): Promise<void>
elementHandle.hover(): Promise<void>
elementHandle.press(key: string): Promise<void>
Where key
is of the possible keys or a single character.
elementHandle.screenshot(options?: { path?: string }): Promise<Buffer>
elementHandle.type(text: string): Promise<void>
See my Start task runner preset for details.