Automate browser with Javascript and Electron using NightmareJS

Damn, this is so fucking awesome. Automating your browser using Electron with NightmareJS. Electron is being funded by Github and its competitor is NWJS funded by Intel. So we have a good platform to simulate browser and interact with it. Using PhantomJS on its core.

So if you want to start just make sure you have installed everything for the NodeJS dev environment, that includes:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y build-essential nodejs nodejs-legacy npm
sudo npm install -g nightmare

Once you have it, you can copy any of this examples and type: node file.js

Yahoo search example:

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true })

nightmare
  .goto('http://yahoo.com')
  .type('form[action*="/search"] [name=p]', 'github nightmare')
  .click('form[action*="/search"] [type=submit]')
  .wait('#main')
  .evaluate(function () {
    return document.querySelector('#main .searchCenterMiddle li a').href
  })
  .end()
  .then(function (result) {
    console.log(result)
  })
  .catch(function (error) {
    console.error('Search failed:', error);
  });

Save this as yahoo.js and run it: node yahoo.js  

You can check the whole API Documentation on its repo in github: https://github.com/segmentio/nightmare

 

 

Leave a Reply