Playwright Screenshots and Videos

A car dashcam records the road so drivers can review an incident later. Screenshots and videos give tests the same memory. A photo captures one moment, and a video captures the whole journey, so a failed test leaves visual evidence that anyone can inspect without rerunning it.

Evidence Types

Screenshot   one still picture       quick glance at the final state
Video        full recording          shows the path to the failure
Trace        interactive timeline    deep investigation

Automatic Screenshots

The config file tells Playwright when to take pictures.

export default defineConfig({
  use: {
    screenshot: 'only-on-failure',
  },
});
  • off takes no pictures.
  • on takes one at the end of every test.
  • only-on-failure takes one after a failed test.

Manual Screenshots

A test can capture a picture at any moment.

await page.screenshot({ path: 'screens/home.png' });
await page.screenshot({ path: 'screens/home-full.png', fullPage: true });

The fullPage option scrolls the page and captures the whole length.

Capturing One Element

await page.getByTestId('price-card').screenshot({ path: 'screens/price.png' });

Hiding Private Data

Pages often show names, emails, or account numbers. The mask option paints those areas with a solid color before saving the picture.

await page.screenshot({
  path: 'screens/profile.png',
  mask: [page.getByTestId('email'), page.getByTestId('phone')],
});

Recording Videos

export default defineConfig({
  use: {
    video: 'retain-on-failure',
  },
});
  • on records and keeps every video.
  • retain-on-failure records every test and keeps only the failures.
  • on-first-retry records the second attempt of a failed test.

Video size follows the screen size. A custom size reduces file weight.

use: {
  video: { mode: 'retain-on-failure', size: { width: 640, height: 480 } },
}

Where Files Land

Playwright stores artifacts in the test-results folder, with one subfolder per test. Each folder holds that test's screenshots, videos, and traces.

Attaching Pictures to the Report

A test can attach a screenshot so it shows inside the HTML report.

test('pricing page', async ({ page }, testInfo) => {
  await page.goto('/pricing');
  const image = await page.screenshot();
  await testInfo.attach('pricing', { body: image, contentType: 'image/png' });
});

A Real-World Scenario

A nightly run of three hundred tests ends with four red results. The engineer opens the report, watches four short videos over a cup of tea, and spots the cause in the first clip: a promotional pop-up covers the checkout button. One fix to the pop-up timing repairs all four tests.

Cost Awareness

Videos grow quickly across hundreds of tests. Choose retain-on-failure and delete artifacts after a set number of days. Keep pictures lean by capturing only the elements that matter.

Quick Practice Task

Enable only-on-failure screenshots and retain-on-failure videos. Write a test that asserts a wrong title on purpose. Open the test-results folder and view the saved picture and clip.

Common Questions

Why is a screenshot blank?

The test may capture before the page finishes drawing. Assert a visible element first, and capture after that check passes.

Can screenshots serve as baselines?

Plain screenshots only record evidence. Baseline comparison uses toHaveScreenshot, covered in the visual comparison topic.

How can a team save storage?

Keep artifacts for failures only, shrink video size, and delete old runs on a schedule.

Key Takeaways

  • Screenshots show one moment, and videos show the full path.
  • Config options decide when Playwright saves each artifact.
  • The mask option hides private information.
  • Failure-only settings keep storage small.