DEV Community

Styrow.dev
Styrow.dev

Posted on Originally published at styrow.dev

How would you systematically debug a flaky E2E test in Playwright that only fails intermittently in CI, leveraging Playw

🚨 Flaky Tests Haunting Your CI?
How do you systematically debug those intermittent Playwright E2E failures that only show up in CI, leveraging Playwright's own power?

📌 Problem Statement
Flaky end-to-end tests are a nightmare, often passing locally but failing randomly in CI pipelines. These intermittent failures waste countless developer hours and erode confidence in your test suite. Identifying the root cause requires deep visibility into the CI environment's execution.

💡 Solution & Code Walkthrough
Playwright offers robust built-in features to transform your CI into a powerful debugging station. The key is to configure it for maximum observability on failure. This ensures you capture crucial diagnostic information like traces, videos, and screenshots exactly when a test misbehaves.

• Maximum Observability: Configure playwright.config.ts to automatically collect rich diagnostic data.
• Retries: Use retries in CI not just to "pass" flaky tests, but to capture artifacts from the first failure attempt.
• on-first-retry: This powerful setting captures traces and videos only on the initial failed attempt before a retry potentially passes, isolating the problematic state.

// playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  // ✅ Configure retries specifically for CI to gather more data
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : undefined, // Optional: reduce workers for clearer artifact collection

  use: {
    // 🎥 Capture 'time travel' traces for deep debugging
    trace: 'on-first-retry',
    // 🎬 Record video of test runs for visual context
    video: 'on-first-retry',
    // 📸 Grab a screenshot precisely when the test fails
    screenshot: 'only-on-failure',
    // ... other browser context settings
  },
  // ... rest of your Playwright config
});
Enter fullscreen mode Exit fullscreen mode

After a CI failure, access the generated HTML report. It provides direct links to the trace viewer, videos, and screenshots, allowing you to "time-travel" through the test's execution and pinpoint the exact moment of failure.

🔑 Key Takeaways
• ✅ Proactive CI Configuration: Set up Playwright's diagnostic tools before a failure occurs.
• ✅ Trace Viewer: Playwright's Trace Viewer is your most powerful ally for deep debugging, showing actions, network requests, DOM snapshots, and console logs.
• ✅ on-first-retry: Crucial for capturing artifacts from the initial failure, providing insight into why the test was flaky.

❓ Quick Summary Q&A
• Q: What is the primary goal when debugging flaky tests in CI?
A: Maximize observability by capturing diagnostic artifacts like traces, videos, and screenshots.
• Q: Why use trace: 'on-first-retry'?
A: It ensures you capture data from the initial failure of a flaky test, even if a retry later passes.

TAGS: playwright, e2e testing, debugging, ci/cd, automation, flaky tests, typescript

────────────────────────────────────────
Level up your skills on the go! Download our app:
────────────────────────────────────────

📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:

🤖 𝐆𝐨𝐨𝐠𝐥𝐞 𝐏𝐥𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐢𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261008

🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261008&mt=8

────────────────────────────────────────
────────────────────────────────────────

Top comments (0)