Prompt
How do I get screenshots and videos from failed browser tests?
Latest observation
To capture screenshots and videos from failed browser tests, you usually need two things:
- Enable screenshot/video recording in your test runner or browser automation tool
- Configure it to save artifacts when a test fails
The exact setup depends on the tool you’re using. Here are the most common approaches:
Playwright
Playwright makes this pretty easy.
Screenshots on failure
import { test } from '@playwright/test';
test('example', async ({ page }) => {
await page.goto('https://example.com');
// test steps...
});
In playwright.config.ts:
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
screenshot: 'only-on-failure',
video: 'retain-on-failure',
},
});
What this does
screenshot: 'only-on-failure'saves screenshots only when a test failsvideo: 'retain-on-failure'keeps video only for failed tests
Artifacts are usually stored in the test output folder and can be viewed in the HTML report.
Cypress
Cypress can automatically capture screenshots and videos.
Screenshots
Cypress takes screenshots on failure by default if configured properly.
Videos
Video recording is enabled in cypress.config.js:
const { defineConfig } = require('cypress');
module.exports = defineConfig({
video: true,
screenshotOnRunFailure: true,
});
Notes
- Videos are recorded for test runs in
cypress run - Screenshots on failure are saved automatically
- You can customize output folders
Selenium / WebDriver
Selenium doesn’t record video by itself, but you can:
Take screenshots on failure
Example in Java:
if (testFailed) {
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// save file to disk
}
Record video
For video, you typically need:
- a grid service that supports recording, such as Selenium Grid, BrowserStack, Sauce Labs, or Selenoid
- or use a separate screen-recording tool/container setup
BrowserStack / Sauce Labs / cloud testing services
These usually provide:
- automatic screenshots
- session video recordings
- logs and console output
You generally just need to:
- enable video in the service config
- mark artifacts to keep on failure
- inspect the dashboard after the run
General pattern for any test framework
If you’re building this yourself, the logic is:
-
Before each test
- start browser
- initialize screenshot/video recording if needed
-
On test failure
- capture current screenshot
- stop recording and save video
- attach files to test report
-
On test success
- optionally delete artifacts to save space
Best practices
- Save artifacts with test name + timestamp
- Keep only failed-test videos to reduce storage
- Attach artifact links in CI reports
- Preserve browser console logs alongside screenshots/videos
If you tell me which framework you’re using — Playwright, Cypress, Selenium, Puppeteer, WebdriverIO, etc. — I can give you the exact config and code.