Skip to content

Latest commit

 

History

History
160 lines (107 loc) · 4.51 KB

README.md

File metadata and controls

160 lines (107 loc) · 4.51 KB

chrome-har-capturer

Capture HAR files from a remote Chrome instance.

Usage

Start Chrome with options:

  • --remote-debugging-port=<port> to enable the Remote Debugging Protocol on the port <port>;

  • --enable-benchmarking --enable-net-benchmarking to enable the Javascript interface that allows chrome-har-capturer to flush the DNS cache and the socket pool before loading each URL.

For example:

google-chrome --remote-debugging-port=9222 --enable-benchmarking --enable-net-benchmarking

Use the bundled utility

Usage: chrome-har-capturer [options] URL...

Options:
  --host, -h      Remote Debugging Protocol host                  [default: "localhost"]
  --port, -p      Remote Debugging Protocol port                  [default: 9222]
  --output, -o    Dump to file instead of stdout
  --verbose, -v   Enable verbose output on stderr                 [boolean]
  --messages, -m  Dump raw messages instead of the generated HAR  [boolean]
  --autoclose, -c  Close Chrome automatically when finished       [boolean]

This module comes with a utility that can be used to generate a cumulative HAR file from a list of URLs.

Install globally with:

sudo npm install -g chrome-har-capturer

Load a list of URL with:

chrome-har-capturer -o out.har \
    https://github.com \
    http://reddit.com \
    http://www.reddit.com/help/faq

Write a custom application

Install locally with:

npm install chrome-har-capturer

The following snippet loads an array of URLs serially and generate a cumulative HAR file, just like the Record button in the Network Panel of Chrome Developer Tools.

var fs = require('fs');
var chc = require('chrome-har-capturer');
var c = chc.load(['https://github.com',
                  'http://reddit.com',
                  'http://www.reddit.com/help/faq']);
c.on('connect', function() {
    console.log('Connected to Chrome');
});
c.on('end', function(har) {
    fs.writeFileSync('out.har', JSON.stringify(har));
});
c.on('error', function() {
    console.error('Unable to connect to Chrome');
});

API

load(urls, [options])

Connects to a remote instance of Google Chrome using the Remote Debugging Protocol and loads a list of URLs serially. Returns an instance of the Client class.

urls is either an array or a single URL.

options is an object with the following optional properties:

  • host: Remote Debugging Protocol host. Defaults to localhost.
  • port: Remote Debugging Protocol port. Defaults to 9222.
  • chooseTab: Callback used to determine which remote tab attach to. Takes the JSON array returned by http://host:port/json containing the tab list and must return the numeric index of a tab. Defaults to a function that returns the active one (function (tabs) { return 0; }).

setVerbose([verbose])

Enable or disable verbose prints for debugging purposes.

verbose: Verbosity flag. Defaults to true.

Class: Client

Event: 'connect'

function () {}

Emitted when a connection to Chrome has been established.

Event: 'pageStart'

function (url) {}

Emitted when Chrome is about to load url.

Event: 'pageEnd'

function (url, chrome) {}

Emitted when Chrome has finished loading url.

This chrome-har-capturer fork passes the chrome-remote-interface instance so you can do something with the Remote Debug API, for example, execute Javascript remotely after the page has been loaded.

Event: 'pageError'

function (url) {}

Emitted when Chrome has failed loading url. Failed URLs will not appear in the cumulative HAR object.

Event: 'end'

function (har, messages, chrome) {}

Emitted when every given URL has been loaded. har is the cumulative HAR object and messages is the array of raw messages received through the Remote Debugging Protocol.

This chrome-har-capturer fork passes the chrome-remote-interface instance so you can do something with the Remote API when the execution has finished.

Event: 'error'

function () {}

Emitted when http://host:port/json can't be reached or if there are unexpected behaviors with Chrome.

Resources