Capturing Screenshots and Videos with Playwright

Capturing Screenshots and Videos with Playwright

Playwright is a powerful end-to-end testing tool that supports multiple browsers and languages. One of its most useful features is the ability to capture screenshots and videos, which helps developers and testers debug test failures more effectively.


In this blog, we’ll walk through how to take screenshots and record videos using Playwright, with examples in JavaScript.


📸 Why Capture Screenshots and Videos?

Capturing screenshots and videos during automated tests can help you:


Debug failures by seeing exactly what went wrong


Document test flows for stakeholders or team members


Track visual changes in the UI across builds


Monitor flaky tests by watching what actually happened


🧰 Prerequisites

Make sure you have Playwright installed:


bash

Copy

Edit

npm init -y

npm install --save-dev playwright

To install browsers:


bash

Copy

Edit

npx playwright install

🖼️ Taking Screenshots with Playwright

You can capture full-page or element-specific screenshots in Playwright.


✅ Full Page Screenshot

javascript

Copy

Edit

const { chromium } = require('playwright');


(async () => {

  const browser = await chromium.launch();

  const page = await browser.newPage();

  await page.goto('https://example.com');

  

  await page.screenshot({ path: 'fullpage.png', fullPage: true });

  await browser.close();

})();

✅ Element Screenshot

javascript

Copy

Edit

const element = await page.$('h1');

await element.screenshot({ path: 'heading.png' });

🎥 Recording Videos in Playwright

To record videos, you need to enable video recording in the context options.


javascript

Copy

Edit

const { chromium } = require('playwright');

(async () => {

  const browser = await chromium.launch();

  const context = await browser.newContext({

    recordVideo: {

      dir: 'videos/', // Directory to save the video

    }

  });

  const page = await context.newPage();

  await page.goto('https://example.com');

  await page.waitForTimeout(3000); // Just to capture something

  await browser.close(); // Automatically saves video

})();

📂 The recorded video will be saved in the specified videos/ directory after the browser is closed.

📝 Best Practices

Capture on failure: You can set up Playwright to automatically capture a screenshot when a test fails.

Clean up artifacts: Automatically delete or archive screenshots/videos from successful runs to save space.

Use in CI/CD: Combine screenshots/videos with your CI pipelines for test reporting.

🛠️ Example: Capture Screenshot on Failure (Jest + Playwright)

javascript

Copy

Edit

afterEach(async () => {

  if (expect.getState().currentTestName && expect.getState().currentTestName.includes('failed')) {

    await page.screenshot({ path: `error-${Date.now()}.png` });

  }

});

📌 Summary

Capturing screenshots and videos in Playwright is simple and extremely useful. Whether you're troubleshooting a bug or documenting test runs, this feature gives you the visual context you need.

✅ Use cases:

Full-page and element screenshots

Test flow videos

Automatic capture on failure

With just a few lines of code, you can make your test automation suite more powerful and insightful.

Learn Playwright Training Course in Hyderabad

Read More

Working with Alerts and Pop-ups in Playwright

Handling Frames and Iframes Using Playwright

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Tosca for API Testing: A Step-by-Step Tutorial

Waits in Playwright: Explicit, Implicit, and Auto