Nightmare is a high-level browser automation library for Node.js, which is built over the Electron browser. Cookies are an integral part of web browsing sessions, and managing them is essential for tasks such as session persistence, authentication, and preference tracking.
To manage cookies in a Nightmare session, you'll primarily be using the .cookies.set(cookie)
and .cookies.get(name)
functions.
Here's how you can manage cookies in a Nightmare session:
Setting a Cookie
You can set a cookie by passing a cookie object to the .cookies.set()
method. A cookie object might include the following properties: name
, value
, domain
, path
, expires
, secure
, and httpOnly
.
const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });
nightmare
.goto('http://example.com')
.cookies.set({
name: 'test_cookie',
value: 'test_value',
path: '/',
domain: 'example.com',
secure: false,
httpOnly: false,
expires: (new Date()).getTime() + (5 * 60 * 1000) // Expires in 5 minutes
})
.then(() => {
// Continue with other actions after setting the cookie
})
.catch((error) => {
console.error('Cookie setting failed:', error);
});
Getting a Cookie
To retrieve a cookie, you can use the .cookies.get()
method. You can either get a specific cookie by name or all cookies by passing no arguments.
// To get a specific cookie by name
nightmare
.cookies.get('test_cookie')
.then((cookie) => {
console.log('Cookie retrieved:', cookie);
})
.catch((error) => {
console.error('Cookie retrieval failed:', error);
});
// To get all cookies
nightmare
.cookies.get()
.then((cookies) => {
console.log('All cookies:', cookies);
})
.catch((error) => {
console.error('Cookie retrieval failed:', error);
});
Clearing Cookies
You can clear cookies in two ways: by clearing a specific cookie or by clearing all cookies.
// Clearing a specific cookie by name
nightmare
.cookies.clear('test_cookie')
.then(() => {
console.log('Cookie cleared');
})
.catch((error) => {
console.error('Cookie clearing failed:', error);
});
// Clearing all cookies
nightmare
.cookies.clearAll()
.then(() => {
console.log('All cookies cleared');
})
.catch((error) => {
console.error('Cookie clearing failed:', error);
});
Using Promises and Async/Await
Nightmare's API is promise-based, which means you can chain operations sequentially. If you prefer using async/await syntax, you can do so as follows:
(async () => {
const nightmare = Nightmare({ show: true });
try {
await nightmare.goto('http://example.com');
await nightmare.cookies.set({
name: 'test_cookie',
value: 'test_value',
domain: 'example.com'
// Other cookie attributes
});
const cookie = await nightmare.cookies.get('test_cookie');
console.log('Cookie:', cookie);
await nightmare.end();
} catch (error) {
console.error('An error occurred:', error);
}
})();
Please note that you must have Nightmare installed in your project, and for running the above code, Node.js must be set up correctly. You can install Nightmare via npm by executing npm install nightmare
in your project directory.