QA AND TEST AUTOMATION · PART 5 · UI WEB AUTOMATION · PLAYWRIGHT · 51 OF 112

Playwright Trace Viewer, Inspector and Codegen

Debugging and recording tools in Playwright: a real failing ShopRung test (search-case bug: expected "2 products", received "0 products"), what the error, call log and error-context file say, the trace viewer timeline with DOM snapshots and network calls (including GET /api/products?q=mug), trace settings for CI, the inspector and pause, and codegen for recording a first draft of a test, with its limits.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 13

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Error + call log
  • traces on retry or failure
  • snapshots and network
  • inspector and pause
  • codegen drafts

Notes

A real failure

We switched on ShopRung's search case bug, which makes search case sensitive, and ran the mug search test. It failed, and the output already says a lot. The locator, the result count. Expected two products, received zero products. The call log shows the locator was found fourteen times during the five second retry, always with zero products, so this is not a timing problem. It points at line nine, and it saved a trace file.

Terminal output
Error: expect(locator).toHaveText
  (expected) failed
Locator:  getByTestId('result-count')
Expected: "2 products"
Received: "0 products"
Timeout:  5000ms
Call log:
  14 × locator resolved to
  <span ...>0 products</span>
>  9 | await expect(page.getBy...
attachment: trace.zip

Recording traces

Traces are controlled by the trace setting. On first retry records only when a test is retried, which is cheap and the common default for CI. Retain on failure records every test but keeps only failures. On records everything, useful while debugging. You can also pass trace on from the command line, as we did. In CI, upload the test results and report folders as build artifacts, so anyone can download the trace.

Trace settings
use: { trace: "on-first-retry" }
  record only when retried
"retain-on-failure"
  record all, keep failed only
"on"  always (slow, debugging)
"off"

CLI: npx playwright test --trace on
CI: upload test-results/ and
  playwright-report/ as artifacts

The viewer

Open the trace with show trace and the file path, or drop it onto the trace viewer web page, which runs locally in your browser. On the left, every action and assertion in order, with timings. In the centre, a real DOM snapshot of the page before, during and after each step; you can inspect it like a live page. Tabs show network calls, console messages, the source line and errors. It is like a flight recorder for the test.

Opening a trace
npx playwright show-trace \
  test-results/.../trace.zip
or drag it onto
  trace.playwright.dev
  (runs in your browser)

left: each action and assert
centre: DOM snapshot, before /
  action / after
tabs: Network, Console,
  Source, Errors, Attachments

Reading it

Reading our trace. The fill and the click worked. The after snapshot shows results for mug with no cards. The network tab shows the page called get products with q equals mug, and the API answered a count of zero. So the UI displayed exactly what the API returned; the bug is in the search API's case handling. That is a real defect to report, with the trace attached, not a flaky test to re run.

What our trace showed
  • fill "mug" → click Search
  • snapshot: page says "Results for “mug”", 0 cards
  • Network: GET /api/products?q=mug → {"count": 0, "items": []}
  • so: the UI is fine; the API search is wrong (case) → a real bug, raise it against the search API

Other evidence

Other evidence. Screenshot only on failure and video retain on failure add pictures and recordings to the report. Recent Playwright versions also write an error context file for each failure: the error, the call log and a text snapshot of the page's accessibility tree. It is designed to be pasted into an AI assistant or a bug report, and Part eleven builds on that idea.

More failure artifacts
use: {
  screenshot: "only-on-failure",
  video: "retain-on-failure",
}

error-context.md (written on
  failure): the error, the call
  log and a page snapshot
  (accessibility tree as text)
  ready to paste into an AI
  assistant or a bug report

The inspector

To debug live, run with debug. The inspector opens with the test paused before the first step, and you step over actions one by one while the target element is highlighted in the browser. To stop at one place, put await page pause in the test. UI mode, from the last episode, combines a runner, the timeline and a locator picker, and it re runs tests when you save.

Live debugging
npx playwright test --debug
  opens the Inspector, paused
  step over each action
  highlights the locator target

await page.pause();
  pause at this line only

UI mode (--ui): pick locator,
  watch mode, timeline

Pick a locator

The pick locator button lets you hover over any element and see the locator Playwright would use. It follows the same priority as the locators episode: role and name first, then labels, text and test ids, CSS last. It is a fast way to learn and a good starting point. You still decide whether it is stable, matches exactly one element, and describes meaning rather than position.

Locator picker
  • hover an element in the inspector or UI mode
  • it suggests the best locator it can find, in priority: role + name, label, text, test id, then CSS
  • you still decide: is it stable? exactly one match? meaningful?

Codegen

Codegen opens a browser and a code window side by side. Run it with ShopRung's address, then use the shop normally: search for mug, open a product, log in. Each action appears as code with good locators. The toolbar can add assertions for visibility, text or values by clicking an element. It is great for exploring a new page, learning the API, and getting a first draft quickly.

Record a draft
npx playwright codegen \
  http://localhost:8080

you click and type, it writes:
await page.getByRole("searchbox",
  { name: "Search products" })
  .fill("mug");
await page.getByRole("button",
  { name: "Search" }).click();
toolbar: add assertions
  (visible, text, value)

Its limits

But a recording is a draft, not a finished test. It has no data reset, logs in through the UI every time, has few assertions, and repeats the same locators in every file. Turn recordings into real tests: set up data through the API and fixtures, move locators into page objects or helpers, and add specific assertions about the behaviour you care about. Record to start; engineer to finish.

Then refactor
  • recordings have: no reset or test data logins in every test few or no assertions repeated locators
  • turn them into: API setup + fixtures page objects or helpers clear, specific expects

Common mistakes

Debugging mistakes. Re running a failed test until it passes instead of reading the trace. Traces turned off in CI, or recorded but never uploaded, so they vanish with the machine. Tracing every test on every run, which is slow and produces huge artifacts. Committing page pause or test only. And shipping raw codegen output as if it were a finished suite.

Avoid these
  • re-running until green instead of reading the trace
  • traces off in CI
  • not uploading artifacts
  • trace "on" for every CI run (slow, huge)
  • committing page.pause()
  • shipping raw codegen output

Say it like this

In the interview: first I read the error and call log: expected versus received, and whether the locator was missing or the value was wrong. Then I open the trace: DOM snapshots, network calls and console. I decide whether it is a product bug, a test bug or an environment problem. Product bugs are reported with the trace attached. Our CI records traces on first retry and uploads them as artifacts.

In the interview
  • "Read the error and call log: expected vs received, and whether it was waiting or wrong. Open the trace: DOM
  • snapshots, network, console.
  • Decide: product bug, test
  • bug or environment. Report
  • bugs with the trace. CI keeps
  • traces on first retry and uploads them as artifacts."

Interview questions

Q1What is the Playwright trace viewer?

A tool that replays a recorded test run: every action with timings, DOM snapshots before and after, network requests, console logs, source and errors.

Q2Which trace setting is common for CI?

"on-first-retry" (record when a test is retried) or "retain-on-failure" (keep traces only for failed tests).

Q3How do you open a trace?

npx playwright show-trace path/to/trace.zip, from the HTML report, or by loading it into trace.playwright.dev.

Q4What is Playwright codegen?

A recorder (npx playwright codegen URL) that generates test code with suggested locators and optional assertions while you use the app.

Q5What are the limits of recorded tests?

No data setup, repeated UI logins, few assertions and duplicated locators; they need refactoring into fixtures, helpers or page objects.

Q6How do you debug a Playwright test step by step?

Run with --debug to open the Inspector, or add await page.pause(), or use UI mode with its timeline and watch mode.

Q7How do you tell a timing failure from a real bug in Playwright output?

The call log shows what the locator resolved to during retries: a consistently wrong value suggests a bug; missing or changing elements suggest timing or locator issues.

Q8What artifacts should CI keep for failed UI tests?

Traces, screenshots, videos (if enabled) and the HTML report, uploaded as build artifacts.