node package manager

wd-runjs

wd-runjs

NPM Version Build Status Coverage Status Dependency Status

wd-runjs is a tiny command which runs JavaScript code on browsers by piping stdin via Selenium WebDriver.

Prerequisite

wd-runjs uses a Selenium Server for controlling remote browsers.

If you have already installed Docker onto your local machine, you can use docker-compose for starting a Selenium Grid Hub and Selenium nodes. See masnagam/docker-compose-collection for details.

Usage

  Usage: wd-runjs [options] <URI or path to a navigation script...>
 
  Run JavaScript code on browsers by piping stdin via Selenium WebDriver
 
  Options:
 
    -h, --help                  output usage information
    -V, --version               output the version number
    -b, --browser <browser>     Browser where the JavaScript code will be run (default: chrome)
    -c, --concurrency <number>  Number of ControlFlows to be run at the same time (default: 1)
    -l, --logging <filters>     Filters for the local logging of selenium-webdriver (default: '')
    -s, --server <uri>          URI of a Selenium Server (default: http://localhost:4444/wd/hub)

When it is succeeded to execute the JavaScript code, wd-runjs outputs a JSON array of the following format to stdout.

{
  "uri": "<uri>",
  "browser": "<browser>",
  "result": <result-json>
}

When it is failed, error is output instead of result.

Examples

Maximum depth of nested elements:

$ cat examples/max-depth.js | wd-runjs https://github.com/
 
[{"uri":"https://github.com/","browser":"chrome","result":12}]

Tag statistics:

$ cat examples/tags.js | wd-runjs https://github.com/
 
[{"uri":"https://github.com/","browser":"chrome","result":{"DD":3,"A":34,...}}]
 

By using jq, it is possible to process a result JSON as follows:

$ cat examples/tags.js | wd-runjs https://github.com/ | \
    jq '.[] | { uri, browser, countTags: [.result[]] | add }'
 
{
  "uri": "https://github.com/",
  "browser": "chrome",
  "countTags": 257
}

Use of a URI list:

$ cat uris
http://www.bbc.com/
http://edition.cnn.com/
http://abcnews.go.com/
$ echo "return 'hello';" | wd-runjs -c 3 $(cat uris)

It is recommended to specify the concurrency number less than the number of available Chrome instances.

Navigation script:

$ cat examples/tags.js | wd-runjs examples/navigation.js

The navigation script has to export navigate() function like below:

module.exports.navigate = (driver) => {
  driver.get('http://www.google.com/ncr');
  ...
};

By using the navigation script, it is possible to run a script on web pages where the user authentication is required.

Logging

By using the logging option, it is possible to output messages for debugging.

The following loggers are defined in wd-runjs.

  • wd-runjs.flow-pool
  • wd-runjs.script-runner
  • webdriver.Builder
  • webdriver.http
  • webdriver.http.Executor
  • promise

For available levels, see selenium-webdriver's document.

It is possible to specify multiple loggers and levels as follows:

$ wd-runjs -l 'wd-runjs:DEBUG,promise:FINER' https://github.com/

For outputting all log messages, specify the logging option like below:

$ wd-runjs -l ':ALL' https://github.com/

At this time, the remote logging is not supported.

License

This software is distributed under the MIT license. See LICENSE file for details.