Can Playwright capture videos during test execution?
Yes. Playwright can record a video of every page a test opens, as a .webm file. In Playwright Test you switch it on with one config option; when you use the library directly, you ask for it when creating a browser context. Recording works in Chromium, Firefox and WebKit, and it works in headless mode, which is the usual case on CI.
Turning it on in Playwright Test
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
retries: process.env.CI ? 2 : 0,
use: {
video: 'retain-on-failure',
},
});
The value decides which runs keep their video:
| Mode | Keeps a video for |
|---|---|
'off' | Nothing (the default) |
'on' | Every test |
'retain-on-failure' | Failed tests only; passing tests' videos are deleted |
'on-first-retry' | The first retry of a failed test |
'on-all-retries' | Every retry |
'retain-on-first-failure' | The first failed run only |
retain-on-failure is a good default for CI: you get evidence for failures without filling the disk with videos of passing tests. Recent versions list a few more combinations; check the video option in the docs for your version.
Smaller files: set the size
// in a spec file, or under use: in the config
test.use({
video: { mode: 'on', size: { width: 640, height: 360 } },
});
By default the video is scaled to fit within 800 × 800 pixels. A smaller size keeps artefacts light when you upload them from CI.
Where the files go
Each test gets a folder under test-results/, named after the file, test and project, and the video is saved there as video.webm. In a test run with video: 'on', the folder for a test named "vid" in video.spec.ts was test-results/video-vid-chromium/. The HTML report (npx playwright show-report) embeds the video next to the error, so most people never open the folder.
Recording without the test runner
// record.mjs (ES module, so top-level await works)
import { chromium } from 'playwright';
const browser = await chromium.launch();
const context = await browser.newContext({
recordVideo: { dir: 'videos/', size: { width: 640, height: 360 } },
});
const page = await context.newPage();
await page.goto('https://example.com');
const video = page.video();
await context.close(); // the file is finished only now
console.log(await video?.path()); // videos/page@<id>.webm
await video?.saveAs('videos/home.webm'); // copy with a readable name
await browser.close();
Gotchas
- The video is complete only after the context closes. The test runner handles this. In your own scripts, call
context.close()before reading or copying the file. - One video per page. A test that opens a popup or a second tab produces a second video.
- Videos are not a debugger. A video shows what happened but not why. A trace adds DOM snapshots, network calls and console logs for every step; see tracing in Playwright.
- Missing ffmpeg. Video encoding uses an ffmpeg build that Playwright downloads with
npx playwright install. A stripped-down CI image can miss it; reinstall the browsers. - Storage on CI. Upload
test-results/or the HTML report as an artefact only when the job fails, or with a short retention period.
Related: screenshots in Playwright, recording tests with codegen (a different kind of "recording") and browser isolation.
Want to master Playwright hands-on?
Join Uncodemy's Playwright Automation Testing Course in Noida — Dedicated Placement Support.
Explore the Course →