Sparround

Debugging techniques: trace viewer, UI mode, inspector

Playwright's strongest side is its debugging tooling. The middle-level expectation: investigate with tools, not with console.log.

Trace viewer (npx playwright show-trace trace.zip) — the test's black box. Inside:

  • A step-by-step timeline with per-step durations
  • A DOM snapshot per step — you can go back and actually inspect the page as it was at that moment (hover elements, browse the DOM)
  • A network tab — every request, status and body
  • Console logs, source code, and the action's locator

Its biggest value: investigating a failure that breaks in CI and won't reproduce locally. The trace uploads as an artifact, you open it on your machine and see the actual state — instead of guessing.

UI mode (npx playwright test --ui) — the most comfortable mode for daily work: pick tests, run them, keep watch mode on; time-travel through steps; live locator picking; filters and annotations. Use it as your default mode while writing tests.

The other tools and when each is needed:

  • Inspector (--debug or PWDEBUG=1) — steps through the test, keeps the browser open and lets you try locators live. A page.pause() call stops the test exactly where you want — ideal for investigating the middle of a long flow.
  • The VS Code extension (Playwright Test for VSCode) — run tests from the editor, set breakpoints, use "Pick locator" and "Record new test". In practice the fastest loop: breakpoint + Debug Test.
  • `--headed` — watch the browser; combine with --workers=1, otherwise four windows open.
  • `--repeat-each=20` with --retries=0 — to PROVE a flaky fix. Saying "I fixed it" is weak; showing 20 consecutive passes is an argument.
  • `--last-failed` — reruns only what failed last time; a real time-saver on big suites.
  • `--trace on` — forces trace recording locally (the default on-first-retry only records on retries).

The golden CI set: trace: 'on-first-retry', screenshot: 'only-on-failure', video: 'retain-on-failure' — no artifact bloat on green runs, everything available when something fails.

SymptomFirst toolWhat you look for
Fails in CI, passes locallyTrace viewer (the CI artifact)The DOM snapshot at failure, network statuses, timing
The locator doesn't find the elementUI mode / Inspector "Pick locator"The element's real role/name, whether it sits inside an iframe
A problem mid-way through a long flowpage.pause() + --debugThe page's live state at that exact moment
The test sometimes passes, sometimes fails--repeat-each=20 --retries=0The reproduction rate; proof the fix actually works
The test takes far longer than expectedThe trace viewer timelineWhich step eats the time — usually a hidden wait or a slow request

This question comes up in almost every interview: "a test fails in CI but passes locally — what do you do?" Start your answer with "first step: download and open the trace from the CI artifacts". That one sentence puts you straight into the "knows the tooling" category, whereas an answer opening with guesses ("probably timing") looks weak. The guesses belong in step two, backed by what the trace shows.

📚 Sources and documentation