Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a flag to conditionally fail the tests on a11y violations #21

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -38,13 +38,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