Nightmare is a high-level browser automation library for Node.js. It is built on top of Electron, which is a framework for building cross-platform desktop apps with web technologies. Nightmare is often used for web scraping, automation of web application actions, and end-to-end testing.
Event hooks in Nightmare are essentially listeners you can attach to specific events that occur in the course of a Nightmare script's execution. They allow you to react to or log certain actions, errors, or changes in the browser context.
Here are some of the commonly used event hooks available in Nightmare:
did-start-loading
: Fires when the navigation is started.did-stop-loading
: Fires when the navigation is done.did-get-response-details
: Emitted when details about the response are available.did-get-redirect-request
: Emitted when a redirect is requested.page
: Generic hook for various page-level events, such asconsole
messages,alert
dialogs, etc.console
: Emitted when the page logs a console message.alert
: Emitted when the page triggers an alert dialog.confirm
: Emitted when the page triggers a confirm dialog.prompt
: Emitted when the page triggers a prompt dialog.error
: Emitted when there is an error in the Electron process.timeout
: Emitted when an operation exceeds the defined timeout period.
To use these event hooks, you attach them to the Nightmare instance using the .on(event, callback)
method. Below is an example in Node.js showing how to use some of these event hooks:
const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });
// Listen for console.log messages from the page
nightmare.on('console', (type, ...args) => {
console.log(`Console ${type}:`, args.join(', '));
});
// Listen for alerts
nightmare.on('alert', (message) => {
console.log(`Alert message: ${message}`);
});
// Listen for errors
nightmare.on('page', (type, message, stack) => {
if (type === 'error') {
console.error(`Page error: ${message}\nStack trace: ${stack}`);
}
});
// Listen for load started
nightmare.on('did-start-loading', () => {
console.log('Navigation started');
});
// Listen for load stopped
nightmare.on('did-stop-loading', () => {
console.log('Navigation done');
});
// Perform actions with Nightmare
nightmare
.goto('http://example.com')
.type('input[ name="q"]', 'github nightmare')
.click('.search-button')
.wait('.result')
.evaluate(() => document.querySelector('.result').textContent)
.end()
.then((text) => {
console.log('Search Result:', text);
})
.catch((error) => {
console.error('Search failed:', error);
});
In this example, we're listening for various events like console messages, alerts, and navigation events, and we're printing information to the console when these events occur. This can be very useful for debugging and understanding what's happening inside the browser context during the execution of your Nightmare script.
Please note that the Nightmare library, while still functional, has not been actively maintained since around 2019, and many developers have transitioned to using other libraries such as Puppeteer, which is actively maintained by the Chrome DevTools team.