Prompt
How do I capture screenshots and videos on test failures
Latest observation
You usually do this in your test framework by adding a “failure hook” that:
- detects when a test fails
- captures a screenshot and/or video
- attaches the artifact to the test report or saves it to disk
The exact setup depends on the tool you’re using. Here are the common patterns.
Cypress
Screenshots
Cypress can automatically take screenshots on failure if configured:
// cypress.config.js
module.exports = {
screenshotOnRunFailure: true,
video: true,
}
Videos
Cypress records video for the whole spec run when video: true is enabled.
Tips
- Screenshots are saved in
cypress/screenshots - Videos are saved in
cypress/videos - You can also manually call
cy.screenshot()in a failing test or hook
Playwright
Playwright can capture screenshots and video automatically for failing tests.
Configure in playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
screenshot: 'only-on-failure',
video: 'retain-on-failure',
},
});
Behavior
screenshot: 'only-on-failure'saves a screenshot only when the test failsvideo: 'retain-on-failure'keeps the recording only for failed tests
Selenium / WebDriver
You typically do this manually in your test framework after a failure.
Example idea
- In an
afterEach/ teardown hook, check if the test failed - If so, call the driver’s screenshot API and save the image
Java example
if (testFailed) {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Files.copy(src.toPath(), Paths.get("screenshots/failure.png"));
}
Videos
Selenium itself does not provide video recording.
You’d usually use:
- a cloud grid provider with video recording, or
- a separate screen-recording tool in CI
Jest / Mocha / Puppeteer
Screenshots
Take a screenshot in a failure handler or catch block.
afterEach(async () => {
if (expect.getState().currentTestName && testFailed) {
await page.screenshot({ path: `screenshots/${Date.now()}.png` });
}
});
Videos
Puppeteer does not have built-in simple test video capture; people usually use:
- CI screen recording
- Docker/Xvfb recording
- a cloud browser service
General approach
If your framework doesn’t support this out of the box:
- Add a teardown hook
afterEach,afterTest,onTestFailure, etc.
- Detect failure
- framework-specific test status or exception
- Capture screenshot
- using the browser driver API
- Capture video
- either built-in framework support or external recording
- Store artifacts
- save locally, upload to CI artifacts, or attach to test reports
Best practice
- Save artifacts with the test name and timestamp
- Keep only failure artifacts to avoid huge storage use
- Upload screenshots/videos in CI so they’re easy to inspect
- If possible, include logs and DOM snapshots too
If you tell me which framework you’re using — for example Cypress, Playwright, Selenium, Appium, Jest, or Robot Framework — I can give you the exact code/config for screenshots and videos on failure.