Module for adding visual regression testing to Cypress using Resemble.JS library
npm install cypress-visual-regression-resemble-js
sh
$ npm install cypress-visual-regression-resemble-js
`
Add the following config to your cypress.config.js file:
`javascript
const { defineConfig } = require("cypress");
const getCompareSnapshotsPlugin = require('cypress-visual-regression-resemble-js/dist/plugin');
module.exports = defineConfig({
env: {
screenshotsFolder: './cypress/snapshots/actual',
trashAssetsBeforeRuns: true,
video: false
},
e2e: {
setupNodeEvents(on, config) {
getCompareSnapshotsPlugin(on, config);
},
},
});
`
Add the command to cypress/support/commands.js:
`javascript
const compareSnapshotCommand = require('cypress-visual-regression-resemble-js/dist/command');
compareSnapshotCommand();
`
> Make sure you import commands.js in cypress/support/e2e.js:
>
> `javascript
> import './commands'
> `
$3
If you're using TypeScript, use files with a .ts extension, as follows:
cypress/cypress.config.ts
`ts
import { defineConfig } from 'cypress';
import getCompareSnapshotsPlugin from 'cypress-visual-regression-resemble-js/dist/plugin';
export default defineConfig({
env: {
screenshotsFolder: './cypress/snapshots/actual',
trashAssetsBeforeRuns: true,
video: false
},
e2e: {
setupNodeEvents(on, config) {
getCompareSnapshotsPlugin(on, config);
},
},
});
`
cypress/support/commands.ts
`ts
import compareSnapshotCommand from 'cypress-visual-regression-resemble-js/dist/command';
compareSnapshotCommand();
`
cypress/tsconfig.json
`json:
{
"compilerOptions": {
"types": [
"cypress",
"cypress-visual-regression-resemble-js"
]
}
}
`
For more info on how to use TypeScript with Cypress, please refer to this document.
$3
failSilently is enabled by default. Add the following config to your cypress.config.js file to see the errors:
`javascript
{
env: {
failSilently: false
}
}
`
You can also pass default arguments to compareSnapshotCommand():
`javascript
const compareSnapshotCommand = require('cypress-visual-regression-resemble-js/dist/command');
compareSnapshotCommand({
capture: 'fullPage'
});
`
These will be used by default when no parameters are passed to the compareSnapshot command.
Configure snapshot paths
You can control where snapshots should be located by setting two environment variables:
| Variable | Description |
|----------|-------------|
| SNAPSHOT_BASE_DIRECTORY | Directory of the base snapshots |
| SNAPSHOT_DIFF_DIRECTORY | Directory for the snapshot diff |
The actual directory always points to the configured screenshot directory.
Configure snapshot generation
In order to control the creation of diff images you may want to use the following environment variables which are
typically set by using the field env in configuration in cypress.config.json.
| Variable | Description |
|---------------------------------|----------------------------|
| ALWAYS_GENERATE_DIFF | Boolean, defaults to true |
ALWAYS_GENERATE_DIFF specifies if diff images are generated for successful tests.
If you want to see all diff images which are different (based on your thresholds), use the following in your cypress.config.json:
`json
{
"env": {
"ALWAYS_GENERATE_DIFF": false
}
}
`
To Use
Add cy.compareSnapshot('home'); in your tests specs whenever you want to test for visual regressions, making sure to replace home with a relevant name. You can also add an optional error threshold: Value can range from 0.00 (no difference) to 1.00 (every pixel is different). So, if you enter an error threshold of 0.51, the test would fail only if > 51% of pixels are different.
More examples:
| Threshold | Fails when |
|-----------|------------|
| .25 | > 25% |
| .30 | > 30% |
| .50 | > 50% |
| .75 | > 75% |
Sample:
`js
it('should display the login page correctly', () => {
cy.visit('/03.html');
cy.get('H1').contains('Login');
cy.compareSnapshot('login', 0.0);
cy.compareSnapshot('login', 0.1);
});
`
You can target a single HTML element as well:
`js
cy.get('#my-header').compareSnapshot('just-header')
`
You can pass arguments as an object to cy.compareSnapshot(), rather than just an error threshold, as well:
`js
it('should display the login page correctly', () => {
cy.visit('/03.html');
cy.compareSnapshot('login', {
capture: 'fullPage',
errorThreshold: 0.1
});
});
`
> Looking for more examples? Review docker/sample_application.
Take the base images:
`sh
$ ./node_modules/.bin/cypress run --env type=base --config screenshotsFolder=cypress/snapshots/base,testFiles=\"*/regression-tests.js\"
use comma separated format for multiple config commands
$ ./node_modules/.bin/cypress run \
--env type=base \
--config screenshotsFolder=cypress/snapshots/base,testFiles=\"*/regression-tests.js\"
`
Find regressions:
`sh
$ ./node_modules/.bin/cypress run --env type=actual
``
