Browser automation is useful for print testing, but only if you split the problem correctly. window.print() is not the same thing as verifying the browser’s print dialog, and print preview is not the same thing as validating the final PDF or hard-copy layout. Those are three different layers, and each needs a different test strategy.

The short version: use Playwright or Selenium to verify the DOM and page state before print, emulate print media to catch CSS regressions, and validate exported PDFs separately. Do not expect either framework to fully automate the native print dialog across browsers. That boundary is controlled by the browser, not your test code.

What you can verify reliably

For print-related flows, the most stable checks are usually these:

  1. The page is in the correct state before printing, for example, filters applied, totals loaded, or a report finished rendering.
  2. Print-specific CSS is active, including @media print rules, page breaks, hidden elements, and typography changes.
  3. The generated PDF or printed output matches layout expectations closely enough to catch regressions.

If the failure mode is a broken print button, automation can catch it. If the failure mode is the OS print dialog UI, browser automation is usually the wrong abstraction.

What browser automation cannot promise

Native print preview and the system print dialog are browser and OS features. Web drivers can trigger them, but not reliably inspect or control them the way they can inspect DOM nodes.

That means these checks are brittle or out of scope for normal browser automation:

  • Selecting a printer from the system dialog
  • Reading native preview UI text and controls
  • Verifying platform-specific print settings, such as duplex or destination choice
  • Comparing the rendered output that the operating system sends to a physical printer

You can still test the application path that leads to printing. For most teams, that is enough.

The practical split: three layers of print testing

1. DOM-level checks before print

This layer answers, “Did the app prepare the right content?” It is the cheapest layer to maintain and the easiest to debug.

Examples:

  • The report finished loading
  • The correct account, date range, or invoice is displayed
  • Print-only controls are hidden before calling window.print()
  • Required content exists and is not clipped by layout logic

2. Print media emulation and CSS assertions

This layer answers, “Does the page look correct under print styles?” It catches regressions in @media print, page breaks, position: fixed, and elements hidden only in print.

This is where Playwright is especially direct because it has built-in browser context APIs for media emulation and PDF generation. Selenium can do some of this too, but the ergonomics differ by browser and driver.

3. PDF export or print output validation

This layer answers, “Did the exported document preserve layout?” It is the closest stable proxy for actual print output when the app can generate PDFs or when the browser can print to PDF in headless mode.

At this layer, you validate page count, selected text, key headings, and layout integrity. Visual diffing can help, but even simple file-based assertions catch a lot of accidental regressions.

Decision table: which layer catches which failure

Failure type Best test layer Why it works Tool fit
Wrong report data before printing DOM-level preflight Fast, deterministic, easy to debug Playwright, Selenium
Missing @media print styles Print media emulation Exercises print CSS without native dialog Playwright first, Selenium possible
A heading is pushed onto a second page PDF validation Page-based output is visible in the artifact Playwright, Selenium with PDF flow
Native print dialog does not open Smoke test around window.print() Confirms the trigger path runs Playwright, Selenium
Printer selection or OS dialog behavior Not a good browser-automation target Outside the browser DOM boundary Manual or OS-level automation

Playwright: the cleaner path for print CSS and PDF checks

Playwright’s documentation covers browser context, page emulation, and PDF generation, which makes it a strong fit for print-layout regression work.

A minimal flow often looks like this:

import { test, expect } from '@playwright/test';
test('invoice renders correctly for print', async ({ page }) => {
  await page.goto('https://example.com/invoice/123');

  await expect(page.getByRole('heading', { name: /invoice/i })).toBeVisible();

  await page.emulateMedia({ media: 'print' });
  await expect(page.locator('.screen-only')).toBeHidden();
  await expect(page.locator('.print-only')).toBeVisible();

  await page.pdf({ path: 'invoice.pdf', format: 'A4' });
});

A few details matter here:

  • emulateMedia({ media: 'print' }) lets you check print CSS without depending on the native dialog.
  • page.pdf() is only available for Chromium-based browser contexts, so your print regression path should not assume every browser can produce the same artifact.
  • If the page relies on async data, wait for the content to settle before generating the PDF, otherwise you will create flaky snapshots.

A useful pattern for window.print() tests in Playwright

You usually do not need to assert the native dialog itself. It is more stable to assert that the print trigger is wired correctly and the page enters the expected state.

await page.exposeFunction('onBeforePrint', () => {
  console.log('print triggered');
});
await page.evaluate(() => {
  window.addEventListener('beforeprint', () => {
    (window as any).onBeforePrint();
  });
  window.print();
});

This checks that your page reaches the print hook. It does not prove the OS dialog is usable, and that is the correct limitation to acknowledge.

Selenium: still useful, but the print workflow is more browser-dependent

WebDriver can drive the same page, but print-specific behavior depends more heavily on browser and driver support. Selenium is still a valid choice if your team already standardizes on it, but the path from test code to print artifact tends to be less direct.

