Skip to content

Commit

Permalink
Merge pull request #21 from devAvengersnl/master
Browse files Browse the repository at this point in the history
Add a flag to conditionally fail the tests on a11y violations
  • Loading branch information
avanslaars committed Mar 25, 2020
2 parents 60e89ed + 1678f45 commit e14693d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Update `Cypress/support/index.js` file to include the cypress-axe commands by ad
import 'cypress-axe'
```

### Add a task to log the messages to the terminal when the cypress executes the spec files

[Example - configuring log task](https://docs.cypress.io/api/commands/task.html#Usage)

## Commands

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

Optionally you can also pass additional argument `skipFailures` to disable the failures and only log them to the console output

Reference : https://github.com/avanslaars/cypress-axe/issues/17

## Output

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.
Expand Down
34 changes: 26 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Cypress.Commands.add('configureAxe', (configurationOptions = {}) => {
})
})

Cypress.Commands.add('checkA11y', (context, options, violationCallback) => {
Cypress.Commands.add('checkA11y', (context, options, violationCallback, skipFailures = false) => {
cy.window({ log: false })
.then(win => {
if (isEmptyObjectorNull(context)) context = undefined
Expand Down Expand Up @@ -43,13 +43,31 @@ Cypress.Commands.add('checkA11y', (context, options, violationCallback) => {
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`
)
if(!skipFailures) {
assert.equal(
violations.length,
0,
`${violations.length} accessibility violation${
violations.length === 1 ? '' : 's'
} ${violations.length === 1 ? 'was' : 'were'} detected`
)
} else {
cy.task('log', violations.length === 0 ? "No violations were detected!": `${violations.length} accessibility violation${
violations.length === 1 ? '' : 's'
} ${violations.length === 1 ? 'was' : 'were'} detected`);
let header = "\n\n---------|impact|\t id|\t help|\t helpUrl|---------\n";
header = header + "----------------------------------------------------------\n";
for(let v = 0 ; v < violations.length ; v++) {
if (v == 0) {
vDetail = header + `|${violations[v].impact}| ${violations[v].id}| ${violations[v].help}| ${violations[v].helpUrl}|\n`;
} else {
vDetail = vDetail + `|${violations[v].impact}| ${violations[v].id}| ${violations[v].help}| ${violations[v].helpUrl}|\n`;
}
}
if (violations.length > 0) {
cy.task('log', vDetail);
}
}
})
})

Expand Down

0 comments on commit e14693d

Please sign in to comment.