Skip to content

Commit e14693d

Browse files
authored
Merge pull request #21 from devAvengersnl/master
Add a flag to conditionally fail the tests on a11y violations
2 parents 60e89ed + 1678f45 commit e14693d

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Update `Cypress/support/index.js` file to include the cypress-axe commands by ad
2525
import 'cypress-axe'
2626
```
2727

28+
### Add a task to log the messages to the terminal when the cypress executes the spec files
29+
30+
[Example - configuring log task](https://docs.cypress.io/api/commands/task.html#Usage)
2831

2932
## Commands
3033

@@ -103,6 +106,10 @@ it('Has no a11y violations after button click', () => {
103106
})
104107
```
105108

109+
Optionally you can also pass additional argument `skipFailures` to disable the failures and only log them to the console output
110+
111+
Reference : https://github.com/avanslaars/cypress-axe/issues/17
112+
106113
## Output
107114

108115
When accessibility violations are detected, your test will fail and an entry titled "A11Y ERROR!" will be added to the command log for each type of violation found (they will be above the failed assertion). Clicking on those will reveal more specifics about the error in the DevTools console.

index.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Cypress.Commands.add('configureAxe', (configurationOptions = {}) => {
1111
})
1212
})
1313

14-
Cypress.Commands.add('checkA11y', (context, options, violationCallback) => {
14+
Cypress.Commands.add('checkA11y', (context, options, violationCallback, skipFailures = false) => {
1515
cy.window({ log: false })
1616
.then(win => {
1717
if (isEmptyObjectorNull(context)) context = undefined
@@ -43,13 +43,31 @@ Cypress.Commands.add('checkA11y', (context, options, violationCallback) => {
4343
return cy.wrap(violations, { log: false })
4444
})
4545
.then(violations => {
46-
assert.equal(
47-
violations.length,
48-
0,
49-
`${violations.length} accessibility violation${
50-
violations.length === 1 ? '' : 's'
51-
} ${violations.length === 1 ? 'was' : 'were'} detected`
52-
)
46+
if(!skipFailures) {
47+
assert.equal(
48+
violations.length,
49+
0,
50+
`${violations.length} accessibility violation${
51+
violations.length === 1 ? '' : 's'
52+
} ${violations.length === 1 ? 'was' : 'were'} detected`
53+
)
54+
} else {
55+
cy.task('log', violations.length === 0 ? "No violations were detected!": `${violations.length} accessibility violation${
56+
violations.length === 1 ? '' : 's'
57+
} ${violations.length === 1 ? 'was' : 'were'} detected`);
58+
let header = "\n\n---------|impact|\t id|\t help|\t helpUrl|---------\n";
59+
header = header + "----------------------------------------------------------\n";
60+
for(let v = 0 ; v < violations.length ; v++) {
61+
if (v == 0) {
62+
vDetail = header + `|${violations[v].impact}| ${violations[v].id}| ${violations[v].help}| ${violations[v].helpUrl}|\n`;
63+
} else {
64+
vDetail = vDetail + `|${violations[v].impact}| ${violations[v].id}| ${violations[v].help}| ${violations[v].helpUrl}|\n`;
65+
}
66+
}
67+
if (violations.length > 0) {
68+
cy.task('log', vDetail);
69+
}
70+
}
5371
})
5472
})
5573

0 commit comments

Comments
 (0)