From 1678f453a42968c3f80a2aa15c2353676577a7a4 Mon Sep 17 00:00:00 2001 From: Chitrang Natu Date: Mon, 16 Dec 2019 17:36:43 +0100 Subject: [PATCH] fix issue-17 --- README.md | 7 +++++++ index.js | 34 ++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e0c7825..6ab4aec 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/index.js b/index.js index b894222..08206d8 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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); + } + } }) })