QA Interview Hub · Knowledge base · Quizzes · Українська

Playwright › Debugging, trace viewer, reports, flaky tests

Local debugging: the tools and when to use which

The local debugging tool depends on the moment: UI mode for writing tests, Inspector for stopping, trace for a failure that already happened. To stop before an action use --debug or page.pause(), and pick a locator with Pick locator or codegen. These need a screen, so on CI without a display the trace is used. Adding pauses instead of looking at the page does not help.

Playwright · Debugging

A test fails on step five. How do you stop right there and check the locator?

A test fails on step five with "element(s) not found". The first four steps work. The easiest move is to add waitForTimeout and run again, but that shows nothing. Playwright has a set of tools, and each answers its own question: what is on the page now, which locator fits, what the library is doing, and what happened while I was not looking. This lesson covers which to take in which situation.

How it works and why. All the tools rest on one thing: the browser is driven through a protocol, so from outside you can stop it, look at a DOM snapshot or try a locator without changing the test. That gives three moments to step in: before the run (record steps, pick a locator), during it (stop and step through) and after it (analyse a recording).

Three moments, three tools

Writing a testUI mode, codegen, VS Code extension Standing on a step--debug, page.pause(), Inspector Already failed, including on CItrace viewer, HTML report
Figure 1. The tool depends on when you step in. On CI only the third is available: there is no interactive screen there.
TaskTool
Write a test and rerun it quicklyUI mode, --ui
Stop before a specific action--debug, page.pause()
See what is happening in the browser--headed, slowMo
Pick a locatorPick locator, codegen
Investigate a failure from CItrace viewer
See what the library itself is doingthe DEBUG=pw:api variable
  • npx playwright test --ui: UI mode. A list of tests, watch mode (rerun when a file changes), time travel through steps with DOM snapshots, network and console, locator picking on a snapshot. The main tool while writing tests.
  • --debug: a shortcut for Playwright Inspector. It turns on headed mode and a single worker and disables the test timeout, so the clock does not stop you. A specific line can be launched like this: npx playwright test login.spec.ts:12 --debug.
  • await page.pause(): a breakpoint in code that opens the Inspector right here.
  • --headed and launchOptions: { slowMo: 300 }: watch the flow at a slowed-down pace.
  • The VS Code extension: running a single test, breakpoints, the Pick locator button with a locator suggestion.

Example: a locator does not find a row. The test fails, and the temptation is one: add a pause.

// before: guessing that the row "has not arrived yet"
await page.goto('/transactions');
await page.waitForTimeout(3000);
await expect(page.getByRole('row', { name: /Transfer/ })).toBeVisible();
Locator: getByRole('row', { name: /Transfer/ })
Expected: visible
Received: <element(s) not found>
Timeout: 5000ms

(The error is shortened.) Instead of a third second of pause, stop the test before the assertion and look at the page:

await page.goto('/transactions');
await page.pause(); // the Inspector opens, then steps by hand

In the Inspector switch on Pick locator and click the row. It suggests getByText('Money transfer'), not getByRole('row', …). That is the diagnosis: the rows in the application are div elements without the row role, so a role locator finds nothing however long it waits. A pause would have changed nothing.

// after: a locator that matches the real markup
const rows = page.getByTestId('tx-row').filter({ hasText: 'Transfer' });
await rows.first().highlight(); // highlight in the browser: is it the one?
await expect(rows).toHaveCount(3);

The highlight() method highlights the element in the open browser, so you check the locator with your eyes before the commit goes further. Remove page.pause() before committing.

Extra diagnostics. DEBUG=pw:api npx playwright test prints every API call, wait and retry of a check to the console: useful when it is unclear what exactly the test "hangs" on. Switch slowMo on with a variable so that a slowed-down run is not committed by accident:

// playwright.config.ts
use: {
  launchOptions: { slowMo: process.env.SLOW ? 300 : 0 },
},

Codegen is useful for more than recording actions:

npx playwright codegen --device="iPhone 13" https://app.test
npx playwright codegen --save-storage=auth.json https://app.test/login
npx playwright codegen --load-storage=auth.json https://app.test/dash

The first command opens the application with device emulation, the second saves the state after a manual login, the third opens it already logged in. The generated code is not committed as is: you take the locators from it and rewrite the structure into fixtures and a Page Object.

A flaky test and slow motion

Typical

Debug a flaky test headed with slowMo

Slowing down gives the application time to load everything. The race that fails on a fast CI disappears and the test "works".

Better

Repeat at CI speed and take a trace

A headless run with --repeat-each and a recorded trace reproduces the real conditions and keeps a record of the failed attempt.

Figure 2. Stopping tools show a state but do not reproduce races: instability needs repeats and a recording.

Limits and alternatives. The Inspector, UI mode and headed mode need a screen, so they are unavailable on an ordinary CI without a display: there the trace and other artifacts work. Headed and headless behave almost the same, but differences happen (window size, fonts, rendering), so to investigate a CI failure it is more reliable to reproduce the environment with the same Docker image. In Cypress the analogue of UI mode is the interactive runner with time travel, in Selenium you stop with an ordinary IDE debugger, and there is no DOM snapshot "before" and "after" each action.

Common mistakes.

  • Committing page.pause(). In headless mode the call does nothing, but in a headed run (locally or on CI with a virtual display) it stops the test, so it stays a trap. A linter rule for this call (for example no-restricted-syntax) catches such cases.
  • Debugging a flaky test only headed with slowMo: slow motion hides races that show up on a fast CI.
  • Trusting a codegen selector without checking: it may hold on to text or structure that changes.
  • Adding pauses instead of looking at the page: "element not found" is more often a locator problem than a timing one.

Check yourself

The Inspector did not open for page.pause(): the test passed on CI and nothing stopped. Why?

Answer

page.pause() works only in headed mode. In a headless run (an ordinary CI) the call is ignored. That is why a trace, not a pause, is used to investigate a CI failure.

Pick locator suggested getByText('Money transfer'), while the test uses getByRole('row', { name: /Transfer/ }) and fails with "not found". What does that say about the page?

Answer

The element exists, but most likely it has no row role (it is a div without ARIA markup). Waiting will not help here: a role locator will never find it. Change the locator (a test id with filter) or add semantics to the application.

In an interview. Junior: While writing tests I work in UI mode, and to stop on a step I use page.pause() or --debug and pick a locator with Pick locator. Middle/Senior: The tool depends on the moment: UI mode for writing, the Inspector for stopping, the trace for what has already failed on CI, where there is no screen. For flaky tests I do not rely on headed and slowMo, because they hide races: I repeat at CI speed with --repeat-each and a recorded trace. I rewrite code from codegen into fixtures and take only the locators from it.

More articles in this chapter