Cypress Visual Regression
Plugin that adds powerful visual regression testing capabilities to Cypress:
Installation
npm install cypress-visual-regression
Configuration
JavaScript
Configure the visual regression plugin and environment variables in your cypress.config.js file like:
const { defineConfig } = require('cypress')
const { configureVisualRegression } = require('cypress-visual-regression')
module.exports = defineConfig({
e2e: {
env: {
visualRegressionType: 'regression'
},
screenshotsFolder: './cypress/snapshots/actual',
setupNodeEvents(on, config) {
configureVisualRegression(on)
}
}
})
Pay attention to the visualRegressionType
option. Use 'base' to generate baseline images, and 'regression' to compare current
screenshot to the base screenshot
In your support file cypress/support/e2e.js add the following:
const { addCompareSnapshotCommand } = require('cypress-visual-regression/dist/command')
addCompareSnapshotCommand()
TypeScript
If you're using TypeScript, use files with a .ts
extension, as follows:
cypress.config.ts
import { defineConfig } from 'cypress'
import { configureVisualRegression } from 'cypress-visual-regression'
export default defineConfig({
e2e: {
env: {
visualRegressionType: 'regression'
},
screenshotsFolder: './cypress/snapshots/actual',
setupNodeEvents(on, config) {
configureVisualRegression(on)
}
}
})
cypress/support/e2e.ts
import { addCompareSnapshotCommand } from 'cypress-visual-regression/dist/command'
addCompareSnapshotCommand()
cypress/tsconfig.json
{
"ts-node": {
"transpileOnly": true,
"compilerOptions": {
"module": "ES2015"
}
}
}
For more info on how to use TypeScript with Cypress, please refer to this document.
Plugin options
All options can be configured within visualRegression
prefix under env
variable inside cypress.config.js
file, like this:
module.exports = defineConfig({
e2e: {
screenshotsFolder: './cypress/snapshots/actual',
env: {
visualRegressionType: 'regression',
visualRegressionBaseDirectory: 'cypress/snapshot/base',
visualRegressionDiffDirectory: 'cypress/snapshot/diff',
visualRegressionGenerateDiff: 'always',
visualRegressionFailSilently: true
}
}
})
Variable | Default | Description |
---|
visualRegressionType | / | Either 'regression' or 'base'. Base will override any existing base images with new screenshots. Regression will compare the base to the current screenshot. |
visualRegressionBaseDirectory | 'cypress/snapshots/base' | Path to the directory where the base snapshots will be stored. |
visualRegressionDiffDirectory | 'cypress/snapshots/diff' | Path to the directory where the generated image differences will be stored. |
visualRegressionGenerateDiff | 'fail' | Either 'fail', 'never' or 'always'. Determines if and when image differences are generated. |
visualRegressionFailSilently | false | Used to decide if any error found in regression should be thrown or returned as part of the result. |
To override different default arguments/options on a global level pass them to the addCompareSnapshotCommand()
command:
- cypress screenshot arguments
- pixelmatch options
- plugin configuration (errorThreshold, failSilently)
const { addCompareSnapshotCommand } = require('cypress-visual-regression/dist/command')
addCompareSnapshotCommand({
capture: 'fullPage',
errorThreshold: 0.5,
pixelmatchOptions: {
threshold: 0
}
})
How To Use
> syntax
cy.compareSnapshot(name)
cy.compareSnapshot(name, errorThreshold)
cy.compareSnapshot(name, options)
> arguments
Arguments | Default | Description |
---|
name | / | Represents the name of the base snapshot file that the actual screenshot will be compared with. |
errorThreshold | 0 | Threshold under which any image difference will be considered as failed test. Represented in percentages. |
options | {} | Used to provide additional cypress screenshot arguments, pixelmatch options, and failSilently and errorThreshold values. |
> yields
.compareSnapshot()
yields the Visual Regression Result object which contains the following info:
Result | Type | Description |
---|
error | string (optional) | Contains visual regression error message |
images | object | Contains base64 string of generated images for actual , base (optional) and diff (optional) images |
baseGenerated | boolean (optional) | Set to true if visual regression plugin was run for base generation (visualRegressionType set to 'base') |
mismatchedPixels | number (optional) | Represents the number of total mismatched pixels during visual comparison. Set if difference were discovered |
percentage | number (optional) | Represents the percentage of the difference between the images in decimals. Set if difference were discovered |
> examples
cy.compareSnapshot('homePage')
cy.get('h1').compareSnapshot('homePage', 0.2)
cy.compareSnapshot('homePage', {errorThreshold: 1, failSilently: true}).then(comparisonResults => {
console.log(comparisonResults.mismatchedPixels)
console.log(comparisonResults.percentage)
console.log(comparisonResults.error)
})
Looking for more examples? See cypress/e2e/main.cy.ts.
Tips & Tricks
Ignore some elements
Following function creates a command that allows you to hide elements of the page based on their className:
export function beforeCompareSnapshots(
ignoredElementsQuerySelector: string,
appContentQuerySelector: string = 'body'
) {
Cypress.Commands.overwrite('compareSnapshot', (originalFn, ...args) => {
return (
cy
.get(appContentQuerySelector)
.then(($app) => {
return new Cypress.Promise((resolve, reject) => {
setTimeout(() => {
$app.find(ignoredElementsQuerySelector).css('visibility', 'hidden')
resolve()
}, 300)
})
})
.then(() => {
return originalFn(...args)
})
)
})
}
You may then use this function like:
const { addCompareSnapshotCommand } = require('cypress-visual-regression/dist/command')
const beforeCompareSnapshots = require('./commands/beforeCompareSnapshots')
addCompareSnapshotCommand({
errorThreshold: 0.1
})
beforeCompareSnapshots(".chromatic-ignore,[data-chromatic='ignore']", '._app-content')
In this example, we ignore the elements that are also ignored by 3rd party tool Chromatic.
Debug
set process env visual_regression_log
to debug
to enable logging:
visual_regression_log=debug cypress open --e2e -b chrome -C cypress.base.config.ts