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.

Implementation:Microsoft Playwright Comparators

From Leeroopedia
Revision as of 11:36, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Microsoft_Playwright_Comparators.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:Implementation Page

Overview

Comparators provides the visual and content comparison engine for Playwright's assertion system, supporting PNG, JPEG, text, and binary comparisons with configurable thresholds.

Description

This module provides the getComparator factory function that returns the appropriate comparison function based on MIME type. Supported comparators:

  • image/png and image/jpeg -- pixel-level comparison using either pixelmatch or SSIM algorithms with configurable threshold, maxDiffPixels, and maxDiffPixelRatio
  • text/plain -- text comparison with unified diff output
  • binary -- byte-level buffer comparison

Image comparison handles size mismatches by padding smaller images, decodes JPEG using jpegjs, and generates diff images highlighting differences.

Usage

Used by Playwright's expect assertions for screenshot and snapshot comparisons.

Code Reference

Source Location

packages/playwright-core/src/server/utils/comparators.ts (134 lines)

Function Signatures

export type ImageComparatorOptions = {
  threshold?: number;
  maxDiffPixels?: number;
  maxDiffPixelRatio?: number;
  comparator?: string;
};
export type ComparatorResult = { diff?: Buffer; errorMessage: string } | null;
export type Comparator = (actualBuffer: Buffer | string, expectedBuffer: Buffer, options?: any) => ComparatorResult;

export function getComparator(mimeType: string): Comparator;
export function compareBuffersOrStrings(actualBuffer: Buffer | string, expectedBuffer: Buffer): ComparatorResult;

Import

import { getComparator } from './server/utils/comparators';
import type { ComparatorResult, ImageComparatorOptions } from './server/utils/comparators';

I/O Contract

Inputs

  • mimeType: string -- determines which comparator to use
  • actualBuffer: Buffer | string -- the actual content
  • expectedBuffer: Buffer -- the expected content
  • options -- threshold, max diff pixels, comparator type

Outputs

  • Returns null if comparison passes
  • Returns { errorMessage, diff? } if comparison fails, with optional diff image

Related Pages

Page Connections

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