Skip to content

Commit 5c9d767

Browse files
author
Peter Reinhardt
committed
updating readme
1 parent 4547fb3 commit 5c9d767

File tree

3 files changed

+23
-220
lines changed

3 files changed

+23
-220
lines changed

Readme.md

+22-58
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
Nightmare
33
=========
44

5-
Nightmare is a high level wrapper for [Electron](http://electron.atom.io/) (similar to phantomjs) that lets you automate browser tasks.
5+
Nightmare is a high level browser automation library.
66

77
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.
88

9+
Under the covers it uses [Electron](http://electron.atom.io/), which is similar to [PhantomJS](http://phantomjs.org/) but faster and more modern.
10+
911
[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.
1012

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

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

2425
```js
2526
var Nightmare = require('nightmare');
26-
var nightmare = Nightmare()
27-
yield nightmare
27+
yield Nightmare()
2828
.goto('http://yahoo.com')
29-
.type('input[title="Search"]', 'github nightmare')
30-
.click('.searchsubmit')
31-
.run(function (err, nightmare) {
32-
if (err) return console.log(err);
33-
console.log('Done!');
34-
});
29+
.type('input[title="Search"]', 'github nightmare')
30+
.click('.searchsubmit');
3531
```
3632

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

4339
describe('test yahoo search results', function() {
44-
this.timeout(30000);
45-
4640
it('should find the nightmare github link first', function*() {
4741
var nightmare = Nightmare()
48-
yield nightmare
42+
var breadcrumb = yield nightmare
4943
.goto('http://yahoo.com')
50-
.type('input[title="Search"]', 'github nightmare')
51-
.click('.searchsubmit')
52-
.wait('.url.breadcrumb')
53-
.evaluate(function () {
54-
return document.querySelector('.url.breadcrumb').innerText;
55-
}, function (breadcrumb) {
56-
expect(breadcrumb).to.equal('github.com');
57-
});
44+
.type('input[title="Search"]', 'github nightmare')
45+
.click('.searchsubmit')
46+
.wait('.url.breadcrumb')
47+
.evaluate(function () {
48+
return document.querySelector('.url.breadcrumb').innerText;
49+
});
50+
expect(breadcrumb).to.equal('github.com');
5851
});
5952
});
6053
```
6154

62-
Or, here's how you might automate a nicely abstracted login + task on Swiftly:
63-
64-
```js
65-
var Nightmare = require('nightmare');
66-
var Swiftly = require('nightmare-swiftly');
67-
var nightmare = Nightmare();
68-
yield nightmare()
69-
.use(Swiftly.login(email, password))
70-
.use(Swiftly.task(instructions, uploads, path));
71-
```
72-
73-
And [here's the `nightmare-swiftly` plugin](https://github.com/segmentio/nightmare-swiftly).
74-
7555
You can see examples of every function [in the tests here](https://github.com/segmentio/nightmare/blob/master/test/index.js).
7656

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

124-
#### .upload(selector, path)
125-
Specify the `path` to upload into a file input `selector` element.
126-
127104
#### .scrollTo(top, left)
128105
Scrolls the page to desired position. `top` and `left` are always relative to the top left corner of the document.
129106

130107
#### .inject(type, file)
131108
Inject a local `file` onto the current page. The file `type` must be either 'js' or 'css'.
132109

133-
#### .evaluate(fn, cb, arg1, arg2,...)
134-
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:
110+
#### .evaluate(fn, arg1, arg2,...)
111+
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:
135112

136113
```js
137-
var p1 = 1;
138-
var p2 = 2;
139-
140-
yield nightmare
141-
.evaluate(function (param1, param2) {
142-
// now we're executing inside the browser scope.
143-
return param1 + param2;
144-
}, function (result) {
145-
// now we're inside Node scope again
146-
console.log( result);
147-
}, p1, p2 // <-- that's how you pass parameters from Node scope to browser scope
148-
);
114+
var selector = 'h1';
115+
var text = yield nightmare
116+
.evaluate(function (selector) {
117+
// now we're executing inside the browser scope.
118+
return document.querySelector(selector).innerText;
119+
}, selector); // <-- that's how you pass parameters from Node scope to browser scope
149120
```
150121

151122
#### .wait()
@@ -245,13 +216,6 @@ yield nightmare
245216
#### .headers(headers)
246217
Set the request `headers`. You have to call this before calling `.goto()`.
247218

248-
## Plugins
249-
250-
Here's a list of plugins, pull request to add your own to the list :)
251-
252-
* [nightmare-swiftly](https://github.com/segmentio/nightmare-swiftly)
253-
* [nightmare-google-oauth2](https://github.com/h2non/nightmare-google-oauth2)
254-
255219
## Usage
256220
#### Installation
257221
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:

test/fixtures/upload/index.html

-12
This file was deleted.

test/index.js

+1-150
Original file line numberDiff line numberDiff line change
@@ -322,32 +322,7 @@ describe('Nightmare', function () {
322322
});
323323
});
324324

325-
/*describe('upload', function () {
326-
it('should upload a file', function (done) {
327-
new Nightmare()
328-
.goto(fixture('upload'))
329-
.upload('input[type=file]', 'test/files/test.css')
330-
.click('button[type=submit]')
331-
.wait(1000)
332-
.evaluate(function () {
333-
return JSON.parse(document.body.querySelector('pre').innerHTML)
334-
}, function (files) {
335-
files.file.originalname.should.equal('test.css');
336-
})
337-
.run(done);
338-
});
339-
340-
it('should verify a file exists before upload', function (done) {
341-
new Nightmare()
342-
.goto(fixture('upload'))
343-
.upload('#uploaded_file', 'nope.jpg')
344-
.run(function (err) {
345-
err.should.exist;
346-
done();
347-
});
348-
});
349-
});
350-
325+
/*
351326
describe('rendering', function () {
352327
it('should take a screenshot', function (done) {
353328
new Nightmare()
@@ -629,130 +604,6 @@ describe('Nightmare', function () {
629604
})
630605
.run(done);
631606
});
632-
});
633-
634-
describe('multiple', function () {
635-
it('should run fine with two instances in parallel', function (done) {
636-
var partiallyDone = after(2, done);
637-
638-
new Nightmare()
639-
.goto(fixture('simple'))
640-
.evaluate(function () {
641-
return document.documentElement.innerHTML;
642-
}, function (res) {
643-
res.length.should.be.greaterThan(1);
644-
partiallyDone();
645-
})
646-
.run();
647-
648-
new Nightmare()
649-
.goto(fixture('simple'))
650-
.evaluate(function () {
651-
return document.documentElement.innerHTML;
652-
}, function (res) {
653-
res.length.should.be.greaterThan(1);
654-
partiallyDone();
655-
})
656-
.run();
657-
});
658-
659-
it('should run fine with one instance in sequence', function (done) {
660-
new Nightmare()
661-
.goto(fixture('simple'))
662-
.evaluate(function () {
663-
return document.documentElement.innerHTML;
664-
}, function (res) {
665-
res.length.should.be.greaterThan(1);
666-
})
667-
.run(function (err, nightmare) {
668-
669-
nightmare
670-
.goto(fixture('simple'))
671-
.evaluate(function () {
672-
return document.documentElement.innerHTML;
673-
}, function (res) {
674-
res.length.should.be.greaterThan(1);
675-
}).run(function (err, nightmare) {
676-
done();
677-
});
678-
679-
});
680-
});
681-
});
682-
683-
describe('queue', function () {
684-
it('should be ok with no callback to run', function (done) {
685-
var nightmare = new Nightmare()
686-
.goto(fixture('simple'))
687-
.run();
688-
689-
setTimeout(done, 4000);
690-
});
691-
692-
it('should execute the queue in order', function (done) {
693-
var queue = [];
694-
new Nightmare()
695-
.goto(fixture('simple'))
696-
.title(function (title) {
697-
queue.push(1);
698-
})
699-
.run(function (err, nightmare) {
700-
queue.push(2);
701-
queue.should.eql([1, 2]);
702-
done();
703-
});
704-
});
705-
706-
it('should be pluggable with .use()', function (done) {
707-
function testTitle(term) {
708-
return function (nightmare) {
709-
nightmare
710-
.title(function (title) {
711-
title.should.equal(term);
712-
});
713-
};
714-
}
715-
716-
new Nightmare()
717-
.goto(fixture('simple'))
718-
.use(testTitle('Simple'))
719-
.run(done);
720-
});
721-
722-
it('should execute the plugins in order', function (done) {
723-
var queue = [];
724-
new Nightmare()
725-
.goto(fixture('simple'))
726-
.evaluate(function () {
727-
window.testQueue = [];
728-
window.testQueue.push(1);
729-
}, function () {
730-
queue.push(1);
731-
})
732-
.use(function (nightmare) {
733-
nightmare
734-
.evaluate(function () {
735-
window.testQueue.push(2);
736-
});
737-
queue.push(2);
738-
})
739-
.use(function (nightmare) {
740-
nightmare
741-
.evaluate(function () {
742-
window.testQueue.push(3);
743-
});
744-
queue.push(3);
745-
})
746-
.evaluate(function () {
747-
return window.testQueue;
748-
}, function (testQueue) {
749-
testQueue.should.eql([1, 2, 3]);
750-
})
751-
.run(function (err, nightmare) {
752-
queue.should.eql([1, 2, 3]);
753-
done();
754-
});
755-
});
756607
});*/
757608
});
758609

0 commit comments

Comments
 (0)