forked from jotty-mcclure/cypress-attest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (66 loc) · 2.67 KB
/
index.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
Cypress.Commands.add('injectAttest', () => {
cy.window({ log: false }).then(async window => {
return await cy.exec(`node ./${__dirname}/attest.js`).then(({stdout}) => {
window.eval(stdout);
});
});
})
Cypress.Commands.add('configureAttest', (configurationOptions = {}) => {
cy.window({ log: false }).then(win => {
return win.axe.configure(configurationOptions)
})
})
Cypress.Commands.add('checkA11y', (reporter={}, context, options) => {
cy.window({ log: false })
.then(win => {
if (isEmptyObjectorNull(context)) context = undefined
if (isEmptyObjectorNull(options)) options = undefined
return win.axe.run(
context ? (context = context) : (context = win.document),
options
);
})
.then((results) => {
if ( !reporter ) return cy.wrap(results.violations, { log: false });
return cy.task('logResults', {
reportName: reporter.reportName || '',
reportDirectory: reporter.directory || './cy-a11y-results',
pageName: reporter.scopeName || '',
results: results
}, { log: false }).then(reportPath => {
Cypress.log({
name: 'A11y Report',
message: `A report with the accessibility findings has been created! View console for the location.`,
consoleProps: () => {
return {
message: `A report of the accessibility findings is available here: file://${reportPath}`
}
},
});
return cy.wrap(results.violations, { log: false });
});
})
.then((violations) => {
if (violations.length) {
cy.wrap(violations, { log: false }).each(v => {
Cypress.log({
name: 'a11y error!',
consoleProps: () => v,
message: `${v.id} on ${v.nodes.length} Node${v.nodes.length === 1 ? '' : 's'}`
});
})
}
return cy.wrap(violations, { log: false });
})
.then(violations => {
assert.equal(
violations.length,
0,
`${violations.length} accessibility violation${violations.length === 1 ? '' : 's'} ${violations.length === 1 ? 'was' : 'were'} detected.`
);
});
})
function isEmptyObjectorNull(value) {
if (value == null) return true
return Object.entries(value).length === 0 && value.constructor === Object
}