forked from haim-io/cypress-image-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.js
65 lines (55 loc) · 2.11 KB
/
command.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
import { recurse } from 'cypress-recurse';
const compareSnapshotCommand = defaultScreenshotOptions => {
const height = Cypress.config('viewportHeight') || 1440
const width = Cypress.config('viewportWidth') || 1980
// Force screenshot resolution to keep consistency of test runs across machines
Cypress.config('viewportHeight', parseInt(height, 10))
Cypress.config('viewportWidth', parseInt(width, 10))
Cypress.Commands.add(
'compareSnapshot',
{ prevSubject: 'optional' },
(subject, name, testThreshold = 0, recurseOptions = {}) => {
const specName = Cypress.spec.name
const testName = `${specName.replace('.js', '')}-${name}`
const defaultRecurseOptions = {
limit: 1,
log: (percentage) => {
const prefix = percentage <= testThreshold ? 'PASS' : 'FAIL'
cy.log(`${prefix}: Image difference percentage ${percentage}`)
},
error: `Image difference greater than threshold: ${testThreshold}`
}
recurse(
() => {
// Clear the comparison/diff screenshots/reports for this test
cy.task('deleteScreenshot', { testName })
cy.task('deleteReport', { testName })
// Take a screenshot and copy to baseline if it does not exist
const objToOperateOn = subject ? cy.get(subject) : cy
objToOperateOn
.screenshot(testName, defaultScreenshotOptions)
.task('copyScreenshot', {
testName,
})
// Compare screenshots
const options = {
testName,
testThreshold,
}
return cy.task('compareSnapshotsPlugin', options)
},
(percentage) => percentage <= testThreshold,
Object.assign({}, defaultRecurseOptions, recurseOptions)
);
}
)
Cypress.Commands.add('hideElement', { prevSubject: 'optional' }, (subject, hide=true) => {
if (hide) {
cy.get(subject).invoke('attr', 'style', `display: none;`)
} else {
cy.get(subject).invoke('attr', 'style', `display: '';`)
}
return undefined
})
}
module.exports = compareSnapshotCommand