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

Playwright › Debugging, trace viewer, reports, flaky tests

Diagnostics: trace viewer, reports, flaky tests

A CI failure is investigated with a trace: a trace.zip archive with an action log, DOM snapshots, network, console and the test source. A snapshot is restored as a live page, so a locator and server responses can be checked. on-first-retry is cheap but records only the first retry, so retain-on-failure is used for flaky tests. A trace is not attached to public tickets without cleaning.

Playwright · Debugging

A test is red on CI, green locally, and the log is one line. What can you learn from it?

Payment passes on a laptop and fails on CI. The log has a single line: "Timeout 5000ms exceeded". The report screenshot explains nothing: the button is there, the order status says "Processing". Without a record of what happened you can only guess. This lesson covers what Playwright records, how to keep it and read it, and how to tell a flaky test from a broken application.

How it works and why. The browser on CI dies together with the test, there is no screen, and the failure cannot be repeated on demand. So Playwright records not a video but a trace: a trace.zip archive with an action log and page snapshots. For every action (click, fill, navigation, assertion) it stores DOM snapshots before, during and after it, plus network requests with response bodies, console messages, page errors and the test's source code with the line highlighted. A snapshot is rebuilt from the recorded DOM and resources, so it is a live page: you can hover, open DevTools, copy text and check a locator on the Locator and Pick locator tabs. A video cannot do that: you cannot click it or see what the server answered.

Recording is not free: DOM snapshots slow a run down and take space. So the trace mode decides when to record and what to keep:

Where a trace comes from with retries

Attempt 1 failsno trace is recorded yet Retry 1: trace is onactions, DOM and network recorded test-results/…/trace.zipnext to the screenshot and error retry is greenflaky mark retry is redfailed + trace
Figure 1. The on-first-retry mode: the first attempt stays without a trace, recording starts with the first retry.
Trace modeWhat happensWhen to use
on-first-retryRecords only the first retryCI with retries, the cheapest option
retain-on-failureRecords every attempt, deletes green onesHunting flakiness, when the first failed attempt is what you need
on-all-retriesRecords every retryWhen the second retry fails too
onRecords and keeps everythingLocal investigation of a single test

Example: from a log line to the cause. A config with no artifacts, and a test that fails only on CI:

// before: no retries and no trace, only the error text
export default defineConfig({
  retries: 0,
});
Error: expect(locator).toHaveText(expected) failed

Locator: getByTestId('order-status')
Expected string: "Paid"
Received string: "Processing"
Timeout: 5000ms

(The error is shortened.) It shows that the assertion waited five seconds and the text did not change, but not why. Turn the artifacts on:

// after
export default defineConfig({
  retries: process.env.CI ? 2 : 0,
  use: {
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
  },
});

After the run, open the trace from the HTML report (npx playwright show-report) or from a file: npx playwright show-trace test-results/…/trace.zip. The same file can be loaded at trace.playwright.dev: the viewer runs in the browser, and the data is not sent to a server. Read top to bottom. In the action log the click on "Pay" is green. On the Network tab the POST to the payment endpoint went out at once, and the response arrived about seven seconds later (in this example). The assertion waits only five. So the test is not "wrong" and the locator is right: the application on CI is slower than the wait limit.

// fix: wait for the concrete response, then check the UI
const paid = page.waitForResponse(
  (r) => r.url().includes('/api/payments') && r.ok(),
);
await page.getByRole('button', { name: 'Pay' }).click();
await paid;
await expect(page.getByTestId('order-status')).toHaveText('Paid');

A longer pause or waitForTimeout would be worse: the first hides the cause, the second wastes time on fast runs. And a seven-second response is itself a signal for the backend team.

What to do with a rerun

Typical

Rerun and forget

The pipeline is green, the cause is unknown. Next week it fails again, and the report only says flaky.

Better

A retry as a signal

The retry saves the pipeline, while the flaky mark and the trace become a task: read the record and fix the cause.

Figure 2. Retries exist so that one glitch does not stop the team, not to hide instability.

How to read a trace by symptom.

SymptomWhere to look
Element not foundThe "before" snapshot on the action tab: is the element there, did a modal cover it, is the text different (language, case)
An assertion did not get its resultNetwork: was the request sent, with what status and how long it took; Console: was there an error in the application
A click did not workThe action log: which wait check failed (covered, disabled, moving)
Wrong dataThe response body on the Network tab and the Source tab with the test line

Anatomy of an error: a click on a disabled button

1Error: locator.click: Test timeout of 30000ms exceeded.
Call log:
2- waiting for getByRole('button', { name: 'Pay' })
  - locator resolved to <button disabled class="pay">
3- waiting for element to be visible, enabled and stable
  - element is not enabled
  - retrying click action
4at checkout.spec.ts:24:31
  1. What: click did not fit into the test limit, 30 s. That is the consequence, not the cause.
  2. Element found: the locator worked, no need to change it.
  3. Why: the button stayed disabled and Playwright kept waiting for it to become enabled.
  4. Where: line 24 of the test. Start here: what was supposed to enable the button?

What to do

Find out why the form is not ready (an empty field, a request still running) and check it explicitly: await expect(pay).toBeEnabled(). Not force: true: it hides the bug.

Figure 3. The real error text (shortened): the numbers lead to the explanations.

Limits and alternatives. A trace does not see server logs or database state: if a request returned 500, the cause is on the backend, and the trace only proves it. For flakiness on the very first attempt on-first-retry is not enough: if the first attempt failed and the retry passed, the trace is of a green run and the failed attempt is not visible. Then take retain-on-failure. A trace may contain cookies, authorization headers and response bodies, so do not attach it to public tickets without cleaning. In Cypress a similar role is played by Test Replay in Cypress Cloud (the run's DOM, network and console), in Selenium artifacts are collected by hand (screenshots, driver logs): there is no ready record of actions with the DOM.

Reports. The built-in HTML one is for people, list and dot for the console, JUnit for CI systems, github for annotations in a PR, blob for shards, and Allure works through a separate package. A test that passed only after a retry is marked flaky by the HTML report. The full set of artifacts and reporter settings is in the lesson “Artifacts and reports”.

Common mistakes.

  • on-first-retry without retries: there are no retries and no trace, and you have to guess the cause.
  • trace: 'on' in a large suite: gigabytes of artifacts and a slower CI.
  • Not uploading the report as a CI artifact (a step without if: always() or !cancelled()): after a failure there is neither a report nor a trace.
  • Treating a retry as the fix: the test is green but the instability stays and returns at the worst moment.
  • Adding secrets to report attachments: the whole team sees them.

Check yourself

The config has trace: 'on-first-retry' and retries: 0. A test failed on CI. Will there be a trace?

Answer

No. The mode records only the first retry, and there are no retries, so nothing is recorded. Either turn retries on or take retain-on-failure, which records every attempt and keeps only the failed ones.

A test is marked flaky: attempt 1 failed, attempt 2 passed. With on-first-retry you open the trace. What is in it and why may that not be enough?

Answer

The trace belongs to the first retry, which passed, so it holds a green run. Failed attempt 1 has no recording: only the error text and a screenshot. To study the instability itself you need retain-on-failure, which keeps the trace of every failed attempt.

In the trace the click on "Pay" is green, but on the Network tab the payment response has status 500, and the assertion waited for "Paid". Is the cause in the test or in the application?

Answer

In the application or the environment: the test clicked correctly and waited correctly, and the backend returned an error. Raising the timeout is useless. Next you look at the response body, the server logs and the data the test used.

In an interview. Junior: I investigate a CI failure through the trace: it shows every action, page snapshots, network and console. On CI I set trace: 'on-first-retry' together with retries. Middle/Senior: I pick the mode for the task: on-first-retry is cheap, but for a flaky test it gives only the green retry, so for an investigation I take retain-on-failure. I read the action log, then Network and Console, and that is how I tell a slow backend from a bad locator. I do not count retries as a fix: I watch the flaky mark and fix the cause. A trace may contain tokens, so I do not add it to public tickets without cleaning.

More articles in this chapter