Skip to content

Commit

Permalink
updating readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Reinhardt committed Sep 13, 2015
1 parent 4547fb3 commit 5c9d767
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 220 deletions.
80 changes: 22 additions & 58 deletions Readme.md
Expand Up @@ -2,10 +2,12 @@
Nightmare
=========

Nightmare is a high level wrapper for [Electron](http://electron.atom.io/) (similar to phantomjs) that lets you automate browser tasks.
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](http://electron.atom.io/), which is similar to [PhantomJS](http://phantomjs.org/) but faster and more modern.

This comment has been minimized.

Copy link
@doodzik

doodzik Nov 25, 2016

@reinpk do you have a source for your claim that it is faster?

This comment has been minimized.

Copy link
@jekku

jekku Nov 25, 2016

Contributor

#484

Not sure about the source credibility tho


[Daydream](https://github.com/segmentio/daydream) is a complementary chrome extension built by [@stevenmiller888](https://github.com/stevenmiller888) that generates Nightmare scripts for you while you browse.

* [Examples](#examples)
Expand All @@ -14,7 +16,6 @@ The goal is to expose just a few simple methods, and have an API that feels sync
- [Interact with the page](#interact-with-the-page)
- [Extract from the page](#extract-from-the-page)
- [Settings](#settings)
* [Plugins](#plugins)
* [Usage](#usage)

## Examples
Expand All @@ -23,15 +24,10 @@ Let's search on Yahoo:

```js
var Nightmare = require('nightmare');
var nightmare = Nightmare()
yield nightmare
yield 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!');
});
.type('input[title="Search"]', 'github nightmare')
.click('.searchsubmit');
```

Or, let's run some mocha tests:
Expand All @@ -41,37 +37,21 @@ var Nightmare = require('nightmare');
var expect = require('chai').expect; // jshint ignore:line

describe('test yahoo search results', function() {
this.timeout(30000);

it('should find the nightmare github link first', function*() {
var nightmare = Nightmare()
yield 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;
}, function (breadcrumb) {
expect(breadcrumb).to.equal('github.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');
});
});
```

Or, here's how you might automate a nicely abstracted login + task on Swiftly:

```js
var Nightmare = require('nightmare');
var Swiftly = require('nightmare-swiftly');
var nightmare = Nightmare();
yield nightmare()
.use(Swiftly.login(email, password))
.use(Swiftly.task(instructions, uploads, path));
```

And [here's the `nightmare-swiftly` plugin](https://github.com/segmentio/nightmare-swiftly).

You can see examples of every function [in the tests here](https://github.com/segmentio/nightmare/blob/master/test/index.js).

## API
Expand Down Expand Up @@ -121,31 +101,22 @@ Toggles the `selector` checkbox element.
#### .select(selector, option)
Changes the `selector` dropdown element to the option with attribute [value=`option`]

#### .upload(selector, path)
Specify the `path` to upload into a file input `selector` element.

#### .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, cb, arg1, arg2,...)
Invokes `fn` on the page with `arg1, arg2,...`. All the `args` are optional. On completion it passes the return value of `fn` as to `cb(res)`. Useful for extracting information from the page. Here's an example:
#### .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:

```js
var p1 = 1;
var p2 = 2;

yield nightmare
.evaluate(function (param1, param2) {
// now we're executing inside the browser scope.
return param1 + param2;
}, function (result) {
// now we're inside Node scope again
console.log( result);
}, p1, p2 // <-- that's how you pass parameters from Node scope to browser scope
);
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()
Expand Down Expand Up @@ -245,13 +216,6 @@ yield nightmare
#### .headers(headers)
Set the request `headers`. You have to call this before calling `.goto()`.

## Plugins

Here's a list of plugins, pull request to add your own to the list :)

* [nightmare-swiftly](https://github.com/segmentio/nightmare-swiftly)
* [nightmare-google-oauth2](https://github.com/h2non/nightmare-google-oauth2)

## Usage
#### Installation
Nightmare is a Node.js module, so you'll need to [have Node.js installed](http://nodejs.org/). Then you just need to `npm install` the module:
Expand Down
12 changes: 0 additions & 12 deletions test/fixtures/upload/index.html

This file was deleted.

151 changes: 1 addition & 150 deletions test/index.js
Expand Up @@ -322,32 +322,7 @@ describe('Nightmare', function () {
});
});

/*describe('upload', function () {
it('should upload a file', function (done) {
new Nightmare()
.goto(fixture('upload'))
.upload('input[type=file]', 'test/files/test.css')
.click('button[type=submit]')
.wait(1000)
.evaluate(function () {
return JSON.parse(document.body.querySelector('pre').innerHTML)
}, function (files) {
files.file.originalname.should.equal('test.css');
})
.run(done);
});
it('should verify a file exists before upload', function (done) {
new Nightmare()
.goto(fixture('upload'))
.upload('#uploaded_file', 'nope.jpg')
.run(function (err) {
err.should.exist;
done();
});
});
});
/*
describe('rendering', function () {
it('should take a screenshot', function (done) {
new Nightmare()
Expand Down Expand Up @@ -629,130 +604,6 @@ describe('Nightmare', function () {
})
.run(done);
});
});
describe('multiple', function () {
it('should run fine with two instances in parallel', function (done) {
var partiallyDone = after(2, done);
new Nightmare()
.goto(fixture('simple'))
.evaluate(function () {
return document.documentElement.innerHTML;
}, function (res) {
res.length.should.be.greaterThan(1);
partiallyDone();
})
.run();
new Nightmare()
.goto(fixture('simple'))
.evaluate(function () {
return document.documentElement.innerHTML;
}, function (res) {
res.length.should.be.greaterThan(1);
partiallyDone();
})
.run();
});
it('should run fine with one instance in sequence', function (done) {
new Nightmare()
.goto(fixture('simple'))
.evaluate(function () {
return document.documentElement.innerHTML;
}, function (res) {
res.length.should.be.greaterThan(1);
})
.run(function (err, nightmare) {
nightmare
.goto(fixture('simple'))
.evaluate(function () {
return document.documentElement.innerHTML;
}, function (res) {
res.length.should.be.greaterThan(1);
}).run(function (err, nightmare) {
done();
});
});
});
});
describe('queue', function () {
it('should be ok with no callback to run', function (done) {
var nightmare = new Nightmare()
.goto(fixture('simple'))
.run();
setTimeout(done, 4000);
});
it('should execute the queue in order', function (done) {
var queue = [];
new Nightmare()
.goto(fixture('simple'))
.title(function (title) {
queue.push(1);
})
.run(function (err, nightmare) {
queue.push(2);
queue.should.eql([1, 2]);
done();
});
});
it('should be pluggable with .use()', function (done) {
function testTitle(term) {
return function (nightmare) {
nightmare
.title(function (title) {
title.should.equal(term);
});
};
}
new Nightmare()
.goto(fixture('simple'))
.use(testTitle('Simple'))
.run(done);
});
it('should execute the plugins in order', function (done) {
var queue = [];
new Nightmare()
.goto(fixture('simple'))
.evaluate(function () {
window.testQueue = [];
window.testQueue.push(1);
}, function () {
queue.push(1);
})
.use(function (nightmare) {
nightmare
.evaluate(function () {
window.testQueue.push(2);
});
queue.push(2);
})
.use(function (nightmare) {
nightmare
.evaluate(function () {
window.testQueue.push(3);
});
queue.push(3);
})
.evaluate(function () {
return window.testQueue;
}, function (testQueue) {
testQueue.should.eql([1, 2, 3]);
})
.run(function (err, nightmare) {
queue.should.eql([1, 2, 3]);
done();
});
});
});*/
});

Expand Down

0 comments on commit 5c9d767

Please sign in to comment.