forked from haim-io/cypress-image-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
169 lines (140 loc) · 4.77 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import fs from 'fs-extra'
import pixelmatch from 'pixelmatch'
import { PNG } from 'pngjs'
import {
createDir,
cleanDir,
adjustCanvas,
parseImage,
setFilePermission,
renameAndMoveFile, renameAndCopyFile
} from './utils'
import paths from './config'
import TestStatus from './reporter/test-status'
import { createReport } from './reporter'
let testStatuses = []
const setupFolders = () => {
createDir([paths.dir.baseline, paths.dir.comparison, paths.dir.diff, paths.reportDir])
}
const tearDownDirs = () => {
cleanDir([paths.dir.comparison, paths.dir.diff, paths.reportDir])
}
const generateReport = (instance = '') => {
if (testStatuses.length > 0) {
createReport({ tests: JSON.stringify(testStatuses), instance })
}
return true
}
const deleteReport = args => {
testStatuses = testStatuses.filter(testStatus => testStatus.name !== args.testName)
return true
}
const copyScreenshot = args => {
// If baseline does not exist, copy comparison image to baseline folder
if (!fs.existsSync(paths.image.baseline(args.testName))) {
fs.copySync(paths.image.comparison(args.testName), paths.image.baseline(args.testName))
}
return true
}
// Delete screenshot from comparison and diff directories
const deleteScreenshot = args => {
if (fs.existsSync(paths.image.comparison(args.testName))) {
fs.unlinkSync(paths.image.comparison(args.testName))
}
if (fs.existsSync(paths.image.diff(args.testName))) {
fs.unlinkSync(paths.image.diff(args.testName))
}
return true
}
async function compareSnapshotsPlugin(args) {
const baselineImg = await parseImage(paths.image.baseline(args.testName))
const comparisonImg = await parseImage(paths.image.comparison(args.testName))
const diff = new PNG({
width: Math.max(comparisonImg.width, baselineImg.width),
height: Math.max(comparisonImg.height, baselineImg.height),
})
const baselineFullCanvas = await adjustCanvas(
baselineImg,
diff.width,
diff.height
)
const comparisonFullCanvas = await adjustCanvas(
comparisonImg,
diff.width,
diff.height
)
const pixelMismatchResult = pixelmatch(
baselineFullCanvas.data,
comparisonFullCanvas.data,
diff.data,
diff.width,
diff.height,
{ threshold: 0.1 }
)
const percentage = (pixelMismatchResult / diff.width / diff.height) ** 0.5
const testFailed = percentage > args.testThreshold
if (testFailed) {
fs.ensureFileSync(paths.image.diff(args.testName))
diff.pack().pipe(fs.createWriteStream(paths.image.diff(args.testName)))
}
// Saving test status object to build report if task is triggered
testStatuses.push(new TestStatus({
status: !testFailed,
name: args.testName,
percentage,
failureThreshold: args.testThreshold
}))
return percentage
}
const getCompareSnapshotsPlugin = (on, config) => {
// Create folder structure
setupFolders()
// Delete comparison, diff images and generated reports to ensure a clean run
tearDownDirs()
// Force screenshot resolution to keep consistency of test runs across machines
on('before:browser:launch', (browser, launchOptions) => {
const width = config.viewportWidth || '1280'
const height = config.viewportHeight || '720'
if (browser.name === 'chrome') {
launchOptions.args.push(`--window-size=${width},${height}`)
launchOptions.args.push('--force-device-scale-factor=1')
}
if (browser.name === 'electron') {
// eslint-disable-next-line no-param-reassign
launchOptions.preferences.width = Number.parseInt(width, 10)
// eslint-disable-next-line no-param-reassign
launchOptions.preferences.height = Number.parseInt(height, 10)
}
if (browser.name === 'firefox') {
launchOptions.args.push(`--width=${width}`)
launchOptions.args.push(`--height=${height}`)
}
return launchOptions
})
// Intercept cypress screenshot and create a new image with our own
// name convention and file structure for simplicity and consistency
on('after:screenshot', details => {
// A screenshot could be taken automatically due to a test failure
// and not a call to cy.compareSnapshot / cy.screenshot. These files
// should be left alone
if (details.testFailure) {
return;
}
// Change screenshots file permission so it can be moved from drive to drive
setFilePermission(details.path, 0o777)
setFilePermission(paths.image.comparison(details.name), 0o777)
if (config.env.preserveOriginalScreenshot === true) {
renameAndCopyFile(details.path, paths.image.comparison(details.name))
} else {
renameAndMoveFile(details.path, paths.image.comparison(details.name))
}
})
on('task', {
compareSnapshotsPlugin,
copyScreenshot,
deleteScreenshot,
generateReport,
deleteReport,
})
}
module.exports = getCompareSnapshotsPlugin