Skip to content

Commit 4fb5572

Browse files
Merge pull request #3 from uktrade/feature/enhance-test-name-and-threshold-functionality
Enhance test name and threshold functionality
2 parents 7f78b10 + 833fd54 commit 4fb5572

7 files changed

+24
-14
lines changed
Loading

cypress/specs/image-diff-spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ it('should compare screenshot of the entire page', () => {
33
cy.compareSnapshot('wholePage')
44
})
55

6+
it('should compare screenshot of the entire page', () => {
7+
cy.visit('../../report-example.html')
8+
cy.compareSnapshot('wholePageThreshold', 0.2)
9+
})
10+
611
it('should compare screenshot from a given element', () => {
712
cy.visit('../../report-example.html')
813
cy.get('#report-header').compareSnapshot('element')

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "dist/command.js",
66
"scripts": {
77
"test:unit": "jest src/",
8-
"test:e2e": "npx run build && cypress run --browser chrome",
8+
"test:e2e": "npm run build && cypress run --browser chrome --headless",
99
"build": "npx babel src -d dist"
1010
},
1111
"repository": {

src/command.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
const compareSnapshotCommand = defaultScreenshotOptions => {
2+
Cypress.Screenshot.defaults({
3+
screenshotOnRunFailure: false
4+
})
5+
26
Cypress.Commands.add(
37
'compareSnapshot',
48
{ prevSubject: 'optional' },
5-
(subject, name) => {
9+
(subject, name, testThreshold = 0) => {
610
let screenshotOptions = defaultScreenshotOptions
7-
11+
const testName = `${Cypress.spec.name.replace('.js', '')} - ${name}`
812
// take snapshot
913
const objToOperateOn = subject ? cy.get(subject) : cy
1014

1115
objToOperateOn
12-
.screenshot(name, screenshotOptions)
16+
.screenshot(testName, screenshotOptions)
1317
.task('copyScreenshot', {
14-
testName: name,
18+
testName,
1519
})
1620

1721
// run visual tests
1822
const options = {
19-
testName: name,
23+
testName,
24+
testThreshold,
2025
}
2126

22-
cy.task('compareSnapshotsPlugin', options).then((result) => {
23-
if (result > 0) {
24-
throw new Error(
25-
`The image has ${result} pixel differences.`
26-
)
27+
cy.task('compareSnapshotsPlugin', options).then((percentage) => {
28+
if (percentage > testThreshold) {
29+
throw new Error(`The image difference percentage ${percentage} exceeded the threshold: ${testThreshold}`)
2730
}
2831
})
2932
}

src/plugin.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ async function compareSnapshotsPlugin(args) {
4444
diff.data,
4545
baselineFullCanvas.width,
4646
baselineFullCanvas.height,
47-
{ threshold: process.env.CYPRESS_VISUAL_THRESHOLD || 0.1 }
47+
{ threshold: 0.1 }
4848
)
49+
50+
const percentage = (pixelMismatchResult / diff.width / diff.height) ** 0.5
4951

50-
if (pixelMismatchResult != 0) {
52+
if (percentage > args.threshold) {
5153
diff.pack().pipe(fs.createWriteStream(paths.image.diff(args.testName)))
5254
}
5355

54-
return pixelMismatchResult
56+
return percentage
5557
}
5658

5759
const getCompareSnapshotsPlugin = (on) => {

0 commit comments

Comments
 (0)