Prompt
How do I capture screenshots and videos during automated browser runs?
Latest observation
You can capture screenshots and videos in automated browser runs in a few common ways, depending on the tool you’re using. Here are the main approaches and examples.
1) Playwright
Playwright has built-in support for both screenshots and video.
Screenshot
await page.screenshot({ path: 'screenshot.png', fullPage: true });
Video
Enable video when creating the browser context:
const context = await browser.newContext({
recordVideo: { dir: 'videos/' }
});
const page = await context.newPage();
After the test/page finishes:
await page.close();
const videoPath = await page.video().path();
Notes
- Video is saved when the page/context closes.
- You typically set the video directory and then retrieve the file after the run.
2) Cypress
Cypress can take screenshots and record videos during test runs.
Screenshot
cy.screenshot('my-screenshot');
Video
Configure it in cypress.config.js:
module.exports = {
video: true
};
Then run in headless mode:
npx cypress run
Notes
- Video recording is mainly for
cypress run, not interactiveopenmode. - Screenshots can be taken on demand or automatically on test failure.
3) Selenium
Selenium supports screenshots directly. Video usually requires external tooling.
Screenshot
In Java:
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
In Python:
driver.save_screenshot("screenshot.png")
Video
For video, common options include:
- recording the screen with tools like ffmpeg
- using a browser/grid service that provides session recordings
- using Selenium Grid providers with built-in video capture
Example with ffmpeg:
ffmpeg -video_size 1366x768 -framerate 25 -f x11grab -i :0.0 output.mp4
4) Puppeteer
Puppeteer supports screenshots and screencast-style recording via Chrome DevTools Protocol, but not as directly as Playwright.
Screenshot
await page.screenshot({ path: 'screenshot.png', fullPage: true });
Video
Usually done through:
- external screen recording tools
- Chrome DevTools Protocol screencast frames
- test runners/plugins that wrap recording
5) Practical tips
- Capture screenshots on failure automatically.
- Save files with timestamps or test names.
- Store artifacts in CI so they’re easy to download.
- If you need video, prefer frameworks with native support like Playwright or Cypress.
Example naming
const name = `artifacts/${testName}-${Date.now()}.png`;
await page.screenshot({ path: name });
If you tell me which framework you’re using—Playwright, Cypress, Selenium, Puppeteer, or something else—I can give you exact code for that setup.