search
HomeWeb Front-endJS TutorialNightmare.js introduction and installation

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.

Nightmare.js Introduction

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.

Install Nightmare.js

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.

Step 1 Download Node.js

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/ 

Step 2 Download NPM

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/

Step 3 Install Nightmare.js library

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 -

Nightmare.js Object

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-

Waiting timeout

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.

Go to timeout

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.

Loading timeout

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.

Execution timeout

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.

path

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.

Electronic path

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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment