Puppeteer

On this page 7

Puppeteer is an open-source Node.js library for browser automation that controls Chrome, Chromium and Firefox through a high-level JavaScript API, either in headless mode without a visible window or in a normal headed window. In web testing it is used to drive pages the way a user would, clicking, typing and navigating, and to inspect the results, while the same capabilities also make it popular for screenshots, PDF generation, performance tracing and crawling.

The Chrome DevTools team released Puppeteer in 2017. It originally spoke only the Chrome DevTools Protocol (CDP), which tied it closely to Chromium; later versions added support for WebDriver BiDi, the standardized bidirectional browser automation protocol, which allows the same scripts to drive Firefox as well.

How Puppeteer controls the browser

Puppeteer launches or connects to a browser and communicates with it over a WebSocket connection using CDP or WebDriver BiDi messages. Each script call, such as navigating to a URL, is translated into protocol commands, and the browser sends back events and results that Puppeteer exposes as promises.

ObjectRepresentsTypical operations
BrowserA running browser instanceOpen pages, create contexts, close
BrowserContextAn isolated session like an incognito profileSeparate cookies and storage
PageOne tabgoto, click, type, screenshot, pdf
FrameA document inside a page or iframeEvaluate scripts, query elements
LocatorA lazy element query with automatic waitingclick, fill, wait for visibility
ElementHandleA reference to a concrete DOM elementInspect or interact directly
Core objects in the Puppeteer API

By default npm install puppeteer downloads a matched browser build, known as Chrome for Testing, so scripts run against a version the library was tested with. The lighter puppeteer-core package skips the download and connects to a browser the team manages itself.

Typical uses of Puppeteer

Puppeteer is not a test framework on its own; it is an automation library that test frameworks and other tools build on. Its uses fall into a few recurring categories.

Use caseKey API featuresPractical note
End-to-end testsLocators, navigation, assertions via JestNeeds a runner and assertion library
Screenshots and visual checkspage.screenshot with clip and fullPageFix viewport and fonts for stable images
PDF generationpage.pdf with print CSSChromium only, uses print rendering
Performance analysisTracing and metrics, Lighthouse user flowsResults depend on device emulation
Crawling and prerenderingNetwork interception, content extractionRespect robots rules and site terms
Form and workflow automationTyping, uploads, dialogsFragile against layout changes
Common Puppeteer use cases

Because Puppeteer can intercept network requests, tests can block third-party scripts, return mocked API responses or simulate slow connections. This makes it suitable for checking how a page behaves under controlled conditions that would be hard to reproduce manually.

Puppeteer compared with Playwright, Selenium and Cypress

Puppeteer sits among several browser automation tools with overlapping goals. The main differences concern browser coverage, language support and the amount of built-in testing infrastructure.

PropertyPuppeteerPlaywrightSeleniumCypress
BrowsersChrome, FirefoxChromium, Firefox, WebKitAll major via driversChromium family, Firefox, WebKit experimental
LanguagesJavaScript, TypeScriptJS, Python, Java, .NETMany languagesJavaScript, TypeScript
ProtocolCDP and WebDriver BiDiCDP and patched protocolsWebDriver and BiDiRuns inside the browser
Built-in test runnerNoYesNoYes
Auto-waitingThrough locatorsBuilt in everywhereExplicit waitsBuilt in
Puppeteer and related automation tools

Playwright was created by engineers who had previously worked on Puppeteer, so the APIs feel similar. Teams that need WebKit coverage, multiple languages or an integrated runner usually choose Playwright, while Puppeteer remains a lean choice for Chrome-focused automation and server-side rendering tasks such as PDF output.

Writing reliable Puppeteer scripts

Most instability in browser automation comes from timing assumptions. A script that clicks an element before it is attached, visible or enabled fails intermittently, which is why modern Puppeteer code prefers locators with automatic waiting over fixed delays.

Flaky patternProblemBetter alternative
Fixed sleep callsToo short on slow machines, wasteful on fast onesWait for a condition or locator
Fragile CSS selectorsBreak when styling changesUse roles, text or test attributes
Shared browser stateTests influence each otherNew browser context per test
Unhandled navigationClick resolves before the page loadsAwait navigation together with the click
Unclosed browsersLeaked processes in CIClose in a finally block
Flaky patterns and better alternatives

In continuous integration, headless Chrome may need extra flags or a larger shared memory area inside containers. Pinning the Puppeteer version, and therefore the browser version, keeps results reproducible across machines.

Limitations and responsible use

Puppeteer has clear boundaries that teams should know before adopting it. It does not drive Safari or WebKit, so cross-browser testing still requires another tool, and some features, notably PDF generation, are available only in Chromium.

  • Headless differences: older headless modes rendered differently from headed Chrome; the current headless mode shares the full browser code.
  • Resource cost: each browser instance consumes significant memory, which limits parallelism on small CI runners.
  • Bot detection: many sites detect automated browsers, and evading such measures can breach terms of service.
  • Security: running untrusted pages with sandboxing disabled exposes the host machine.

Used within these limits, Puppeteer is a dependable building block for automated checks of real rendered pages, catching problems that unit tests and HTML validators cannot see.

The idea in brief

Testing a website by hand means opening it, clicking buttons, filling in forms and checking that everything looks right, over and over after every change. Puppeteer lets a computer do that clicking and checking automatically.

It works like a remote control for a web browser. A short program tells the browser where to go and what to press, then looks at what appeared. It can also take pictures of pages or save them as PDF files, all without anyone sitting at the screen.

A practical scenario

A software company that sells invoicing software needs to generate thousands of PDF invoices each night with exactly the same layout customers see in the browser. An earlier approach using a separate PDF library produced slightly different fonts and page breaks, which led to complaints.

The team sets up a small service that uses Puppeteer with headless Chrome. For each invoice, it opens the invoice page with print styles, waits until web fonts have loaded and calls page.pdf with A4 format and fixed margins. Five browser pages run in parallel inside one browser instance. The nightly batch of about 12,000 invoices completes well within the maintenance window, and because the same HTML and CSS drive both the screen and the PDF, layout discrepancies between the two disappear.