Home >Web Front-end >JS Tutorial >Nightmare.js introduction and installation
Nightmare is an advanced automation library provided by Segment. It's a good choice for smoke test automation because it can perform all the text entry, clicking, and visual inspection that a human can. It runs as a headless browser using Electron. In this article, we will discuss Nightmare.js and Nightmare.js installation in detail.
Segment provides Nightmare, a sophisticated browser automation library. The goal of this nightmare is to emulate user activity (e.g. go to, type, click, etc.), and instead of using tons of nested callbacks, expose a few basic methods via the API to make each script block feel synchronous. Although its purpose is to automate operations across websites without an API, today it is most commonly used for crawling, UI testing, etc.
Note - NodeJS 4.x or higher is required to run Nightmare.
To test the website interface, we tried Nightmare.js, a JavaScript browser automation library.
Using Nightmare on PC is simpler than using it on Cloud 9. Below are setup and startup instructions.
JavaScript usually runs in the browser. Node.js is a variation of JavaScript that can be used on servers, terminals, or other hardware. It makes it easy to create and execute Nightmare scripts.
Node.js download link
https://nodejs.org/en/download/
You can set up the Nightmare library using the Node Package Manager (npm). Nightmare's dependencies will all be installed, and when you install it using npm, it will be installed as well.
NPM installation link
https://nodejs.org/en/
Use command
npm install nightmare
We've seen how to install the Nightmare.js library in the Node js Playground, now let's look at some basics of nightmare and then we'll move on to the nightmare object.
The basic syntax of nightmare is -
const Nightmare = require('nightmare') const nightmare = Nightmare({object : value_of_object})
In the above code, we imported "Nightmare" in the code using Node js's require() method, and then stored it into a variable, which we later used to define some objects and values in brackets Store the variable and store it again into a new variable.
We have already understood some basic code structures of nightmare.js, now let us turn to the objects of Nightmare.js -
Since Nightmare.js creates a new object that can navigate the web and provide useful stuff, to add more specifications here we have the Nightmare object that will help us get better web surfing, Let’s take a look at some of them-
The default time of this object is 30 seconds. The user can also define some time as needed. If the function ".wati()" fails to return a true value within the set time range, an exception will be thrown.
const Nightmare = require('nightmare') const nightmare = Nightmare({ waitTimeout: 2000 // in milliseconds })
In the above code, first, we import Nightmare into our system and then create an object for it. In the variable we provide the object "waitTimeout" with the value 2000 milliseconds.
The default time of this object is 30 seconds. The user can also define some time as needed. If the function ".goto()" fails to load within the provided or set time range, an exception will be thrown.
const Nightmare = require('nightmare') const nightmare = Nightmare({ gotoTimeout: 2000 // in milliseconds })
In the above code, first, we import Nightmare into our system and then create an object for it. In the variable we provide the object "gotoTimeout" with the value 2000 milliseconds.
This object has a default time of infinite seconds. The user can also define some time as needed and force Nightmare to leave when the page transition does not occur due to clicks and other operations or the movement does not complete within the given time.
const Nightmare = require('nightmare') const nightmare = Nightmare({ loadTimeout: 2000 // in milliseconds })
In the above code, first, we import Nightmare into our system and then create an object for it. In the variable we provide the object "loadTimeout" with the value 2000 milliseconds.
This object has a default time of 30 seconds. The user can also define some time as needed and display the maximum time to wait for the statement of the method ".evaluate" to be completed.
const Nightmare = require('nightmare') const nightmare = Nightmare({ executionTimeout: 2000 // in milliseconds })
In the above code, first, we import Nightmare into our system and then create an object for it. In the variable we provide the object "executionTimeout" with the value 2000 milliseconds.
Electron defines many default system paths. These paths can be overridden by using Nightmare's path object, which can be done with the following code -
const Nightmare = require('nightmare') const nightmare = Nightmare({ paths: { userData: ‘/user/data’ } })
In the above code, first, we import Nightmare into our system and then create an object for it. In the variables we provide the object path in which the electron path is defined.
These are very helpful for testing various versions of Electron, and these are pre-built paths to the Electron binaries. These paths can be overridden by using Nightmare's path object. Let's look at the code of how electron paths are defined -
const Nightmare = require('nightmare') const nightmare = Nightmare({ electronPath: require('electron') })
在上面的代码中,首先,我们将噩梦导入到我们的系统中,然后为它创建了一个对象。在变量中,我们提供了对象 electroPath ,其键值将使用 required 关键字导入。
Electron 还支持 google 命令行支持的开关。此类别中有多种开关。让我们转到代码以获取有关如何在 nights.js 对象中使用开关对象的信息 -
const Nightmare = require('nightmare') const nightmare = Nightmare({ switches: { 'key1' : 'value1', 'key2' : 'value2', } })
在上面的代码中,首先,我们将噩梦导入到我们的系统中,然后为它创建了一个对象。在变量中,我们提供了对象开关,并在其中定义了两个键值对。
在这篇文章中,我们详细学习了Nightmare.js以及Nightmare.js的安装。 Nightmare是一个由segment提供的高级自动化库。它是烟雾测试自动化的一个不错的选择,因为它可以执行人类可以执行的所有文本输入、单击和目视检查。它使用 Electron 作为无头浏览器运行。
The above is the detailed content of Nightmare.js introduction and installation. For more information, please follow other related articles on the PHP Chinese website!