Screenshot
A screenshot is a captured image of the visible area of a web page. In the context of web automation and testing, particularly with the Nightmare library, the screenshot function is used to capture the rendered content of a web page and save it as an image file. This can be particularly useful for visual testing, documentation, or simply capturing the state of a web page at a specific point in time.
Capturing Specific Elements
When using Nightmare, you can capture specific parts of a web page by defining a rectangle that specifies the area to be captured. This is done by passing a selector for the element you want to capture into the evaluate function. The function then calculates the bounding rectangle of the element, which is used to limit the screenshot to just that area. This is particularly useful when you want to capture a specific chart or component on a page without including other elements.
Example: Capturing a Chart
In the following example, we use Nightmare to capture a specific chart on a web page:
function captureWebPage (urlToCapture, captureElementSelector, outputImagePath) {
const nightmare = new Nightmare();
return nightmare.goto(urlToCapture)
.wait(captureElementSelector)
.evaluate(captureElementSelector => {
const body = document.querySelector("body");
const captureElement = document.querySelector(captureElementSelector);
const captureRect = captureElement.getBoundingClientRect();
return {
documentArea: {
width: body.scrollWidth,
height: body.scrollHeight
},
captureArea: {
x: captureRect.left,
y: captureRect.top,
width: captureRect.right - captureRect.left,
height: captureRect.bottom - captureRect.top
}
};
}, captureElementSelector)
.then(pageDetails => {
return nightmare.viewport(
pageDetails.documentArea.width,
pageDetails.documentArea.height
)
.screenshot(outputImagePath, pageDetails.captureArea)
.end();
});
};
In this code, the captureElementSelector is passed to the evaluate function, allowing the browser code to access it. The function calculates the dimensions of the element to be captured and returns these details to the Node.js environment, where the screenshot is taken.
Using a Virtual Framebuffer
On headless Linux servers, capturing screenshots can be challenging because there is no display. To overcome this, a virtual framebuffer like Xvfb can be used. Xvfb simulates a display, allowing applications that require a graphical interface to run on a server without a physical display.
Example: Using Xvfb with Nightmare
const Nightmare = require('nightmare');
const Xvfb = require('xvfb');
function captureWebPage (urlToCapture, captureElementSelector, outputImagePath) {
const xvfb = new Xvfb();
xvfb.startSync();
const nightmare = Nightmare();
return nightmare.goto(urlToCapture)
.wait(captureElementSelector)
.evaluate(captureElementSelector => {
const body = document.querySelector("body");
const captureElement = document.querySelector(captureElementSelector);
const captureRect = captureElement.getBoundingClientRect();
return {
documentArea: {
width: body.scrollWidth,
height: body.scrollHeight
},
captureArea: {
x: captureRect.left,
y: captureRect.top,
width: captureRect.right - captureRect.left,
height: captureRect.bottom - captureRect.top
}
};
}, captureElementSelector)
.then(pageDetails => {
return nightmare.viewport(
pageDetails.documentArea.width,
pageDetails.documentArea.height
)
.screenshot(outputImagePath, pageDetails.captureArea)
.end();
})
.then(() => xvfb.stopSync());
};
In this example, Xvfb is started before the Nightmare instance is created and stopped after the screenshot is taken. This allows the script to run on a headless server without hanging.
Basic Screenshot Capture
For simpler use cases, you can capture the entire visible area of a web page without specifying a particular element. This is useful for capturing the overall appearance of a page.
Example: Basic Screenshot Capture
const webServer = require('./web-server.js');
const Nightmare = require('nightmare');
webServer.start()
.then(server => {
const outputImagePath = "./output/nyc-temperatures.png";
const nightmare = new Nightmare();
return nightmare.goto("http://localhost:3000")
.wait("svg")
.screenshot(outputImagePath)
.end()
.then(() => server.close());
})
.then(() => {
console.log("All done :)");
})
.catch(err => {
console.error("Something went wrong :(");
console.error(err);
});
In this example, the script starts a web server, navigates to a local URL, waits for an SVG element to load (indicating that the chart is ready), and then captures a screenshot. The screenshot is saved to the specified file path, and the web server is closed once the process is complete.
FAQ (Frequently asked questions)
What is a screenshot in the context of web pages?
How does Nightmare capture a screenshot?