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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing
