Skip to content

Latest commit

 

History

History
439 lines (323 loc) · 16.1 KB

api.md

File metadata and controls

439 lines (323 loc) · 16.1 KB

Classes

TabEventEmitter

Objects

Chrome : object

Constants

CHROME_BIN : string

(Environment variable) location of Chrome executable

CHROME_PORT : number

(Environment variable) port to use

Functions

__chromate()

Function loaded in target to communicate back to runner.

Tab ⇐ EventEmitter

Kind: global class
Extends: EventEmitter

new Tab([options])

Param Type Description
[options] object see settings

tab.open(targetUrl) ⇒ Promise.<Tab>

Open a new tab at url.

See also: events fired by CDP.

Note that tab.client is the CDP client object.

Target may fire any number of custom events via console.debug({event, data}).

Kind: instance method of Tab
Returns: Promise.<Tab> - Resolved as soon as the page loads. If options.waitForDone is true, waits for 'done' event from the target page before resolving. The data from the 'done' event is available as tab.result.
Emits: see {@link Tab#event:events}

Param Type Default Description
targetUrl string "&quot;about:blank&quot;" url to load in tab

tab.close() ⇒ Promise

Close a tab opened by open()

Kind: instance method of Tab

tab.execute(func, ...args, options) ⇒ Promise.<result>

Execute a (named) function in target and get the result.

The function should return simple text values or use JSON.stringify.

Pass options.awaitPromise if the function returns a Promise.

Kind: instance method of Tab
Returns: Promise.<result> - Promise gets return value of function. Objects (including Arrays) should be JSON.stringify'd.

Param Type Description
func string | function function name in target, or function, to execute in target.
...args any additional arguments to pass to function
options object options to pass to client.Runtime.evaluate().

Example

tab.execute('getResults').then(result => console.log )  // {a:1}

  // in target:
  function getResult() { return JSON.stringify({a:1}); }

Example

tab.execute(function(){ return document.title })
   .then(result => console.log) // -> foo bar

  // in target html:  <title>foo bar</title>

tab.evaluate(expr, options) ⇒ Promise.<result>

Evaluate an expression in target and get the result.

Kind: instance method of Tab
Returns: Promise.<result> - Promise gets return value of expression.

Param Type Description
expr string expression in target to evaluate
options object options to pass to client.Runtime.evaluate().

Example

// Objects must be evaluated using JSON.stringify:
  tab.evaluate('JSON.stringify( data )').then(result => console.log) // data object

Example

tab.evaluate('one + two').then(result => console.log) // 3

  // in target
  var one = 1;
  var two = 2;

Tab.settings

Default settings. May be overridden by passing in options.

Kind: static property of Tab
Properties

Name Type Default Description
port number 9222 port number of Chrome instance. Or set env variable CHROME_PORT.
failonerror boolean true stop processing and close tab in case of network or other errors
verbose boolean false log info and console.logs
screenshot boolean false take a screenshot (WIP!)
viewport object width:680,height:800 window width & height
waitForDone boolean false set to true to have tab wait for a 'done' event from the target. The result is returned in tab.result.
timeout number 0 tab rejects and closes after this time in msec (0 to disable)

Tab.list([options]) ⇒ Promise.<Array>

List all open tabs

Kind: static method of Tab
Returns: Promise.<Array> - of tab objects

Param Type Description
[options] object options.port of Chrome process

Tab.open(targetUrl, [options]) ⇒ Promise.<Tab>

Open a tab at target url. Short hand for new Tab(url, opt).open().

Kind: static method of Tab

Param Type Default Description
targetUrl string "&quot;about:blank&quot;" url to load in tab
[options] object see settings

Tab.close(tabId, [options]) ⇒ Promise

Close a tab with given tab Id.

Kind: static method of Tab

Param Type Description
tabId string id of tab to close
[options] object options.port of Chrome process

Tab.closeAll([options]) ⇒ Promise.<Number>

Close all tabs

Kind: static method of Tab
Returns: Promise.<Number> - no. of tabs closed

Param Type Description
[options] object {port} no.

"done"

'done' (and other custom events) as fired by the target page. Handlers get (message, tab).

Kind: event emitted by Tab

"ready"

Tab client is ready. Handlers get (tab). Note: this overrides the CDP 'ready' event.

Kind: event emitted by Tab

"load"

Page loaded. Handlers get (data, tab).

Kind: event emitted by Tab

"event"

Events fired by CDP. See CDP events.

Kind: event emitted by Tab

"console.<type>"

console.<type> was called, where <type> is one of log|info|warn|error|debug. Handlers get ({type, text}, tab)

Kind: event emitted by Tab

"console"

console.<type> was called and no type-specific handler was found.

Kind: event emitted by Tab

"exception"

An uncaught exception was thrown. Handlers get (exception, tab).

Kind: event emitted by Tab

"abort"

Target is requesting a process abort. If no handler is found, a process.exit(code) is issued. Handlers get (message, tab).

Kind: event emitted by Tab

"data"

Unhandled calls to __chromate() or console.debug(). Handlers get (message, tab).

Kind: event emitted by Tab

Chrome : object

Kind: global namespace

Chrome.settings

Default settings. May be overridden by passing options.

Kind: static property of Chrome
Properties

Name Type Default Description
debug boolean true start Chrome in remote debugging mode
port number 9222 port number of Chrome instance. Or set env variable CHROME_PORT.
headless boolean true start Chrome in headless mode (note: non-headless mode not tested!)
disableGpu boolean true passed --disable-gpu to Chrome
execPath string override Chrome exec path, or set env variable CHROME_BIN
userDataDir string | false path to (possibly existing) dir to use for user data dir. If none given, a temporary user data dir is used and cleaned up after exit. Set to === false to use default user in your system. If path is given, the directory isn't removed after exit. The used value can be obtained as the userDataDir property of the resolved child process of start().
chromeFlags Array.<string> array of additional flags to pass to Chrome, e.g. ['--foo']
canary boolean false use Chrome Canary (must be installed on your system)
retry number 3 no. of times to retry to see if Chrome is ready
retryInterval number 100 msecs between retries (incl. first attempt)
verbose boolean false outputs additional logs

Chrome.flags : Array.<String>

Default set of flags passed to Chrome.

Source: https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-cli/chrome-launcher.ts#L64

Kind: static property of Chrome

Chrome.start([options]) ⇒ Promise.<ChildProcess>

Start a Chrome process and wait until it's ready.

Kind: static method of Chrome
Returns: Promise.<ChildProcess> - In addition to the usual child process properties, child.userDataDir contains the temporary user data dir used (unless one was specified).

Param Type Description
[options] object see settings

Chrome.ready([options]) ⇒ Promise

Is the process ready? Attempts to connect (with retry) to process.

Kind: static method of Chrome
Returns: Promise - resolves or rejects if process is not ready

Param Type Description
[options] object see settings for port, retry, and retryInterval

Chrome.kill(job)

Kill process(es)

Kind: static method of Chrome

Param Type Description
job ChildProcess | Array.<number> spawned process (resolve value of start) or (array of) process ids

Chrome.killall() ⇒ Promise.<Number>

Kill all (headless) Chrome processes

Kind: static method of Chrome
Returns: Promise.<Number> - no. of processes killed

Chrome.list([all]) ⇒ Promise.<Array>

List all (headless) Chrome processes (doesn't list Chrome's child processes)

Kind: static method of Chrome
Returns: Promise.<Array> - list of processes

Param Type Description
[all] boolean if given, list all processes (including child)

Chrome.version([options]) ⇒ Promise.<VersionInfo>

Get Chrome version info

Kind: static method of Chrome

Param Type Description
[options] object options.port of Chrome process

Example

Chrome.version().then(res => console.log)
 // ->
{ Browser: 'HeadlessChrome/60.0.3099.0',
  'Protocol-Version': '1.2',
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/60.0.3099.0 Safari/537.36',
  'V8-Version': '6.0.204',
  'WebKit-Version': '537.36 (@c3445e93b940e12b2e2275e9a985880a58aaa4b0)' }

Chrome.getExecPath([options]) ⇒ string

Get available Chrome path, checking for existence.

Kind: static method of Chrome

Param Type Description
[options] object specify options.canary to prefer Chrome Canary. Otherwise first checks regular Chrome.

CHROME_BIN : string

(Environment variable) location of Chrome executable

Kind: global constant

CHROME_PORT : number

(Environment variable) port to use

Kind: global constant

__chromate()

Function loaded in target to communicate back to runner.

Kind: global function
Example

// in target
  if (window.__chromate) __chromate({event: 'done', data: {pass: 10, fail: 1}});