Giter Site home page Giter Site logo

async-chainable-nighmare's Introduction

async-chainable-nightmare

Plugin for async-chainable that wraps Nightmare

This plugin patches many of the somewhat odd behaviours of the Nightmare module including:

  • All async calls that need to be wrapped in Promises (e.g. resolve, type etc.) are done so automatically
  • Some inconsiquential error messages that would otherwise abort the sequence chain are ignoerd (e.g. typing into an input box that doesn't raise the blur event)
var asyncChainable = require('async-chainable');
var asyncChainableNightmare = require('async-chainable-nightmare');

asyncChainable()
	.use(asyncChainableNightmare)
	.nightmare({show: true})

	.nightmareOn('console', function() {
		var args = Array.prototype.slice.call(arguments, 0);
		console.log('Page Console>', args);
	})

	.nightmareGoto('http://google.com')
	.then(function(cb) { console.log('Navigated'); cb() })

	.then(function(cb) { console.log('Typing into `input[name="q"]`'); cb() })
	.nightmareType('input[name="q"]', 'github async-chainable-nightmare')
	.then(function(cb) { console.log('Typed'); cb() })

	.then(function(cb) { console.log('Clicking `input[name="btnK"]'); cb() })
	.nightmareClick('input[name="btnK"]')
	.then(function(cb) { console.log('Clicked'); cb() })

	.then(function(cb) { console.log('Waiting for `.content`'); cb() })
	.nightmareWait('.content')
	.then(function(cb) { console.log('Main content area found'); cb() })

	.then(function(cb) { console.log('Evaluating `#resultStats`'); cb() })
	.nightmareEvaluate('result', function () {
		return document.querySelector('#resultStats').innerHTML;
	})
	.then(function(cb) { console.log('Evaluated'); cb() })

	.then(function(cb) { console.log('All done'); cb() })
	.end();

API

The async-chainable-nightmare API follows the specification of the main Nightmare. The Nightmare instance must be first initialized by a call to nightmare([options]) followed by subsequent calls to any nightmare prefixed function.

async-chainable-nightmare provides the following functions:

Function Description
nightmare([options]) Initialize a Nightmare instance and store it as nightmare in the async-chainable context
nightmareClick(selector) Simulate a mouse click event on a given selector
nightmareEvaluate([key], function) Execute the given function within the context of the page and, optionally, store the result in the named key within the context
nightmareGoto(url) Navigate the Nightmare instance to the given URL
nightmareOn(event, function) Bind to a Nightmare event
nightmarePdf(path, [options]) Take a PDF screenshot and save it to the given file. PDF options are specified here.
nightmarePDF(path, [options]) Alias of nightmarePdf.
nightmareScreenshot([path]) Take a screenshot. if path is provided that file will be written (must end in .png), if no path is provided a buffer is returned into the screenshot key within the context
nightmareType(selector, text) Enter the given text into the input box specified by the selector
`nightmareWait(selector timeout)`

Debugging

This module uses debug for internal debugging so setting the DEBUG=async-chainable-nightmare environment variable will show additional messages:

DEBUG=async-chainable-nightmare node myScript.js

Timeouts

You can set the default Nightmare timeouts by passing waitTimeout (default: 30s), gotoTimeout (default: 30s), loadTimeout (default: infinite) into the initial nightmare(options) call.

Should any subsequent event timeout the NIGHTMARE-TIMEOUT error response will be passed to end().

For example in the following code the module is asked to load a page and wait for an element which will never load. Since the timeout is set for 1 second Nightmare will give up after that time and immediately throw an error, skipping to the end() call.

	asyncChainable()
		.use(require('async-chainable-nightmare'))
		.nightmare({waitTimeout: 1000}) // Give up waiting after 1 second
		.nightmareGoto('http://somewhere.com')
		.nightmareWait('#someImpossibleElement')
		... Do other things ...
		.end(function(err) {
			// Err will be 'NIGHTMARE-TIMEOUT' here
		});

async-chainable-nighmare's People

Contributors

hash-bang avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

lwdgit

async-chainable-nighmare's Issues

Question

I am testing nightmare capabilites, I am just starting to evaluate your work too, because I think it is useful.

But I have a question, how I can add an custom action to Nightmare ?

the following standard action is able to log all electron events:

Nightmare.action('init-done',
  function(name, options, parent, win, renderer, done) {
    function logAllEvents(eventEmitter,logname)
    {
        var emitLog = eventEmitter.emit;
        eventEmitter.emit = function () {
           console.log(logname,arguments);
           emitLog.apply(eventEmitter, arguments);
        }
    }
    logAllEvents(parent,'parent')
    done();
  },
  function(message, done) {
    this.child.call('init-done', done);
  });

I am not able to use it through your library.

thank you very much

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.