Automating Website Screenshots with Node.js and Puppeteer or Playwright

As a web developer, there are many reasons you might need to take screenshots of websites programmatically. Some common use cases include:

  • Visual regression testing to catch UI bugs
  • Generating thumbnails or social media preview images
  • Archiving web pages for record-keeping
  • Monitoring website uptime and performance

While you could take screenshots manually, automating the process with a tool like Puppeteer or Playwright can save a lot of time and effort, especially if you need to capture many pages. In this post, we‘ll explore how to use these two popular Node.js libraries to automate screenshot generation and compare their features and performance.

Puppeteer Overview

Puppeteer is a Node.js library developed by Google that provides a high-level API for controlling headless Chrome or Chromium. Headless browsers are regular web browsers that don‘t display a user interface. They are very useful for automation tasks like scraping, testing, and of course, taking screenshots.

Here‘s a simple example of using Puppeteer to navigate to a URL and take a screenshot:

const puppeteer = require(‘puppeteer‘);

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(‘https://example.com‘);
  await page.screenshot({path: ‘example.png‘});
  await browser.close();
})();

By default, this will take a screenshot of the visible portion of the page (the viewport) at the default size of 800×600 pixels. But Puppeteer provides many options to customize the screenshot, such as:

  • fullPage: Capture the full scrollable page, not just the visible viewport
  • clip: Capture a specific rectangular area of the page
  • omitBackground: Transparency instead of a white background for the screenshot
  • quality: The quality (0-100) of the image compression (for JPEG screenshots)
  • path: The file path to save the screenshot to
  • type: The image format – png (default) or jpeg

For example, to take a full page screenshot as a JPEG with 50% quality:

await page.screenshot({
  path: ‘example.jpg‘, 
  fullPage: true, 
  type: ‘jpeg‘,
  quality: 50
});

In addition to basic screenshots, Puppeteer can also:

  • Generate PDFs of web pages with page.pdf()
  • Screenshot specific DOM nodes with elementHandle.screenshot()
  • Capture screenshots in headful (non-headless) mode
  • Authenticate pages that require login using page.authenticate() or by manipulating cookies

Playwright Overview

Playwright is a newer cross-browser automation library developed by Microsoft. It supports Chromium (like Puppeteer), Firefox, and WebKit (Safari) browsers.

The API for Playwright is very similar to Puppeteer. Here‘s the equivalent screenshot script from above for Playwright:

const { chromium } = require(‘playwright‘);

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto(‘https://example.com‘);
  await page.screenshot({ path: ‘example.png‘ });
  await browser.close();
})();

The main difference is that Playwright requires you to specify which browser engine to use (e.g. chromium, firefox, or webkit).

Playwright shares most of the same screenshot configuration options as Puppeteer. A few extra ones include:

  • animations: Control CSS animation and transition support
  • scale: Adjusts the scale factor for high-DPI screenshots

Playwright also supports taking screenshots of particular elements and generating PDFs.

Puppeteer vs Playwright

So how do Puppeteer and Playwright stack up in terms of screenshot capabilities and performance? Let‘s take a look at some key differences and benchmarks.

PuppeteerPlaywright
BrowsersChromiumChromium, Firefox, WebKit
Node version>= 8.9.0>= 12
MaintainerGoogleMicrosoft
APIUses PromisesUses Promises, supports async/await

In terms of performance for taking screenshots, both libraries are quite fast. In one benchmark test, Playwright narrowly edged out Puppeteer, capturing 100 screenshots in 14.24 seconds vs 15.75 seconds for Puppeteer.

However, the time to take a single screenshot is typically small (< 100ms), so unless you are capturing thousands of screenshots, the difference may not be noticeable.

Other factors like network and disk I/O speed often have a larger impact on overall performance when taking many screenshots. To optimize the process, you can:

  • Run the script on the same physical machine or datacenter as the target website to minimize latency
  • Use a fast SSD for saving the screenshots to avoid disk bottlenecks
  • Scale horizontally by running multiple instances of the script in parallel

Real-World Example

At my previous job, we used automated screenshots as part of a visual regression testing system for our web app. We had a Jenkins job that would:

  1. Launch a fresh container and checkout the latest code
  2. Build the web app and start the dev server
  3. Run a Puppeteer script to navigate to key pages and take fullsize screenshots
  4. Compare the new screenshots to baseline images using Resemble.js
  5. If the diff exceeded a threshold, fail the build and notify the team

This ended up catching many subtle UI bugs that normal unit tests missed, such as style regressions, mobile layout issues, and content overflows. Integrating this automated screenshot system into our CI/CD pipeline improved the quality of our application and freed up QA resources for more exploratory testing.

Conclusion

Puppeteer and Playwright are both excellent choices for automating screenshot capture in Node.js. They share a similar API and features, with Playwright having the added benefit of cross-browser support.

Which one you choose largely depends on your specific use case and environment. If you only need to capture Chrome/Chromium screenshots, Puppeteer may be slightly simpler. If you need Firefox or Safari support, or want to compare rendering across browsers, Playwright is the way to go.

Regardless of which tool you pick, automated screenshots are a powerful addition to your web development arsenal. They can enhance your testing, monitoring, and archival processes and catch visual issues before they make it to production.

Did you like this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.