Writing a Simple Nightmare.js Smoke Test

118 VIEWS

Nightmare.js is a browser automation library which uses PhantomJS, the headless web browser. Nightmare.js is a great tool for various functions—most notably smoke testing. Smoke testing is a form of non-intensive testing which simply ensures basic functions work. Nightmare.js is great for smoke testing because it is super simple to set up and run.

In this post, I will go over how to write a simple smoke test using Nightmare.js.

To get started, make sure you have Nightmare.js installed in your repository with:

npm install nightmare 

and open a JS file. In the file, add the following lines to the top:

const Nightmare = require('nightmare'),
nightmare = Nightmare({});

This creates a new instance of Nightmare which can navigate the Web. It is possible to put optional parameters in the JSON passed in inside the nightmare = Nightmare({}); line—Some of these options can be found at https://github.com/segmentio/nightmare#api. A good method for debugging tests is to pass in parameters as follows:

nightmare = Nightmare({
    show: true
  });

When a test with this is run, it will show a visible web browser rather than a headless one.

With everything set up, it’s time to start building the test. Start a promise chain with:

Nightmare
.goto(‘https://google.com’)

This will simply navigate the test to https://google.com. We can now direct the test to do anything we want in this browser with the commands found at: https://github.com/segmentio/nightmare#api.

Using the various command options, we can write a file that looks like this:

const Nightmare = require('nightmare'),
  nightmare = Nightmare({
    show: true
  });
nightmare
  .goto('https://google.com') //navigates to google
  .wait(2000)
  .type('[class="lst-c"]', 'sweetcode') //types 'sweetcode' into the search bar
  .wait(2000)
  .click('[name="btnK"]') //clicks the search button
  .wait(5000)
  .exists('[href="https://sweetcode.io/"]') //checks if the link to sweetcode exists
  .then((result) => { //result is the result of the above line (boolean)
    console.log('Result:', result);
    return nightmare.end();
  });

Because of line 3 (which reads show: true), when the command node .js is run from the command line, a visible UI browser will pop up, and you can watch the test taking place. There are other params you can put in this object to edit (among other things, the size of the browser).

Nightmare.js can also work very easily with Mocha. Simply by putting the Nightmare.js code into a mocha it function, you can, for example, put an assert into a .then to verify the result through Mocha, as shown here:

const Nightmare = require('nightmare'),
  nightmare = Nightmare({
    show: true
  });
let assert = require('chai').assert;

describe(‘this is a test’, function() {
  it(‘should verify that a hyperlink exists‘, function(done) {
  nightmare
    .goto('https://google.com') //navigates to google
    .wait(2000)
    .type('[class="lst-c"]', 'sweetcode') //types 'sweetcode' into the search bar
    .wait(2000)
    .click('[name="btnK"]') //clicks the search button
    .wait(5000)
    .exists('[href="https://sweetcode.io/"]') //checks if the link to sweetcode exists
    .then((result) => { //result is the result of the above line (boolean)
      console.log('Result:', result);
      assert.equal(result, true);
      return nightmare.end();
    });
  })
})

The above test, when run with mocha .js, should do the same thing as the last test, but it will run through Mocha and will return a Mocha result, as any Mocha test would, failing if the given locator does not exist, due to assert.equal(result, true);.

Using the techniques shown above, as well as the rest of the Nightmare.js API, you can write automated smoke tests incredibly easily.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Judah Bolz-Weber is a Fixate IO contributor and a college student from Denver, CO currently working as a software quality assurance automation engineering apprentice for HomeAdvisor Inc, and working towards his Bachelor's in Applied Computer Science at the University of Colorado Denver. He is fascinated by and enjoys studying all forms of software automation.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu