Nightmare
A high-level browser automation library
README
Nightmare
Nightmare is a high-level browser automation library from Segment .
The goal is to expose a few simple methods that mimic user actions (like goto, typeand click), with an API that feels synchronous for each block of scripting, rather than deeply nested callbacks. It was originally designed for automating tasks across sites that don't have APIs, but is most often used for UI testing and crawling.
Under the covers it uses Electron , which is similar to PhantomJS but roughly twice as fast and more modern.
⚠️Security Warning:We've implemented many of the security recommendations outlined by Electron to try and keep you safe, but undiscovered vulnerabilities may exist in Electron that could allow a malicious website to execute code on your computer. Avoid visiting untrusted websites.
🛠Migrating to 3.x:You'll want to check out this issue before upgrading. We've worked hard to make improvements to nightmare while limiting the breaking changes and there's a good chance you won't need to do anything.
Niffy is a perceptual diffing tool built on Nightmare. It helps you detect UI changes and bugs across releases of your web app.
Daydream is a complementary chrome extension built by @stevenmiller888 that generates Nightmare scripts for you while you browse.
Many thanks to @matthewmueller and @rosshinkley for their help on Nightmare.
Examples
UI Testing Quick Start
Perceptual Diffing with Niffy & Nightmare
API
Set up an instance
Interact with the page
Extract from the page
Cookies
Proxies
Promises
Extending Nightmare
Usage
Debugging
Additional Resources
Examples
Let's search on DuckDuckGo:
- ``` js
- const Nightmare = require('nightmare')
- const nightmare = Nightmare({ show: true })
- nightmare
- .goto('https://duckduckgo.com')
- .type('#search_form_input_homepage', 'github nightmare')
- .click('#search_button_homepage')
- .wait('#r1-0 a.result__a')
- .evaluate(() => document.querySelector('#r1-0 a.result__a').href)
- .end()
- .then(console.log)
- .catch(error => {
- console.error('Search failed:', error)
- })
- ```
You can run this with:
- ``` shell
- npm install --save nightmare
- node example.js
- ```
Or, let's run some mocha tests:
- ``` js
- const Nightmare = require('nightmare')
- const chai = require('chai')
- const expect = chai.expect
- describe('test duckduckgo search results', () => {
- it('should find the nightmare github link first', function(done) {
- this.timeout('10s')
- const nightmare = Nightmare()
- nightmare
- .goto('https://duckduckgo.com')
- .type('#search_form_input_homepage', 'github nightmare')
- .click('#search_button_homepage')
- .wait('#links .result__a')
- .evaluate(() => document.querySelector('#links .result__a').href)
- .end()
- .then(link => {
- expect(link).to.equal('https://github.com/segmentio/nightmare')
- done()
- })
- })
- })
- ```
You can see examples of every function in the tests here .
To get started with UI Testing, check out this quick start guide .
To install dependencies
- ``` null
- npm install
- ```
To run the mocha tests
- ``` null
- npm test
- ```
Node versions
Nightmare is intended to be run on NodeJS 4.x or higher.
API
Nightmare(options)
Creates a new instance that can navigate around the web. The available options are documented here , along with the following nightmare-specific options.
waitTimeout (default: 30s)
Throws an exception if the .wait()didn't return truewithin the set timeframe.
- ``` js
- const nightmare = Nightmare({
- waitTimeout: 1000 // in ms
- })
- ```
gotoTimeout (default: 30s)
Throws an exception if the .goto()didn't finish loading within the set timeframe. Note that, even though gotonormally waits for all the resources on a page to load, a timeout exception is only raised if the DOM itself has not yet loaded.
- ``` js
- const nightmare = Nightmare({
- gotoTimeout: 1000 // in ms
- })
- ```
loadTimeout (default: infinite)
Forces Nightmare to move on if a page transition caused by an action (eg, .click()) didn't finish within the set timeframe. If loadTimeoutis shorter than gotoTimeout, the exceptions thrown by gotoTimeoutwill be suppressed.
- ``` js
- const nightmare = Nightmare({
- loadTimeout: 1000 // in ms
- })
- ```
executionTimeout (default: 30s)
The maximum amount of time to wait for an .evaluate()statement to complete.
- ``` js
- const nightmare = Nightmare({
- executionTimeout: 1000 // in ms
- })
- ```
paths
The default system paths that Electron knows about. Here's a list of available paths: https://github.com/atom/electron/blob/master/docs/api/app.md#appgetpathname
You can overwrite them in Nightmare by doing the following:
- ``` js
- const nightmare = Nightmare({
- paths: {
- userData: '/user/data'
- }
- })
- ```
switches
The command line switches used by the Chrome browser that are also supported by Electron. Here's a list of supported Chrome command line switches: https://github.com/atom/electron/blob/master/docs/api/chrome-command-line-switches.md
- ``` js
- const nightmare = Nightmare({
- switches: {
- 'proxy-server': '1.2.3.4:5678',
- 'ignore-certificate-errors': true
- }
- })
- ```
electronPath
The path to the prebuilt Electron binary. This is useful for testing on different versions of Electron. Note that Nightmare only supports the version on which this package depends. Use this option at your own risk.
- ``` js
- const nightmare = Nightmare({
- electronPath: require('electron')
- })
- ```
dock (OS X)
A boolean to optionally show the Electron icon in the dock (defaults to false). This is useful for testing purposes.
- ``` js
- const nightmare = Nightmare({
- dock: true
- })
- ```
openDevTools
Optionally shows the DevTools in the Electron window using true, or use an object hash containing mode: 'detach'to show in a separate window. The hash gets passed to contents.openDevTools() to be handled. This is also useful for testing purposes. Note that this option is honored only if showis set to true.
- ``` js
- const nightmare = Nightmare({
- openDevTools: {
- mode: 'detach'
- },
- show: true
- })
- ```
typeInterval (default: 100ms)
How long to wait between keystrokes when using .type().
- ``` js
- const nightmare = Nightmare({
- typeInterval: 20
- })
- ```
pollInterval (default: 250ms)
How long to wait between checks for the .wait()condition to be successful.
- ``` js
- const nightmare = Nightmare({
- pollInterval: 50 //in ms
- })
- ```
maxAuthRetries (default: 3)
Defines the number of times to retry an authentication when set up with .authenticate().
- ``` js
- const nightmare = Nightmare({
- maxAuthRetries: 3
- })
- ```
certificateSubjectName
A string to determine the client certificate selected by electron. If this options is set, the select-client-certificate event will be set to loop through the certificateList and find the first certificate that matches subjectNameon the electron Certificate Object .
- ``` js
- const nightmare = Nightmare({
- certificateSubjectName: 'tester'
- })
- ```
.engineVersions()
Gets the versions for Electron and Chromium.
.useragent(useragent)
Sets the useragentused by electron.
.authentication(user, password)
Sets the userand passwordfor accessing a web page using basic authentication. Be sure to set it before calling .goto(url).
.end()
Completes any queue operations, disconnect and close the electron process. Note that if you're using promises, .then()must be called after .end()to run the .end()task. Also note that if using an .end()callback, the .end()call is equivalent to calling .end()followed by .then(fn). Consider:
- ``` js
- nightmare
- .goto(someUrl)
- .end(() => 'some value')
- //prints "some value"
- .then(console.log)
- ```
.halt(error, done)
Clears all queued operations, kills the electron process, and passes error message or 'Nightmare Halted' to an unresolved promise. Done will be called after the process has exited.
Interact with the Page
.goto(url[, headers])
Loads the page at url. Optionally, a headershash can be supplied to set headers on the gotorequest.
When a page load is successful, gotoreturns an object with metadata about the page load, including:
url: The URL that was loaded
code: The HTTP status code (e.g. 200, 404, 500)
method: The HTTP method used (e.g. "GET", "POST")
referrer: The page that the window was displaying prior to this load or an empty string if this is the first page load.
headers: An object representing the response headers for the request as in {header1-name: header1-value, header2-name: header2-value}
If the page load fails, the error will be an object with the following properties:
message: A string describing the type of error
code: The underlying error code describing what went wrong. Note this is NOT the HTTP status code. For possible values, see https://code.google.com/p/chromium/codesearch#chromium/src/net/base/net_error_list.h
details: A string with additional details about the error. This may be null or an empty string.
url: The URL that failed to load
Note that any valid response from a server is considered “successful.” That means things like 404 “not found” errors are successful results for goto. Only things that would cause no page to appear in the browser window, such as no server responding at the given address, the server hanging up in the middle of a response, or invalid URLs, are errors.
You can also adjust how long gotowill wait before timing out by setting the gotoTimeout option on the Nightmare constructor.