A basic Python example for preflight assertions looks like this:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome() driver.get(‘https://example.com/invoice/123’)

heading = driver.find_element(By.CSS_SELECTOR, ‘h1’) assert ‘Invoice’ in heading.text

driver.execute_script(“window.matchMedia(‘print’).matches”) driver.quit()

That is only a start. Selenium can inspect DOM state and run JavaScript, but if your goal is PDF generation or structured print-output checks, you will usually need browser-specific handling around headless runs or external PDF capture.

Selenium print media emulation caveat

Selenium itself does not define a universal print-emulation API across all browsers. You can still toggle logic with JavaScript or browser-specific settings, but the exact mechanism is less standardized than Playwright’s media emulation.

That matters when your test goal is layout regression, not just a print button smoke test.

A good test structure for print layouts

For most teams, a layered test suite is better than one giant print test.

Layer A, fast and stable

Use DOM assertions to verify the pre-print state:

  • correct page loaded
  • required data present
  • no loading spinner
  • content visible and non-empty
  • print action enabled only when ready

Layer B, print CSS checks

Switch to print media and verify the rules that matter:

  • @media print hides navigation, modals, and sticky controls
  • page headers and footers render correctly
  • break-before, break-after, and break-inside behave as expected
  • fonts, margins, and page size are correct

Layer C, artifact checks

Generate a PDF or printed artifact and check:

  • file exists and is non-empty
  • page count is reasonable
  • document title, invoice number, or report heading is present
  • critical sections do not split badly across pages

If your app produces PDFs server-side, test the server output directly as well. That is often more deterministic than going through the browser just to get a printable document.

Failure modes worth designing for

Hidden content becomes visible in print

A screen-only sidebar, toast, or navigation element can leak into print if the selector is too broad. This is a good candidate for an explicit print-media assertion.

Page breaks move content unexpectedly

Long tables, accordions, or card grids can spill across pages in ways that look fine on screen but fail in print. Target those components with a PDF regression fixture.

Async data is still loading

Print tests often fail because the page is not ready, not because the print CSS is wrong. Wait for the network or a clear application-ready signal before switching to print media.

Browser differences hide layout bugs

If your team supports both Chromium and Firefox, do not assume one browser’s print rendering matches another’s. Decide which browser is authoritative for the print artifact, then make that choice explicit in the suite.

When Playwright is the better fit

Choose Playwright if you want one tool to handle most of the print-validation workflow with less ceremony. Its media emulation and PDF support make it easier to separate CSS checks from artifact checks.

It is especially practical when:

  • your print regressions are mostly CSS and layout issues
  • you want reliable headless runs in CI
  • you need a short path from test to PDF artifact
  • your team is comfortable with browser-context APIs

When Selenium is the better fit

Choose Selenium if your stack already depends on WebDriver, if you need to keep one automation standard across a larger test estate, or if your team already has a stable reporting and grid setup around Selenium.

It is a better fit when:

  • the organization already has Selenium expertise
  • print tests are a small part of a wider WebDriver suite
  • you are willing to use browser-specific workarounds for artifact generation
  • changing the framework would create more risk than value

What I would not over-automate

I would not try to fully automate native print dialogs just to prove the dialog exists. That is usually brittle, slow, and expensive to debug.

Instead, I would spend automation budget on the parts that actually regress in production:

  • readiness before print
  • print CSS correctness
  • exported document integrity
  • page break behavior in known problem components

That gives you a test suite that fails for real reasons, not because a browser dialog changed in an update.

A concise recommendation

If your goal is to test print preview in Playwright and Selenium, use this rule of thumb:

  • Use Playwright for print media emulation and PDF-oriented layout regression checks.
  • Use Selenium when your team already runs on WebDriver and you only need smoke-level verification plus a few targeted print assertions.
  • Use both only if you have a real migration constraint, not because the same print problem needs two frameworks.

For most frontend and QA teams, the highest-value split is simple: DOM assertions before print, print-media checks during print simulation, and PDF validation after export.

FAQ

Can browser automation test the actual print dialog?

Not reliably in a cross-browser, cross-OS way. You can usually test the path that triggers printing, but not the native dialog UI as a stable DOM surface.

Is window.print() enough for regression coverage?

No. It proves the print trigger runs, but it does not verify print CSS, page breaks, or the final artifact.

Should I use screenshots or PDFs for print regression testing?

Use PDFs when the output is page-oriented and you care about pagination. Use screenshots for small, isolated print previews, but they are weaker for multi-page documents.

How do I catch @media print regressions early?

Run a test that switches the page to print media, then assert that print-only and screen-only elements behave as expected before generating the artifact.

Is Playwright better than Selenium for print layout testing?

For this specific problem, Playwright is usually simpler because its media emulation and PDF APIs are more direct. Selenium is still viable if your team already standardizes on it.