From 5c9d76720c75f30732a278a2c6ada09ec3615184 Mon Sep 17 00:00:00 2001 From: Peter Reinhardt Date: Sat, 15 Aug 2015 19:27:24 -0700 Subject: [PATCH] updating readme --- Readme.md | 80 +++++------------ test/fixtures/upload/index.html | 12 --- test/index.js | 151 +------------------------------- 3 files changed, 23 insertions(+), 220 deletions(-) delete mode 100644 test/fixtures/upload/index.html diff --git a/Readme.md b/Readme.md index a2bcdc35..1da2bb11 100644 --- a/Readme.md +++ b/Readme.md @@ -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. + [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) @@ -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 @@ -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: @@ -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 @@ -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() @@ -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: diff --git a/test/fixtures/upload/index.html b/test/fixtures/upload/index.html deleted file mode 100644 index 5ea8bb63..00000000 --- a/test/fixtures/upload/index.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - Uploade - - -
- - -
- - diff --git a/test/index.js b/test/index.js index 79c5badf..3cfced46 100644 --- a/test/index.js +++ b/test/index.js @@ -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() @@ -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(); - }); - }); });*/ });