{"id":57,"date":"2016-07-18T21:44:16","date_gmt":"2016-07-18T21:44:16","guid":{"rendered":"http:\/\/wizardofbots.com\/network\/?p=57"},"modified":"2016-07-18T22:28:01","modified_gmt":"2016-07-18T22:28:01","slug":"reach-nirvana-using-nightmarejs","status":"publish","type":"post","link":"https:\/\/wizardofbots.com\/network\/reach-nirvana-using-nightmarejs\/","title":{"rendered":"Automate browser with Javascript and Electron using NightmareJS"},"content":{"rendered":"<p>Damn, this is so fucking awesome. Automating your browser using <a href=\"http:\/\/electron.atom.io\/\">Electron<\/a> with <a href=\"https:\/\/github.com\/segmentio\/nightmare\">NightmareJS<\/a>. 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 <a href=\"http:\/\/phantomjs.org\/\">PhantomJS<\/a> on its core.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/media.giphy.com\/media\/8Lx71V31BB0A\/giphy.gif\" width=\"500\" height=\"300\" \/><\/p>\n<p>So if you want to start just make sure you have installed everything for the NodeJS dev environment, that includes:<\/p>\n<pre><code class=\"language-bash\">curl -sL https:\/\/deb.nodesource.com\/setup_6.x <span class=\"token operator\">|<\/span> <span class=\"token function\">sudo<\/span> -E <span class=\"token function\">bash<\/span> -\r\nsudo apt-get install -y build-essential nodejs nodejs-legacy npm\r\nsudo npm install -g nightmare\r\n<\/code><\/pre>\n<p>Once you have it, you can copy any of this examples and type: node file.js<\/p>\n<p>Yahoo search example:<\/p>\n<pre class=\"lang:js decode:true \">var Nightmare = require('nightmare');\r\nvar nightmare = Nightmare({ show: true })\r\n\r\nnightmare\r\n  .goto('http:\/\/yahoo.com')\r\n  .type('form[action*=\"\/search\"] [name=p]', 'github nightmare')\r\n  .click('form[action*=\"\/search\"] [type=submit]')\r\n  .wait('#main')\r\n  .evaluate(function () {\r\n    return document.querySelector('#main .searchCenterMiddle li a').href\r\n  })\r\n  .end()\r\n  .then(function (result) {\r\n    console.log(result)\r\n  })\r\n  .catch(function (error) {\r\n    console.error('Search failed:', error);\r\n  });<\/pre>\n<p>Save this as yahoo.js and run it: <strong>node yahoo.js \u00a0<\/strong><\/p>\n<p>You can check the whole API Documentation on its repo in github: <a href=\"https:\/\/github.com\/segmentio\/nightmare\">https:\/\/github.com\/segmentio\/nightmare<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[34,35],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-automation","tag-nightmarejs"],"_links":{"self":[{"href":"https:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":6,"href":"https:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":63,"href":"https:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/posts\/57\/revisions\/63"}],"wp:attachment":[{"href":"https:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wizardofbots.com\/network\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}