npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cypress-screenshot-compare

v2.0.5

Published

Module for adding visual regression testing to Cypress

Downloads

17

Readme

Cypress Screenshot Compare

npm

Module for adding visual regression testing to Cypress.

Getting Started

Install:

$ npm install cypress-screenshot-compare

Add the following config to your cypress.config.js file:

const { defineConfig } = require("cypress");
const getCompareSnapshotsPlugin = require('cypress-screenshot-compare/dist/plugin');

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      getCompareSnapshotsPlugin(on, config);
    },
  },
});

Add the command to cypress/support/commands.js:

const compareSnapshotCommand = require('cypress-screenshot-compare/dist/command');

compareSnapshotCommand();

Make sure you import commands.js in cypress/support/e2e.js:

import './commands'

TypeScript

If you're using TypeScript, use files with a .ts extension, as follows:

cypress/cypress.config.ts

import { defineConfig } from 'cypress';
import getCompareSnapshotsPlugin from 'cypress-screenshot-compare/dist/plugin';

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

cypress/support/commands.ts

import compareSnapshotCommand from 'cypress-screenshot-compare/dist/command';

compareSnapshotCommand();

cypress/tsconfig.json

{
  "compilerOptions": {
    "types": [
      "cypress",
      "cypress-screenshot-compare"
    ]
  }
}

For more info on how to use TypeScript with Cypress, please refer to this document.

Options

There are two possible start-up options update and compare, defined in the screenshotEvalType field under 'env'

{
  env: {
      screenshotEvalType: 'update'
  }
}

update will create/update the screenshots in the 'base' folder

compare will compare screenshots located in 'base' folder with the newer one created in the 'actual' folder. By default, it will automatically create a new screenshot if one is not already present in the 'actual' folder

failSilently is enabled by default. Add the following config to your cypress.config.js file to see the errors:

{
  env: {
    failSilently: false
  }
}

You can also pass default arguments to compareSnapshotCommand():

const compareSnapshotCommand = require('cypress-screenshot-compare/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 difference | | INTEGRATION_FOLDER | Used for computing correct snapshot directories |

The actual directory always points to the configured screenshot directory.

For more information regarding INTEGRATION_FOLDER please refer to PR#139

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 | | ALLOW_VISUAL_REGRESSION_TO_FAIL | Boolean, defaults to false |

ALWAYS_GENERATE_DIFF specifies if diff images are generated for successful tests. If you only want the tests to create diff images based on your threshold without the tests to fail, you can set ALLOW_VISUAL_REGRESSION_TO_FAIL. If this variable is set, diffs will be computed using your thresholds but tests will not fail if a diff is found.

If you want to see all diff images which are different (based on your thresholds), use the following in your cypress.config.json:

{
  "env": {
    "ALWAYS_GENERATE_DIFF": false,
    "ALLOW_VISUAL_REGRESSION_TO_FAIL": true
  }
}

To Use

Add cy.compareSnapshot('home'); in your tests specs whenever you want to test for screenshot comparison, 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:

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:

cy.get('#my-header').compareSnapshot('just-header')
Based on cypress-visual-regression