This repository has been archived by the owner on Jul 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kir Belevich
committed
Aug 9, 2018
0 parents
commit b150fd0
Showing
16 changed files
with
4,537 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
node_modules/ | ||
build/ | ||
coverage/ | ||
tmp/ | ||
.npmrc | ||
*.log | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# https://docs.travis-ci.com/user/customizing-the-build/ | ||
|
||
sudo: false | ||
language: node_js | ||
node_js: | ||
- 6 | ||
- 8 | ||
- 10 | ||
env: | ||
global: | ||
- PATH=$HOME/.yarn/bin:$PATH | ||
before_install: | ||
- curl -o- -L https://yarnpkg.com/install.sh | bash | ||
cache: | ||
yarn: true | ||
directories: | ||
- node_modules | ||
branches: | ||
only: | ||
- master | ||
matrix: | ||
fast_finish: true | ||
script: yarn start ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# The MIT License (MIT) | ||
|
||
Copyright (c) 2018–present Kir Belevich | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "foxr", | ||
"version": "0.1.0", | ||
"description": "Node.js API to control Firefox", | ||
"keywords": "firefox, headless, remote, marionette, puppeteer, selenium", | ||
"homepage": "https://github.com/deepsweet/foxr", | ||
"repository": "deepsweet/foxr", | ||
"author": "Kir Belevich <[email protected]> (https://twitter.com/deepsweet)", | ||
"license": "MIT", | ||
"files": [ | ||
"build/" | ||
], | ||
"main": "build/index.js", | ||
"types": "build/index.d.ts", | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.0.0-0", | ||
"typeon": "^0.1.1" | ||
}, | ||
"devDependencies": { | ||
"@deepsweet/eslint-config-node-ts": "^0.1.0", | ||
"@deepsweet/start-preset-node-ts-lib": "^0.2.3", | ||
"@types/node": "^10.5.6", | ||
"@types/tape": "^4.2.32", | ||
"tape": "^4.9.1", | ||
"typescript": "~2.9.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": "@deepsweet/eslint-config-node-ts", | ||
"rules": { | ||
"import/named": "off" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
# foxr | ||
|
||
[](https://www.npmjs.com/package/foxr) [](https://travis-ci.org/deepsweet/foxr) [](https://codecov.io/github/deepsweet/foxr) | ||
|
||
Node.js API to control Firefox. | ||
|
||
* uses a built-in [Marionette](https://vakila.github.io/blog/marionette-act-i-automation/) through [remote protocol](https://firefox-source-docs.mozilla.org/testing/marionette/marionette/index.html) | ||
* no [Selenium WebDriver](https://github.com/SeleniumHQ/selenium/wiki/FirefoxDriver) is needed | ||
* works with [Headless mode](https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode) | ||
* similar to compatible to [Puppeteer](https://github.com/GoogleChrome/puppeteer) API<sup>*</sup> | ||
|
||
<sup>*</sup>At this point Foxr is more a working proof of concept. Although the goal is to have a fully compatible to Puppeteer API, or at least a subset of it, the work is pretty much in progress. | ||
|
||
## Example | ||
|
||
Run a locally installed Firefox: | ||
|
||
```sh | ||
/path/to/firefox -headless -marionette -safe-mode | ||
``` | ||
|
||
Or a [dockerized version](https://github.com/deepsweet/firefox-headless-remote): | ||
|
||
```sh | ||
docker run -it --rm --shm-size 2g -p 2828:2828 deepsweet/firefox-headless-remote:61 | ||
``` | ||
|
||
```js | ||
import foxr from 'foxr' | ||
|
||
(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) { | ||
throw error | ||
} | ||
})() | ||
``` | ||
|
||
## Requirements | ||
|
||
* Node.js >= 6 | ||
* [`esm` loader](https://github.com/standard-things/esm) | ||
|
||
## Install | ||
|
||
```sh | ||
yarn add --dev foxr | ||
# or | ||
npm install --dev foxr | ||
``` | ||
|
||
## API | ||
|
||
### Foxr | ||
|
||
#### `connect` | ||
|
||
Connect to the Marionette endpoint. | ||
|
||
```ts | ||
type TOptions = { | ||
host?: string, | ||
port?: number | ||
} | ||
|
||
foxr.connect(options?: TOptions): Promise<TBrowser> | ||
``` | ||
|
||
* `host` – `'localhost'` by default | ||
* `port` – `2828` by default | ||
|
||
### Browser | ||
|
||
#### `close` | ||
|
||
```ts | ||
browser.close(): Promise<void> | ||
``` | ||
|
||
#### `disconnect` | ||
|
||
```ts | ||
browser.disconnect(): Promise<void> | ||
``` | ||
|
||
#### `newPage` | ||
|
||
```ts | ||
browser.newPage(): Promise<TPage> | ||
``` | ||
|
||
#### `pages` | ||
|
||
```ts | ||
type TPageID = number | ||
type TPages = TPageID[] | ||
|
||
browser.pages(): Promise<TPages> | ||
``` | ||
|
||
### Page | ||
|
||
#### `$` | ||
|
||
```ts | ||
page.$(selector: string): Promise<TElement> | ||
``` | ||
|
||
#### `$$` | ||
|
||
```ts | ||
page.$$(selector: string): Promise<TElement[]> | ||
``` | ||
|
||
#### `close` | ||
|
||
```ts | ||
page.close(): Promise<void> | ||
``` | ||
|
||
#### `evaluate` | ||
|
||
```ts | ||
type TAnyJson = boolean | number | string | null | TJsonArray | TJsonMap | ||
interface TJsonMap { [key: string]: TAnyJson } | ||
interface TJsonArray extends Array<TAnyJson> {} | ||
|
||
type TSerializableFunction = (...args: TAnyJson[]) => TAnyJson | ||
|
||
page.evaluate(target: TSerializableFunction | string): Promise<TAnyJson> | ||
``` | ||
|
||
#### `goto` | ||
|
||
```ts | ||
page.goto(url: string): Promise<void> | ||
``` | ||
|
||
#### `screenshot` | ||
|
||
```ts | ||
type TOptions = { | ||
path?: string | ||
} | ||
|
||
page.screenshot(options?: TOptions): Promise<Buffer> | ||
``` | ||
|
||
#### `setContent` | ||
|
||
```ts | ||
page.setContent(html: string): Promise<void> | ||
``` | ||
|
||
#### `title` | ||
|
||
```ts | ||
page.title(): Promise<string> | ||
``` | ||
|
||
### Element | ||
|
||
#### `$` | ||
|
||
```ts | ||
element.$(selector: string): Promise<TElement> | ||
``` | ||
|
||
#### `$$` | ||
|
||
```ts | ||
element.$$(selector: string): Promise<TElement[]> | ||
``` | ||
|
||
#### `screenshot` | ||
|
||
```ts | ||
type TOptions = { | ||
path?: string | ||
} | ||
|
||
element.screenshot(options?: TOptions): Promise<Buffer> | ||
``` | ||
|
||
## Development | ||
|
||
See [my Start task runner preset](https://github.com/deepsweet/_/tree/master/packages/start-preset-node-ts-lib) for details. | ||
|
||
## TODO | ||
|
||
* test using Firefox on Travis | ||
* cover more API | ||
* … | ||
* logo | ||
|
||
## References | ||
|
||
* [Marionette Python Client API](https://marionette-client.readthedocs.io/en/latest/reference.html) | ||
* [Marionette Python Client source](https://searchfox.org/mozilla-central/source/testing/marionette/client/) | ||
* [Marionette JS client source (outdated)](https://github.com/mozilla-b2g/gaia/tree/master/tests/jsmarionette/client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { TSend } from '../protocol' | ||
import { createPage } from './page' | ||
|
||
export const createBrowser = (send: TSend, onEnd: () => void) => ({ | ||
close: async (): Promise<void> => { | ||
await send('Marionette:Quit') | ||
onEnd() | ||
}, | ||
|
||
disconnect: async (): Promise<void> => { | ||
await send('WebDriver:DeleteSession') | ||
onEnd() | ||
}, | ||
|
||
newPage: async () => { | ||
await send('WebDriver:ExecuteScript', { | ||
script: 'window.open()' | ||
}) | ||
|
||
const windows = await send('WebDriver:GetWindowHandles') | ||
|
||
await send('WebDriver:SwitchToWindow', { | ||
focus: true, | ||
name: windows[windows.length - 1] | ||
}) | ||
|
||
return createPage(send) | ||
}, | ||
|
||
pages: (): Promise<number[]> => { | ||
return send('WebDriver:GetWindowHandles') | ||
} | ||
}) |
Oops, something went wrong.