Skip to content

About

Visual Regression Tracker integration plugin

Topics

Resources

Stars

75 stars

Watchers

3 watching

Forks

Repository files navigation

Cypress agent for Visual Regression Tracker

npm

Adds cy.vrtStart(), cy.vrtTrack() and cy.vrtStop() to Cypress tests. The agent is built on sdk-js.

Requirements

  • Cypress 12.17.1 or later
  • Node.js 26 or later
  • Visual Regression Tracker server 5.0.8 or later; 6 is recommended

Install

npm install --save-dev @visual-regression-tracker/agent-cypress

Register the plugin in cypress.config.js (under component for component tests):

import { defineConfig } from "cypress";
import { addVisualRegressionTrackerPlugin } from "@visual-regression-tracker/agent-cypress";

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      addVisualRegressionTrackerPlugin(on, config);
    },
  },
});

Add the commands in cypress/support/e2e.js (or component.js):

import { addVrtCommands } from "@visual-regression-tracker/agent-cypress";

addVrtCommands();

To add only some of them, use addVrtStartCommand, addVrtStopCommand, addVrtTrackCommand, addVrtTrackBufferCommand and addVrtTrackBase64Command.

Configuration

Option Environment variable Required Default Description
apiUrl VRT_APIURL yes URL of the server API, e.g. http://localhost:4200
apiKey VRT_APIKEY yes API key of a user with access to the project
project VRT_PROJECT yes Project name or ID
branchName VRT_BRANCHNAME yes Current git branch
sourceBranch VRT_SOURCEBRANCH no project's main branch Branch to take baselines from while branchName has none of its own
ciBuildId VRT_CIBUILDID no ID of one CI build; machines with the same ID share a build
enableSoftAssert VRT_ENABLESOFTASSERT no false Log failed comparisons instead of failing the test

Set the options under env.visualRegressionTracker in the Cypress config:

export default defineConfig({
  env: {
    visualRegressionTracker: {
      apiUrl: "http://localhost:4200",
      apiKey: "tXZVHX0EA4YQM1MGDD",
      project: "Default project",
      branchName: "develop",
    },
  },
});

If visualRegressionTracker is not set, the options are read from vrt.json in the working directory, with the same keys:

{
  "apiUrl": "http://localhost:4200",
  "apiKey": "tXZVHX0EA4YQM1MGDD",
  "project": "Default project",
  "branchName": "develop",
  "sourceBranch": "release",
  "ciBuildId": "commit_sha",
  "enableSoftAssert": false
}

Environment variables override vrt.json key by key. When visualRegressionTracker is set, only VRT_SOURCEBRANCH is read from the environment.

baselineBranchName (VRT_BASELINEBRANCHNAME) is a deprecated alias of sourceBranch. It still works and logs a warning once.

Usage

before(() => cy.vrtStart());
after(() => cy.vrtStop());

it("home page", () => {
  cy.visit("/");

  cy.vrtTrack("Home page");

  cy.get("#navbar").vrtTrack("Navbar", {
    os: "linux",
    device: "desktop",
    customTags: "smoke,dark",
    comment: "Logged out",
    diffTollerancePercent: 1,
    ignoreAreas: [{ x: 1, y: 2, width: 100, height: 200 }],
    retryLimit: 2,
    keepScreenshot: false,
  });
});

cy.vrtTrack(name, options, errorCallback) takes a screenshot of the page, or of the element it is chained on, and uploads it as multipart. Every option is optional:

Option Description
os, device, customTags Tell baselines of the same name apart
viewport Defaults to viewportWidthxviewportHeight times the pixel ratio
diffTollerancePercent Allowed difference, in percent of all pixels (the spelling is the API's)
comment Shown with the test run
ignoreAreas [{ x, y, width, height }] in pixels, left out of the comparison
retryLimit Retries while the result is unresolved, default 2
keepScreenshot Keep the local screenshot file, default false
any cy.screenshot() option Passed to the screenshot

The browser is taken from Cypress.browser.name.

cy.vrtTrackBuffer(name, buffer, options) and cy.vrtTrackBase64(name, base64, options) track an image you already have.

Results and statuses

Status Outcome
new No baseline yet: fails the test, or logs with enableSoftAssert
unresolved Differs from the baseline: fails the test, or logs with enableSoftAssert
ok, approved, autoApproved Passes
failed A reviewer rejected it: passes with a warning
anything else Passes with a warning

Logs and warnings appear in the terminal that runs Cypress, not in the browser.

Errors

A failed comparison or request fails the test with the server's message. Use errorCallback to handle it for one screenshot:

cy.vrtTrack("Home page", {}, (err) => {
  cy.log(err);
  return true; // true: do not fail the test
});

Request errors name the HTTP status and add a hint for common causes, such as a wrong apiKey (401) or an image over the server's limits (413, 422). See sdk-js.

Examples

examples-js

Releasing

Every push to master runs semantic-release. It reads the Conventional Commits since the last v* tag:

Commit Release
fix: patch
feat: minor
feat!: or a BREAKING CHANGE: footer major
chore:, ci:, docs:, test: none

It then publishes to npm through trusted publishing with provenance, tags vX.Y.Z, and writes the GitHub release. The version field in package.json is a placeholder. The released version lives in the tags and on npm.

Release candidates

Every push to next releases a pre-release of the next version, X.Y.Z-rc.N (6.0.0-rc.1 once a breaking change is in). It goes to npm under the next dist-tag, never latest: npm install @visual-regression-tracker/agent-cypress@next installs the candidate, and a plain install keeps getting the last stable release. Its GitHub release is marked as a pre-release.

Breaking changes go into next through pull requests that target it. Merge master into next when master gets new commits.

To release the final version, merge next into master with a merge commit, not a squash, so that semantic-release still sees the breaking commits. That push releases it to latest. The repository deletes merged branches, so create next again from master before the next round of candidates.

License

Apache-2.0

About

Visual Regression Tracker integration plugin

Topics

Resources

Stars

75 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages