Nightmare is a high level wrapper for PhantomJS that lets you automate browser tasks.
The goal is to expose just a few simple methods, and have an API that feels synchronous for each block of scripting, rather than deeply nested callbacks. It's designed for automating tasks across sites that don't have APIs.
Let's search on Yahoo:
var Nightmare = require('nightmare');
new Nightmare()
.goto('http://yahoo.com')
.type('input[title="Search"]', 'github nightmare')
.click('.searchsubmit')
.run(function (err, nightmare) {
if (err) return console.log(err);
console.log('Done!');
});
Or, let's extract the entirety of Kayak's home page after everything has rendered:
var Nightmare = require('nightmare');
new Nightmare()
.goto('http://kayak.com')
.evaluate(function (page) {
return document.documentElement.innerHTML;
}, function (res) {
console.log(res);
})
.run();
Or, here's how you might automate a nicely abstracted login + task on Swiftly:
var Nightmare = require('nightmare');
var Swiftly = require('nightmare-swiftly');
new Nightmare()
.use(Swiftly.login(email, password))
.use(Swiftly.task(instructions, uploads, path))
.run(function(err, nightmare){
if (err) return fn(err);
fn();
});
And here's the nightmare-swiftly
plugin.
Create a new instance that can navigate around the web.
The available options are:
timeout
: how long to wait for page loads, default5000ms
interval
: how frequently to poll for page load state, default50ms
port
: port to mount the phantomjs instance to, default12301
Load the page at url
.
Go back to the previous page.
Refresh the current page.
Get the url of the current page, the signature of the callback is cb(url)
.
Clicks the selector
element once.
Enters the text
provided into the selector
element.
Specify the path
to upload into a file input selector
element.
Invokes fn
on the page with args
. On completion it passes the return value of fn
as to cb(res)
. Useful for extracting information from the page.
Wait until a page finishes loading, typically after a .click()
.
Wait for ms
milliseconds e.g. .wait(5000)
Wait until the element selector
is present e.g. .wait('#pay-button')
Wait until the fn
evaluated on the page returns value
. Optionally, refresh the page every delay
milliseconds, and only check after each refresh.
Saves a screenshot of the current page to the specified path
. Useful for debugging.
Set the useragent
used by PhantomJS.
Set the width
and height
of the viewport, useful for screenshotting. Weirdly, you have to set the viewport before calling .goto()
.
Useful for using repeated code blocks, see the example with Swiftly login and task creation in the docs above.
Executes the queue of functions, and calls your cb
when the script hits an error or completes the queue. The callback signature is cb(err, nightmare)
.
Here's a list of plugins, pull request to add your own to the list :)
You'll need to include the nightmare module, and already have phantomjs itself installed:
$ sudo brew update && brew install phantomjs
$ npm install --save nightmare
WWWWWW||WWWWWW
W W W||W W W
||
( OO )__________
/ | \
/o o| MIT \
\___/||_||__||_|| *
|| || || ||
_||_|| _||_||
(__|__|(__|__|
Copyright (c) 2014 Segment.io Inc. friends@segment.io
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.