playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature]: Add npx playwright trace diff <traceA.zip> <traceB.zip> CLI command

Open halcyon-past opened this issue 1 week ago • 4 comments

Motivation

When debugging flaky tests or CI regressions, developers often inspect two trace.zip files (e.g. passing on main vs failing on a PR branch, or attempt 1 vs retry). Today, developers must open two Trace Viewer windows and inspect both manually to find where runs diverge.

Adding a CLI command npx playwright trace diff <traceA.zip> <traceB.zip> allows developers and automated workflows to quickly identify the first divergence between two traces without needing a GUI.

Proposed Solution

Add diff subcommand to playwright trace:

npx playwright trace diff <baseline-trace.zip> <target-trace.zip> [options]

Options:

  • --json: Output structured JSON data (filtered when used with --only).
  • --only <actions|network|console>: Filter output to a specific category.
  • --ignore-timings: Ignore timing deltas when comparing across different hardware.
  • --exit-code: Exit with code 1 when differences are detected.

Key Architecture & Diffing Details:

  • Zero-Disk In-Memory Parsing: Streams .trace and .network entries directly from .zip archives into memory via ZipFile.read(), bypassing disk decompression, temporary directory cleanup, and OS temp directory exhaustion.
  • Resource & Decompression Limits: Rejects archives >500 MB and enforces a 200 MB stream limit per entry with automatic file descriptor closure to prevent zip bomb attacks and memory exhaustion.
  • Sequence Alignment (Myers Diff): Uses bounded linear-space Myers diff ($D \le 1000$, $<8$ MB memory footprint) with common prefix/suffix fast-path trimming on normalized action signatures to prevent false divergence cascades.
  • Error Source Aggregation: Aggregates test assertion errors (model.errorDescriptors), stderr output (model.stdio), console errors, and uncaught page errors with dynamic millisecond/stack trace normalization.
  • Network Request Alignment: Pairs HTTP requests per URL using Myers diff to identify status code changes, missing requests, and extra requests while normalizing ephemeral localhost ports and query tokens.
  • Action Ordinals: Reports concrete action ordinals (#<ordinal>) matching trace actions output for direct lookup.

Terminal Output Example

Trace Diff: pass.zip -> fail.zip
============================================================
Metadata:
  Browser:   chromium (same)
  Viewport:  800x600 (same)
  Duration:  4.21s -> 31.04s (+26.83s)
  Actions:   12 -> 8

First Divergence:
  Reason:    Target action failed with error
  Baseline:  #4 Click locator('#btn-checkout') [passed, 84ms]
  Target:    #4 Click locator('#btn-checkout') [failed, 30s]
             Timeout 30000ms exceeded.

Network Diff:
  ~ POST /api/cart/summary  200 -> 500
  + GET /extra  200 (new in target)
  - GET /page2  200 (missing in target)

Console / Page Errors (new in target):
  + [console.error] Uncaught TypeError: Cannot read properties of undefined

Additional context

Implementation with full test coverage is ready in PR #44378.

halcyon-past avatar Nov 25 '26 06:11 halcyon-past

Hi, I'm the Playwright bot. I started triaging this issue on Linux: agent run

Hi, I'm the Playwright bot and I checked the current trace commands and the linked proposal.

Automatic trace comparison is not available on main at b9cff8a68a090c27d29d856747265732468acff0. The existing trace actions, trace requests, trace console, and trace errors commands let you inspect each trace without a GUI, but do not compare two runs.

This needs a design decision about what makes two traces equivalent, not just a new command. For example, two requests can have the same URL and status but different response bodies. Whether that counts as a difference determines what --exit-code promises; timing and token normalization also affect that promise. The JSON result would make those choices part of a public interface. I left this as a design request and did not add a competing implementation. I read #44378, but did not run or approve it.

Triaged on Linux by the Playwright bot - agent run

Thank you for the issue. From our experience, agents are better in figuring out the problem from traces than an automatic diff tool. We can improve the output of other npx playwright trace commands to make this usecase easier. If you have ideas, please file an issue.

dgozman avatar Nov 25 '26 16:11 dgozman

Thank you @dgozman for the review and context. That makes sense—letting agents reason over individual trace outputs avoids rigid equivalence definitions in core.

I'd be interested in contributing improvements to make existing commands easier for agents to consume (such as structured --json output for trace actions and trace requests). I will open a separate issue to discuss the format.

halcyon-past avatar Nov 25 '26 17:11 halcyon-past