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
Replace String Characters in JavaScriptReplace String Characters in JavaScriptMar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Build Your Own AJAX Web ApplicationsBuild Your Own AJAX Web ApplicationsMar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 jQuery Fun and Games Plugins10 jQuery Fun and Games PluginsMar 08, 2025 am 12:42 AM

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

How do I create and publish my own JavaScript libraries?How do I create and publish my own JavaScript libraries?Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

jQuery Parallax Tutorial - Animated Header BackgroundjQuery Parallax Tutorial - Animated Header BackgroundMar 08, 2025 am 12:39 AM

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

Auto Refresh Div Content Using jQuery and AJAXAuto Refresh Div Content Using jQuery and AJAXMar 08, 2025 am 12:58 AM

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optiona

Getting Started With Matter.js: IntroductionGetting Started With Matter.js: IntroductionMar 08, 2025 am 12:53 AM

Matter.js is a 2D rigid body physics engine written in JavaScript. This library can help you easily simulate 2D physics in your browser. It provides many features, such as the ability to create rigid bodies and assign physical properties such as mass, area, or density. You can also simulate different types of collisions and forces, such as gravity friction. Matter.js supports all mainstream browsers. Additionally, it is suitable for mobile devices as it detects touches and is responsive. All of these features make it worth your time to learn how to use the engine, as this makes it easy to create a physics-based 2D game or simulation. In this tutorial, I will cover the basics of this library, including its installation and usage, and provide a

How do I optimize JavaScript code for performance in the browser?How do I optimize JavaScript code for performance in the browser?Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor