Giter VIP home page Giter VIP logo

nightmare's Introduction

Build Status Nightmare

Nightmare is a high-level browser automation library.

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.

Under the covers it uses Electron, which is similar to PhantomJS but faster and more modern.

Daydream is a complementary chrome extension built by @stevenmiller888 that generates Nightmare scripts for you while you browse.

Many thanks to @matthewmueller for his help on Nightmare.

Examples

Let's search on Yahoo:

var Nightmare = require('nightmare');
yield Nightmare()
  .goto('http://yahoo.com')
  .type('input[title="Search"]', 'github nightmare')
  .click('.searchsubmit');

Or, let's run some mocha tests:

var Nightmare = require('nightmare');
var expect = require('chai').expect; // jshint ignore:line

describe('test yahoo search results', function() {
  it('should find the nightmare github link first', function*() {
    var nightmare = Nightmare()
    var breadcrumb = yield nightmare
      .goto('http://yahoo.com')
      .type('input[title="Search"]', 'github nightmare')
      .click('.searchsubmit')
      .wait('.url.breadcrumb')
      .evaluate(function () {
        return document.querySelector('.url.breadcrumb').innerText;
      });
    expect(breadcrumb).to.equal('github.com');
  });
});

You can see examples of every function in the tests here.

Please note that the examples are using the mocha-generators package for Mocha, which enables the support for generators.

API

Nightmare(options)

Create a new instance that can navigate around the web. The available options are documented here.

.useragent(useragent)

Set the useragent used by electron.

.end()

Complete any queue operations, disconnect and close the electron process.

Interact with the Page

.goto(url)

Load the page at url.

.back()

Go back to the previous page.

.forward()

Go forward to the next page.

.refresh()

Refresh the current page.

.click(selector)

Clicks the selector element once.

.type(selector, text)

Enters the text provided into the selector element.

.check(selector)

Toggles the selector checkbox element.

.select(selector, option)

Changes the selector dropdown element to the option with attribute [value=option]

.scrollTo(top, left)

Scrolls the page to desired position. top and left are always relative to the top left corner of the document.

.inject(type, file)

Inject a local file onto the current page. The file type must be either js or css.

.evaluate(fn, arg1, arg2,...)

Invokes fn on the page with arg1, arg2,.... All the args are optional. On completion it returns the return value of fn. Useful for extracting information from the page. Here's an example:

var selector = 'h1';
var text = yield nightmare
  .evaluate(function (selector) {
    // now we're executing inside the browser scope.
    return document.querySelector(selector).innerText;
   }, selector); // <-- that's how you pass parameters from Node scope to browser scope

.wait(ms)

Wait for ms milliseconds e.g. .wait(5000)

.wait(selector)

Wait until the element selector is present e.g. .wait('#pay-button')

.wait(fn)

Wait until the fn evaluated on the page returns true.

Extract from the Page

.exists(selector)

Returns whether the selector exists or not on the page.

.visible(selector)

Returns whether the selector is visible or not

.on(event, callback)

Capture page events with the callback. You have to call .on() before calling .goto(). Supported events are documented here.

.screenshot(path[, clip])

Saves a screenshot of the current page to the specified path. Useful for debugging. The output is always a png. You can optionally provide a clip rect as documented here.

.pdf(path, options)

Saves a PDF with A4 size pages of the current page to the specified path. Options are here.

.title()

Returns the title of the current page.

.url()

Returns the url of the current page.

Usage

Installation

Nightmare is a Node.js module, so you'll need to have Node.js installed. Then you just need to npm install the module:

$ npm install --save nightmare

Execution

Nightmare is a node module that can be used in a Node.js script or module. Here's a simple script to open a web page:

var Nightmare = require('../nightmare');
var vo = require('vo');

vo(run)(function(err, result) {
  if (err) throw err;
});

function *run() {
  var nightmare = Nightmare();
  var title = yield nightmare
    .goto('http://cnn.com')
    .evaluate(function() {
      return document.title;
    });
  console.log(title);
  yield nightmare.end();
}

If you save this as cnn.js, you can run it on the command line like this:

npm install vo nightmare
node --harmony cnn.js

Debugging

There are three good ways to get more information about what's happening inside the headless browser:

  1. Use the DEBUG=* flag described below.
  2. Pass { show: true } to the nightmare constructor to have it create a visible, rendered window that you can watch what's happening.
  3. Listen for specific events.

To run the same file with debugging output, run it like this DEBUG=nightmare node --harmony cnn.js (on Windows use set DEBUG=nightmare & node cnn.js).

This will print out some additional information about what's going on:

  nightmare queueing action "goto" +0ms
  nightmare queueing action "evaluate" +4ms
  Breaking News, U.S., World, Weather, Entertainment & Video News - CNN.com

Tests

Automated tests for nightmare itself are run using Mocha and Chai, both of which will be installed via npm install. To run nightmare's tests, just run make test.

When the tests are done, you'll see something like this:

make test
  ․․․․․․․․․․․․․․․․․․
  18 passing (1m)

License (MIT)

WWWWWW||WWWWWW
 W W W||W W W
      ||
    ( OO )__________
     /  |           \
    /o o|    MIT     \
    \___/||_||__||_|| *
         || ||  || ||
        _||_|| _||_||
       (__|__|(__|__|

Copyright (c) 2015 Segment.io, Inc. [email protected]

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.

nightmare's People

Contributors

reinpk avatar matthewmueller avatar lambtron avatar dominicbarnes avatar securingsincity avatar fritx avatar imsky avatar 38elements avatar azurelogic avatar queckezz avatar renruyi avatar tejasmanohar avatar ocombe avatar sperand-io avatar xouabita avatar jefeweisen avatar h2non avatar tscanlin avatar atian25 avatar stevenschobert avatar zeevl avatar thotypous avatar pdelanauze avatar mortonfox avatar larrybattlework avatar kevva avatar paazmaya avatar evanhahn avatar danielstjules avatar af avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.