Skip to content

Commit

Permalink
Merge pull request #80 from RasaHQ/feature/cypress-accessibility-testing
Browse files Browse the repository at this point in the history
test(accessibilityTesting):Proof of concept to check accessibility in…
  • Loading branch information
csunjka authored Aug 26, 2024
2 parents 41bdb75 + b77c455 commit 98a760c
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 5 deletions.
48 changes: 46 additions & 2 deletions e2e/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { chatbotWidgetPage } from '@rasa-cypress-POM/chatbotWidgetPOM';
import { addCommands } from 'cypress/plugins/mockSocketIO/commands';
import { rasaChatbotPropertySettings } from './types';
import 'cypress-axe';
// ***********************************************
// This example commands.ts shows you how to
// create various custom commands and overwrite
Expand Down Expand Up @@ -55,8 +56,6 @@ Cypress.Commands.add(
document
.getElementsByTagName('rasa-chatbot-widget')[0]
.setAttribute(rasaProp.key, rasaProp.value);
console.log(rasaProp.key);
console.log(rasaProp.value);
}
});
},
Expand All @@ -65,6 +64,51 @@ Cypress.Commands.add(
}
);

/**
* Check accessibility and generate report
*/
Cypress.Commands.add('checkA11yAndReport', (context, options?) => {
cy.checkA11y(context, options, (violations) => {
cy.writeFile(
`cypress/accessibilityReports/${context.selector}.html`,
formatViolations(violations)
);
});
});

function formatViolations(violations) {
const violationsHtml = violations
.map((violation) => {
const nodesHtml = violation.nodes
.map((node) => {
const target = node.target.join(', ');
const html = node.html;
const summary = node.failureSummary;
return `<li><strong>${target}</strong>: ${html}<br><em>${summary}</em></li>`;
})
.join('');

return `
<h2>${violation.id}: ${violation.impact}</h2>
<p>${violation.description}</p>
<ul>${nodesHtml}</ul>
`;
})
.join('');

return `
<html>
<head>
<title>Accessibility Report</title>
</head>
<body>
<h1>Accessibility Violations</h1>
${violationsHtml}
</body>
</html>
`;
}

addCommands();

//
Expand Down
3 changes: 3 additions & 0 deletions e2e/cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ import './commands';

// Cypress visual regression plugin
import '@frsource/cypress-plugin-visual-regression-diff';

// Cypress-axe plugin
import 'cypress-axe';
1 change: 1 addition & 0 deletions e2e/cypress/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ declare global {
expected: Object,
options: Object
): Chainable<Object>;
checkA11yAndReport(context, options?): Chainable<void>;
}
}
}
Loading

0 comments on commit 98a760c

Please sign in to comment.