Introduction and Installation of Nightmare.js


Nightmare is a high-level automation library offered by segment. It's a good option for smoke test automation because it can perform all of the text inputs, clicks, and visual checks that a person can. It operates as a headless browser using Electron. In this article, we will discuss Nightmare.js in detail and the installation of Nightmare.js.

Introduction to Nightmare.js

Segment offers Nightmare, a sophisticated browser automation library. The objective of this Nightmare is to mimic user activities (such as goto, type, click, etc.), rather than using heavily nested callbacks, to expose a few basic methods with an API that feels synchronous for each block of scripting. Although it was intended to automate operations across websites without APIs, today it is most frequently used for crawling, UI testing, etc.

Note − NodeJS 4.x or above is required to run Nightmare.

Installation of Nightmare.js

To test website interfaces, we experimented with Nightmare.js, a JavaScript browser automation library.

Using Nightmare on your PC is more straightforward than doing so on Cloud 9. Here are the setup and starting instructions.

Step 1 Download Node.js

JavaScript typically operates in a browser. Node.js is a variation of JavaScript that may be used on a server, on a terminal, or on other hardware. It enables the simple creation and execution of Nightmare scripts.

Link of Download Node.js

https://nodejs.org/en/download/ 

Step 2 Download NPM

You may set up the library Nightmare using the Node Package Manager (npm). Nightmare's dependencies will all be installed, along with it when you use npm to install it.

Link of installation of NPM

https://nodejs.org/en/

Step 3 Install Nightmare.js Library

Using the command

npm install nightmare 

We have seen how to install the nightmare.js library in the node js playground, now let’s move to see some of the basics of the nightmare and then we will move to the objects of the nightmare.

The basic syntax of the nightmare is −

const Nightmare = require('nightmare')
const nightmare = Nightmare({object : value_of_object})  

In the above code, we have imported the ‘nightmare’ in our code using the require() method of node js then we have stored it into a variable which we have later used to define some object and value for that in parenthesis of stored variable and again stored that into a new variable.

We have seen some basic code structures of nightmare.js now let’s move to the objects of Nightmare.js −

Objects of the Nightmare.js

As nightmare.js creates a new object that navigates through the web and provides the useful stuff, to add some more specifications here we have the objects of the nightmare that will help us to get better web surfing, let’s see some of them −

watiTimeout

This object comes with a default time of 30 seconds and the user can also define some of the time according to the need and throws an exception if the function “ .wati() ” fails to return a true value within the set timeframe.

const Nightmare = require('nightmare')
const nightmare = Nightmare({
   waitTimeout: 2000 // in milliseconds
}) 

In the above code, first, we imported the nightmare into our system then we created an object for it. In the variable, we have provided the object ‘waitTimeout’ with its value of 2000 milliseconds.

gotoTimeout

This object comes with a default time of 30 seconds and the user can also define some of the time according to the need and throws an exception if the function “ .goto() ” fails to load within the provided or the set timeframe.

const Nightmare = require('nightmare')
const nightmare = Nightmare({
   gotoTimeout: 2000 // in milliseconds
})

In the above code, first, we imported the nightmare into our system then we created an object for it. In the variable, we have provided the object ‘gotoTimeout’ with its value of 2000 milliseconds.

loadTimeout

This object comes with a default time of infinite seconds and the user can also define some of the time according to the need and forces Nightmare to get away or move if the transition of the page due to an action like click, etc didn’t complete within the given time.

const Nightmare = require('nightmare')
const nightmare = Nightmare({
   loadTimeout: 2000 // in milliseconds
})

In the above code, first, we imported the nightmare into our system then we created an object for it. In the variable, we have provided the object ‘loadTimeout’ with its value of 2000 milliseconds.

executionTimeout

This object comes with a default time of 30 seconds and the user can also define some of the time according to the need and shows the maximum amount of time to wait for the statement of method ‘.evaluate’ to get complete.

const Nightmare = require('nightmare')
const nightmare = Nightmare({
   executionTimeout: 2000 // in milliseconds
})

In the above code, first, we imported the nightmare into our system then we created an object for it. In the variable, we have provided the object ‘exectutionTimeout’ with its value of 2000 milliseconds.

paths

There are many default systems paths defined that are known by the electron. These paths can be overwritten by using the path object of the nightmare and that can be done by the following code −

const Nightmare = require('nightmare')
const nightmare = Nightmare({
   paths: {
      userData: ‘/user/data
   }
})

In the above code, first, we imported the nightmare into our system then we created an object for it. In the variable, we have provided the object paths in which we have defined a path for an electron.

electronPath

These are quite helpful for the testing of the various versions of the Electron and these are the path that is prebuilt to the electron binary. These paths can be overwritten by using the path object of the nightmare. Let’s see a code of how electronpath is defined −

const Nightmare = require('nightmare')
const nightmare = Nightmare({
   electronPath: require('electron')
})

In the above code, first, we imported the nightmare into our system then we created an object for it. In the variable, we have provided the object electronPath and its key value will be imported using the required keyword.

switches

Electron also supports the switches that are supported by the google command line. There are various switches that are in this category. Let’s move to the code to get information on how to use the switches objects in a nightmare.js object −

const Nightmare = require('nightmare')
const nightmare = Nightmare({
   switches: {
      'key1' : 'value1',
      'key2' : 'value2',
   }
})

In the above code, first, we imported the nightmare into our system then we created an object for it. In the variable, we have provided the object switches, and in that, we have defined two key-value pairs.

Conclusion

In this article, we have learned Nightmare.js in detail and the installation of Nightmare.js. Nightmare is a high-level automation library offered by segment. It's a good option for smoke test automation because it can perform all of the text inputs, clicks, and visual checks that a person can. It operates as a headless browser using Electron.

Updated on: 02-Mar-2023

310 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements