• Stars
    star
    233
  • Rank 171,298 (Top 4 %)
  • Language Handlebars
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Visual regression test with cypress

cypress-image-diff

Visual regression test with cypress

This tool was created to make visual regression as simple as possible, by exposing basic functions that allow you to view the difference between images. The wrapper uses pixelmatch which is simple and powerful and relies on cypress to take screenshots.

NPM Downloads

Build Status

Version

Table of contents

Getting started

Once you have setup cypress and followed Cypress integration you can start writing tests

Writing a test

Create a spec file under cypress integration folder i.e cypress/integration/specs/some-test-spec.js

Then use the cypress image diff command to take screenshots of pages or elements:

Take screenshot and compare the whole page

describe('Visuals', () => {
  it('should compare screenshot of the entire page', () => {
    cy.visit('www.google.com')
    cy.compareSnapshot('home-page')
  })
})

You can also make the comparison assertion more flexible by applying a higher threshold (default is 0):

describe('Visuals', () => {
  it('should compare screenshot of the entire page', () => {
    cy.visit('www.google.com')
    cy.compareSnapshot('home-page-with-threshold', 0.2)
  })
})

You can also retry the snapshot comparison by passing in an optional third parameter. It accepts the same options as cypress-recurse.

describe('Visuals', () => {
  it('should compare screenshot of the entire page', () => {
    cy.visit('www.google.com')
    const retryOptions = {
      limit: 5, // max number of retries
      delay: 500 // delay before next iteration, ms
    }
    cy.compareSnapshot('home-page-with-threshold', 0, retryOptions)
  })
})

Take screenshot and compare an element

describe('Visuals', () => {
  it('should compare screenshot from a given element', () => {
    cy.visit('www.google.com')
    cy.get('#report-header').compareSnapshot('search-bar-element')
  })
})

Hiding an element before taking a screenshot

describe('Visuals', () => {
  it('should compare screenshot from a given element', () => {
    cy.visit('www.google.com')
    cy.get('#report-header').hideElement() // hideElement(false) to unhide
    cy.compareSnapshot('search-bar-element')
  })
})

Custom config file

If you'd like to take advantages of additional features, you will need to set up the custom config file.

Create a file called cypress-image-diff.config.js. This should live along side cypress.config.js, in the root of the directory.

// cypress-image-diff.config.js
const config = {
  ROOT_DIR: 'custom-folder-name',
};

module.exports = config;

Currently supported values in the custom config file:

  • ROOT_DIR (value relative to the root of the directory)

Updating baseline images

If there are wanted changes to the application in test and if we need to update baseline images, you can follow the steps in CLI documentation to update the baselines.

Alternatively, you can delete the baseline image that you wish to be updated and rerun the tests, this will create a new baseline image with the updated image.

Folder structure

This folder structure will be created by default at the root of your project where your package.json lives:

    .
    ├── cypress-visual-screenshots
        ├── baseline
        ├── comparison
        ├── diff
    ├── cypress-visual-report

In some cases, you may want to modify the folder structure relative to the directory. To accomplish this you will need to set a value on the ROOT_DIR key value on the cypress-image-diff.config.js custom config file. ROOT_DIR is a path relative to the current working directory.

// cypress-image-diff.config.js
const config = {
  ROOT_DIR: 'custom-folder-name',
};

module.exports = config;

Output directory:

    .
    ├── visual-test
        ├── custom-folder-name
            ├── cypress-visual-screenshots
                ├── baseline
                ├── comparison
                ├── diff
            ├── cypress-visual-report

Force resolution size

In order to force the screenshot resolution when running a test you will need to set the following cypress config values in cypress.json:

{
  "viewportWidth": 1000, // Default value: 1280
  "viewportHeight": 660 // Default value: 720
}

Preserving the original screenshot

All screenshots will be renamed and moved from the default screenshot location to a new screenshot folder structure. To preserve the screenshot in the original location, set the following values in cypress.json:

{
  "env": {
    "preserveOriginalScreenshot": true
  }
}

Please notice

Be aware that despite forcing a screenshot resolution to a particular height and width for a test, if this test is run on different computers (i.e a 13" Mac vs PC attached to a 30" monitor), the results will be different. So it's extremely important that you standardize where the tests will run, both locally and CI.

