Skip to main content

Overview

Agent Browser includes built-in diff commands for comparing page states, making it ideal for:
  • Visual regression testing
  • Detecting unintended changes between deployments
  • Comparing staging vs production
  • Monitoring page changes over time
  • Testing responsive design at different breakpoints
Diff capabilities include:
  • Snapshot diff - Text-based comparison using the Myers diff algorithm
  • Screenshot diff - Pixel-based visual comparison using Canvas API
  • URL diff - Compare two different URLs side-by-side

Snapshot Diff

Compare accessibility tree snapshots to detect structural changes.

Compare Current vs Last Snapshot

Output:

Scoped Snapshot Diff

Compare only a specific section of the page:

Compact Diff

Remove empty structural elements for cleaner comparison:

Implementation Details

The snapshot diff uses the Myers diff algorithm for efficient line-level comparison. See src/diff.ts for the implementation:
The algorithm produces a minimal edit script with three types of edits:
  • equal - Lines that are unchanged
  • insert - Lines added in the new snapshot
  • delete - Lines removed from the old snapshot

Screenshot Diff

Compare images pixel-by-pixel to detect visual changes.

Basic Screenshot Diff

Output:

Custom Output Path

Adjust Color Threshold

Control sensitivity to color differences (0-1, default 0.1):
The threshold represents the maximum color distance (normalized 0-1) before pixels are marked as different. Lower values detect smaller changes.

Implementation Details

Screenshot diff uses the browser’s Canvas API for pixel comparison. See src/diff.ts:160-340:
Key implementation details:
  1. Isolated page: Diff runs in a separate page to avoid CSP issues
  2. Route interception: Images served via routes instead of base64 in page.evaluate() to avoid CDP message size limits
  3. Euclidean distance: Color differences calculated in RGB space
  4. Output image: Different pixels highlighted in red, matching pixels dimmed to 30% brightness

Dimension Mismatch

If images have different dimensions, the diff will fail:
Ensure both screenshots use the same viewport size:

URL Diff

Compare two different URLs directly.

Snapshot Diff Between URLs

This opens both URLs, takes snapshots, and shows the diff.

Screenshot Diff Between URLs

Takes screenshots of both URLs and performs pixel comparison.

Custom Wait Strategy

Wait for specific load state before comparing:

Scoped URL Diff

Compare only a specific section:

Testing Workflows

Visual Regression Testing

Detect unintended visual changes between deployments:

Responsive Design Testing

Compare page layout at different breakpoints:

Monitoring Changes Over Time

Detect changes to a page over time:

CI/CD Integration

Integrate diff testing into continuous integration:

Best Practices

Snapshot Diff

  1. Use compact mode for cleaner diffs: --compact
  2. Scope to relevant sections to reduce noise: --selector "#main"
  3. Wait for dynamic content before diffing: agent-browser wait --load networkidle
  4. Store baselines in version control for reproducibility

Screenshot Diff

  1. Set consistent viewport before screenshots:
  2. Adjust threshold based on your needs:
    • Strict: -t 0.05 (detects subtle changes)
    • Moderate: -t 0.1 (default, good balance)
    • Lenient: -t 0.3 (ignores minor variations)
  3. Wait for animations to complete:
  4. Use full page screenshots for complete comparison:
  5. Hide dynamic elements (timestamps, ads) before comparison:

URL Diff

  1. Use —wait-until networkidle for dynamic pages
  2. Scope to stable sections to avoid flaky diffs
  3. Set viewport size for consistent layout
  4. Authenticate before diffing if comparing logged-in pages:

Limitations

Snapshot Diff

  • Text-based only - Does not detect visual styling changes (colors, fonts, spacing)
  • Structure-dependent - Minor HTML restructuring can cause large diffs
  • Ref numbers change - Refs (@e1, @e2) are not stable across snapshots

Screenshot Diff

  • Dimension mismatch - Images must have identical dimensions
  • Dynamic content - Timestamps, ads, and animations cause false positives
  • Antialiasing differences - Font rendering can vary across environments
  • Performance - Pixel comparison can be slow for large images

See Also