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
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
Using the techniques shown above, as well as the rest of the Nightmare.js API, you can write automated smoke tests incredibly easily.