One way to handle this is by running it with docker container. This project tests use a docker container to run the tests so it could be used as an example.

More Repositories

1

stream-unzip

Python function to stream unzip all the files in a ZIP archive on the fly
Python
251
star
2

mbtiles-s3-server

Python server to on-the-fly extract and serve vector tiles from an mbtiles file on S3
Python
136
star
3

sqlite-s3vfs

Python writable virtual filesystem for SQLite on S3
Python
112
star
4

stream-zip

Python function to construct a ZIP archive on the fly
Python
86
star
5

mobius3

Continuously sync folder to S3, using inotify under the hood
Python
42
star
6

data-workspace-frontend

An open source data analysis platform with features for users with a range of technical skills
Python
42
star
7

fargatespawner

Spawns JupyterHub single user servers in Docker containers running in AWS Fargate
Python
38
star
8

pg-bulk-ingest

Python utility function to ingest data into a SQLAlchemy-defined PostgreSQL table
Python
34
star
9

dns-rewrite-proxy

A DNS proxy server that conditionally rewrites and filters A record requests
Python
28
star
10

stream-sqlite

Python function to extract rows from a SQLite file while iterating over its bytes
Python
23
star
11

tidy-json-to-csv

Convert JSON to a set of tidy CSV files
Python
20
star
12

tamato

The Tariff Management Tool (TaMaTo) stores and manages the tariffs and controls that are applied on imports and exports at the UK border. 🍅
Python
17
star
13

jupyters3

Jupyter Notebook Contents Manager for AWS S3
Python
17
star
14

dit-clamav-rest

A minimal ClamD REST interface
Python
15
star
15

great-cms

great.gov.uk CMS
Python
15
star
16

stream-read-xbrl

Python package to parse Companies House accounts data in a streaming way
Python
13
star
17

mock-sso

A mock SSO server to test an applications SSO integration
JavaScript
12
star
18

data-hub-api

Django API for Data Hub frontend
Python
12
star
19

django-workflow-engine

Configurable Django workflow engine
Python
11
star
20

data-hub-frontend

The frontend rendering application for Data Hub
JavaScript
10
star
21

streampq

Python PostgreSQL adapter to stream results of multi-statement queries without a server-side cursor
Python
8
star
22

pii-secret-check-hooks

PII (Personal Identifiable Information) and secret check hooks for pre-commit
Python
7
star
23

iterable-subprocess

Python context manager to communicate with a subprocess using iterables: for when data is too big to fit in memory and has to be streamed
Python
7
star
24

market-access-python-frontend

Python
7
star
25

to-file-like-obj

Python utility function to convert an iterable of bytes or str to a readable file-like object
Python
7
star
26

directory-cms

Content Management System service for directory apps
Python
6
star
27

export-opportunities

UK - Great.gov - Export Opportunities - Find and apply for overseas opportunities from businesses looking for products or services like yours.
HTML
6
star
28

django-chunk-s3-av-upload-handlers

Chunking Django file handlers for S3 and ClamAV service uploads
Python
6
star
29

platform-tools

Tooling to assist with setting up and managing AWS copilot infrastructure
Python
6
star
30

tariff-data-manual

Technical documentation about how the UK tariff data works.
Ruby
6
star
31

s3chunkuploader

A multi threaded S3 file chunk uploader which bypasses local file system and pipes the file directly to S3
Python
6
star
32

trade-elements

Front end pattern library for Department of International Trade
HTML
5
star
33

streamlit-gov-uk-components

A collection of Streamlit components that use or are inspired by the GOV.UK Design System
Shell
5
star
34

data-hub-components

Collection of React Components used by DIT's Data Hub.
JavaScript
5
star
35

digital-workspace-v2

DBT's intranet Django/Wagtail
Python
5
star
36

django-log-formatter-ecs

An ECS formatter for Django logs
Python
5
star
37

jwt-postgresql-proxy

Stateless JWT authentication in front of PostgreSQL
Python
5
star
38

trade-tariff-api

Taric file API for Trade Tariff Management application
Python
5
star
39

data-engineering

A collection of information and learning material relating to data engineering at the Department for International Trade
5
star
40

data-hub-prototype

JavaScript
4
star
41

pg-force-execute

Context manager to run PostgreSQL queries with SQLAlchemy, terminating any other clients that block it
Python
4
star
42

terraform-module-eks-base

HCL
4
star
43

directory-tests

Smoke, integration and functional tests for DIT's services
Python
4
star
44

trade-tariff-management

Ruby
4
star
45

countries-of-interest-service

Lightweight API service for querying for companies that have expressed interest in exporting to specific countries
Python
4
star
46

icms

Import Case Management System - Python/Django port on PostgreSQL database
Python
4
star
47

directory-ui-buyer

Python
3
star
48

help-desk-service

Python
3
star
49

terraform-prometheus-module

HCL
3
star
50

terraform-modules

HCL
3
star
51

data-hub-api-spec

Swagger files and test server for the Data Hub CRM project
Python
3
star
52

great-international-ui

Python
3
star
53

stream-write-ods

Python function to construct an ODS spreadsheet on the fly - without having to store the entire file in memory or disk
Python
3
star
54

directory-api

Directory of Exporters API
Python
3
star
55

dnb-service

A Dunn & Bradstreet microservice
Python
3
star
56

export-support

Python
3
star
57

data-workspace-tools

HTML
3
star
58

dit-thumber

A simple django app for soliciting user feedback on django views
Python
3
star
59

lite-api

Service for handling backend calls in LITE.
Python
3
star
60

infrastructure

Vagrant, Docker and Ansible configuration repository for DIT Digital
Shell
3
star
61

clamav-s3-mirror

Python
3
star
62

legal-basis-api

Legal Basis for Consent Service API Server
Python
3
star
63

dit-helpdesk

HTML
3
star
64

django-zenslackchat

A bot which synchronises support requests from Slack to Zendesk and back.
Python
3
star
65

docker-overlay-network-benchmark

Shell
3
star
66

data-hub-helpcentre

Central point for articles, advice and how-tos for the Data Hub
CSS
3
star
67

market-access-api

The API for Market Access
Python
3
star
68

selling-online-overseas

Department of International Trade marketplace navigator.
Python
3
star
69

flower-runner

Celery Flower runner for GOV.UK PaaS
Python
3
star
70

django-staff-sso-client

A client for staff-sso
Python
3
star
71

govuk-frontend-django

GOV.UK Frontend Django
Python
3
star
72

vulnerability-priority-list

A command line report on a GitHub organisation's repositories, ordered by priority, and including time-to-SLA for each severity level
Python
3
star
73

global-uk-tariff

GOV.UK service displaying tariffs to trade with the UK from 1 January 2021
CSS
3
star
74

ci-pipeline

DIT CD pipeline
Ruby
2
star
75

investment-opps

JavaScript
2
star
76

aioftps3

FTP in front of AWS S3, powered by asyncio and aiohttp
Python
2
star
77

django-ga-measurement-protocol

Python
2
star
78

copilot-poc

Co Pilot PoC
Python
2
star
79

public-data-api

The source for the Department for International Trade's Public Data API
HTML
2
star
80

directory-form-data

Python
2
star
81

company-matching-service

Python
2
star
82

dit-contact-forms

DIT Contact Forms
HTML
2
star
83

great-styles

Styles for great.gov.uk
SCSS
2
star
84

markdown-gds

Python
2
star
85

directory-ui-supplier

deprecated. Incorporated into https://github.com/uktrade/great-international-ui
Python
2
star
86

data-workspace-visualisation

Template repository for Data Workspace visualisations
R
2
star
87

terraform-module-openshift

HCL
2
star
88

mirror-git-to-s3

Python functions and CLI to mirror git repositories to S3
Python
2
star
89

directory-sso

Python
2
star
90

prototypes-on-paas

HTML
2
star
91

invest-pir-api

Personalised Investment Report
Python
2
star
92

terraform-module-aws_account

HCL
2
star
93

directory-companies-house-search

Python
2
star
94

great-pattern-library

Great Pattern library
CSS
2
star
95

iigb-beta-content

Content for IIGB website
2
star
96

gds-technical-documentation-template

mkdocs template using GOV.UK Design System styles
JavaScript
2
star
97

statement-of-works

Automatic generation for SOW forms
Python
2
star
98

dit-ip

Python
2
star
99

lite-internal-frontend

Application for handling internal information in LITE.
Python
2
star
100

dit-classification-matcher

A microservice developed in Python + Flask to map between CPV <-> HS <-> TARIC codes
Python
2
star