Skip to content

Cypress Plugin

The @pxdiff/cypress package integrates pxdiff into Cypress E2E tests. Take screenshots during tests and get instant diff results against baselines.

Terminal window
npm install --save-dev @pxdiff/cypress

Peer dependency: cypress >= 13

Register the pxdiff plugin in your Cypress config:

cypress.config.ts
import { pxdiffPlugin } from "@pxdiff/cypress/plugin";
import { defineConfig } from "cypress";
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
return pxdiffPlugin(on, config, {
suite: "my-app",
});
},
},
});

Import @pxdiff/cypress in your support file:

cypress/support/e2e.ts
import { setupPxdiff } from "@pxdiff/cypress";
setupPxdiff();

This registers the cy.pxdiff() custom command.

Terminal window
export PXDIFF_API_KEY=pxd_your_key_here

Use the cy.pxdiff() command to take screenshots and diff them against baselines:

// Viewport screenshot
cy.visit("/");
cy.pxdiff("homepage");
// Element screenshot
cy.get(".dialog").pxdiff("dialog-open");
// With options
cy.pxdiff("checkout-page", {
ignoreSelectors: [".dynamic-timestamp"],
maxDiffPixelRatio: 0.01,
path: ["pages", "Checkout"],
});
// Full-page screenshot
cy.pxdiff("landing-page", { fullPage: true });

Registers pxdiff tasks and event handlers in Cypress’s Node.js process. Call this in setupNodeEvents.

OptionTypeDefaultDescription
suitestring"cypress"Suite name for grouping snapshots
blockingbooleantrueWhether changed snapshots fail the test
apiUrlstringPXDIFF_API_URL or "https://pxdiff.com"API URL
git.branchstring(auto-detected)Git branch override
git.commitstring(auto-detected)Git commit override
localboolean(auto-detected)Local mode (from PXDIFF_LOCAL or absence of CI)
autoApproveboolean(auto-detected)From PXDIFF_AUTO_APPROVE

Custom command that takes a screenshot, uploads it, and asserts on the diff result.

Can be called standalone or chained on an element:

cy.pxdiff("full-page");
cy.get(".component").pxdiff("component-name");
OptionTypeDefaultDescription
blockingboolean(from plugin)Override blocking mode for this snapshot
ignoreSelectorsstring[]CSS selectors to hide via visibility: hidden
maxDiffPixelRationumberMax acceptable diff ratio (0–1). Pass if below this
pathstring[]Display hierarchy path for tree grouping in review UI
fullPagebooleanfalseCapture full scrollable page instead of viewport

Elements matching ignoreSelectors are hidden with visibility: hidden before the screenshot is taken. The CSS is cleaned up immediately after.

In addition, elements with data-pxdiff="ignore" or data-chromatic="ignore" attributes are always hidden:

<span data-pxdiff="ignore">Dynamic content here</span>

When fullPage: true, Cypress captures the full scrollable page by scrolling and stitching multiple viewports. Elements with position: fixed or position: sticky will repeat in every stitched segment. Prefer viewport screenshots for pages with sticky headers or navbars.

The command determines pass/fail as follows:

  • Pass: unchanged, new, auto-approved, within maxDiffPixelRatio, non-blocking mode, or CI mode (soft-fail)
  • Fail: changed snapshots exceeding the threshold in blocking mode

In CI mode (when PXDIFF_SESSION_ID is set and not local), the command always passes. The pxdiff GitHub check run reports the overall result.


The recommended approach for CI is to wrap your Cypress tests with pxdiff run:

.github/workflows/visual.yml
- name: Visual Regression
run: pxdiff run -- npx cypress run
env:
PXDIFF_API_KEY: ${{ secrets.PXDIFF_API_KEY }}

This creates a GitHub check run, groups all screenshots into one session, and reports results back to the PR.

You can also run Cypress tests without pxdiff run. Screenshots are still uploaded and diffed individually:

- name: Visual Tests
run: npx cypress run
env:
PXDIFF_API_KEY: ${{ secrets.PXDIFF_API_KEY }}

VariableDescription
PXDIFF_API_KEY(required) API authentication key
PXDIFF_API_URLAPI base URL (default: https://pxdiff.com)
PXDIFF_SESSION_IDSession ID for grouping (set by pxdiff run)
PXDIFF_LOCALSet to true for local development mode
PXDIFF_AUTO_APPROVESet to true to auto-approve all changes
PXDIFF_BRANCHOverride git branch detection
PXDIFF_COMMITOverride git commit detection