๐งฉ Module 9 โ Reports & Debugging
Playwright is built for fast debugging. From rich HTML reports to trace viewer, screenshots, videos, and the Playwright Inspector โ you have everything you need to diagnose failures quickly.
โ 1) Generate & View HTML Reportโ
After running tests, Playwright generates an HTML report.
# Run tests (with traces enabled for best debugging)
npx playwright test --project=chromium --trace=on
# Open the HTML report
npx playwright show-report
โ The HTML report shows:
- โ Passed / failed / flaky tests
- โ Duration per test
- โ Direct links to traces, screenshots, videos
โ 2) Trace Viewer (Most Powerful Debugging Tool)โ
Traces capture everything: DOM snapshots, network requests, console logs, actions, and more.
- From HTML report: Click any test โ click Trace
- From CLI: Open a specific trace file:
npx playwright show-trace test-results/<test-folder>/trace.zip
๐ฏ In Trace Viewer you can:
- โช Step through each action (click/fill/navigation)
- ๐ Inspect DOM snapshots at each step
- ๐ Review network requests/responses
- ๐งพ Check console logs
โ 3) Screenshots, Videos & Error Contextโ
Configured in playwright.config.js:
use: {
screenshot: 'only-on-failure',
trace: 'on-first-retry',
video: 'on-first-retry',
}
๐ธ Screenshots: Saved on failures
๐ฅ Videos: Recorded on first retry (great for flaky tests)
๐งพ Error Context: Playwright writes error-context.md files with extra info
โ
4) Debug Mode (--debug)โ
Step through a test line-by-line with the Playwright Inspector:
npx playwright test tests/login.spec.js --project=chromium --debug
- ๐งญ Use the step button to execute one action at a time
- ๐ Inspect locators live in the browser
- โ๏ธ Edit the test on the fly and rerun
โ
5) PWDEBUG=1 (Terminal Debugging)โ
An alternative debug mode that prints detailed logs in the terminal:
PWDEBUG=1 npx playwright test tests/login.spec.js --project=chromium
Useful when the Inspector UI doesn't attach (permissions, headless quirks, CI).
โ 6) Common Debugging Scenarios & Fixesโ
| Problem | Likely Cause | Fix |
|---|---|---|
Test times out on click() / fill() | Locator too strict / element not ready | Use getByRole() / getByLabel(), add waitFor() / waitForLoadState() |
toBeVisible() fails | Element is in DOM but hidden / off-screen | Use scrollIntoViewIfNeeded() or verify visibility condition |
| Flaky test (intermittent) | Race condition / animations / network | Add waitForURL(), waitForResponse(), increase timeouts carefully |
--debug shows "about:blank" | Browser/DevTools connection issue | npx playwright install chromium, close all browser windows, try PWDEBUG=1 |
Port conflict for report (EADDRINUSE) | Another process using report port | lsof -i :9323 โ kill -9 <PID> (macOS/Linux) or netstat / taskkill (Windows) |
โ 7) Best Practices for Debuggingโ
- โ
Always enable trace on failure:
trace: 'on-first-retry' - โ
Run locally with
--headedfirst:npx playwright test --headed - โ
Use
data-testidin your app for the most stable locators - โ Keep tests small & focused: One behavior per test = easier to isolate failures
- โ
Write meaningful test names:
test('Login fails with invalid password')is easier to debug thantest('test 3')
๐ฏ Summary: Playwright gives you a complete debugging toolkit โ HTML report + trace viewer + screenshots/videos + Inspector + logs โ so you can fix failures in minutes, not hours.
๐ Keep Goingโ
Apply what you learned in this module:
- ๐งช Practice Exercises โ hands-on tasks to build real skills
- ๐๏ธ Real Projects โ real-world automation examples