Skip to content

Instantly share code, notes, and snippets.

@vingkan
Last active September 21, 2021 07:39
Show Gist options
  • Save vingkan/5f334786aef59601534feaca77efecac to your computer and use it in GitHub Desktop.
Save vingkan/5f334786aef59601534feaca77efecac to your computer and use it in GitHub Desktop.
Getting Started with Nightmare.js

Getting Started with Nightmare.js

In today's Testing Analyst workshop, we played around with Nightmare.js, a JavaScript browser automation library that is useful for testing website interfaces.

It is easier to work with Nightmare on your computer, rather than to use it in Cloud9. Here are the instructions to install and get started.

Step 1. Install Node.js

JavaScript normally runs in the browser. Node.js is a version of JS that can run on a server, in a terminal, or on some other device. It allows you to easily prepare and run Nightmare scripts.

Step 2. Install npm

Nightmare is a library that you can install through the Node Package Manager (npm). With npm, when you install Nightmare, all of its dependencies will also be installed.

Step 3. Install Nightmare

Create a folder on your computer where you want to run Nightmare scripts. Then, save the package.json file included in this Gist to that folder. This file informs npm that you will use Nightmare and its dependencies.

npm i

This command is short for "install" and it will set up Nightmare and all the dependencies for you.

Step 4. Run Your First Script

Try one of the scripts in this Gist, or your own, to make sure everything is working. Navigate to the folder in your terminal using cd and then run:

node filename.js

Step 5. Learn More about Nightmare

We covered some basic features of Nightmare today. Check out the documentation and examples to see what other features can help you.

If you want to learn more about how to "select" elements from the HTML page so that you can click on them, type into them, or extract information from them in your Nightmare scripts, check out this article.

// To Run: node name.js
const Nightmare = require('nightmare');
const nightmare = Nightmare({
show: true});
const SEARCH_TEXT = process.argv[2] || 'Illinois Tech';
nightmare
.goto('http://broken-flask.glitch.me/')
.type('textarea', SEARCH_TEXT)
.evaluate(function(){
//throw new Error('No search results');
var titles = [];
var results = document.querySelector('textarea').value;
return results;
})
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Error:', error);
});
{
"name": "Name",
"version": "0.0.0",
"description": "Description",
"devDependencies": {
"nightmare": "^2.10.0"
}
}
// To Run: node daydream.js
const Nightmare = require('nightmare');
const nightmare = Nightmare({
show: true
});
const SEARCH_TEXT = process.argv[2] || 'Illinois Tech';
nightmare
.goto('https://duckduckgo.com/')
.type('input#search_form_input_homepage', SEARCH_TEXT)
.click('#search_button_homepage')
.evaluate(function(){
//throw new Error('No search results');
var titles = [];
var results = document.querySelectorAll('.result__title');
for (var i = 0; i < results.length; i++) {
let element = results[i];
titles.push(element.innerText);
}
return titles;
})
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Error:', error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment