Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Microsoft Playwright Capture Trace Data

From Leeroopedia
Knowledge Sources
Domains Debugging, Testing
Last Updated 2026-02-11 00:00 GMT

Overview

Intercepting and recording all test execution events (API calls, network requests, DOM snapshots) into a structured trace format.

Description

After trace recording has been initiated, the system must continuously capture all relevant execution events as they occur. This principle addresses the instrumentation layer that sits between the test execution engine and the trace file writer, intercepting every API call, network request, DOM mutation, console message, and error.

The capture process operates on three distinct event streams:

1. Action Events (before/input/after pattern):

Every Playwright API call (e.g., page.click(), page.fill()) flows through an instrumentation pipeline that emits three trace events:

  • before: Recorded synchronously when the call begins, capturing the call ID, method name, parameters, and call stack.
  • input: Recorded just before the actual input action (e.g., mouse click), capturing the precise coordinates.
  • after: Recorded when the call completes, capturing the result, any error, and the end timestamp.

This three-phase pattern enables the trace viewer to show DOM snapshots at each stage of an action: the state before the action, the state just before input, and the state after the action completes.

2. Network Events (HAR format):

Network requests and responses are captured through a HAR (HTTP Archive) tracer that records full request/response details including headers, body content, and timing. These events are written to a separate .network file to keep the main trace file focused on actions.

3. Frame Snapshots:

DOM snapshots are captured by injecting a frame snapshot streamer script into every page frame. When a snapshot is requested (typically before and after each action), this injected script serializes the current DOM tree, including:

  • Document type and HTML structure
  • Computed styles via adopted stylesheets
  • Resource overrides (inline styles, images)
  • Shadow DOM content
  • Viewport dimensions

Resource blobs (images, stylesheets) are stored separately in a resources/ directory, referenced by their SHA1 hash for deduplication.

Usage

This principle is applied automatically and transparently during test execution when tracing is active. Developers do not directly interact with the capture layer. However, understanding it is valuable for:

  • Diagnosing trace file size issues: Understanding what is captured helps optimize trace configuration.
  • Custom instrumentation: Extending the trace format with custom events.
  • Performance tuning: Knowing that snapshots are the most expensive operation helps decide when to disable them.

Theoretical Basis

The capture system follows the aspect-oriented programming pattern, where cross-cutting concerns (tracing) are woven into the execution flow without modifying the core business logic (API methods).

The abstract capture pipeline:

// Instrumentation listener interface
interface TraceCapture:
    onBeforeCall(object, metadata):
        event = createBeforeEvent(metadata)
        if shouldCaptureSnapshot(metadata):
            event.beforeSnapshot = captureDOM(object.page, metadata.id)
        appendTraceEvent(event)

    onAfterCall(object, metadata):
        event = createAfterEvent(metadata)
        if shouldCaptureSnapshot(metadata):
            event.afterSnapshot = captureDOM(object.page, metadata.id)
        appendTraceEvent(event)

// Snapshot capture
function captureDOM(page, callId):
    for each frame in page.frames:
        snapshotData = frame.evaluate(snapshotStreamer.capture())
        for each resourceOverride in snapshotData.resourceOverrides:
            blob = createBlob(resourceOverride.content)
            storeResource(blob.sha1, blob.buffer)
        emitFrameSnapshot(callId, snapshotData)

Key design principles in the capture layer:

  • Synchronous event creation: The before event must be appended synchronously (not awaited) to preserve ordering. The snapshot capture happens asynchronously but the event timestamp is fixed at creation time.
  • SHA1-based deduplication: All binary resources (images, fonts, stylesheets) are stored once, referenced by their content hash. This dramatically reduces trace file size for pages with many shared resources.
  • Selective snapshot capture: Not all API methods warrant DOM snapshots. A metadata table maps method names to snapshot requirements, avoiding unnecessary capture for non-visual operations like evaluate().
  • Best-effort capture: Snapshot failures (e.g., detached frames) are silently caught and ignored rather than failing the test. Traces are diagnostic aids, not correctness-critical.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment