@nice-digital/wdio-cucumber-steps
Version:
Shared step definitions for Cucumber JS BDD tests in WebdriverIO
75 lines • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getErrorMessage = exports.checkForAccessibilityIssues = void 0;
const axe_core_1 = require("axe-core");
// interface AxeWindow {
// axe: {
// run: typeof run;
// };
// }
/**
* Check if the page has accessibility issues.
*/
async function checkForAccessibilityIssues(level) {
let levels = ["wcag2a", "wcag2aa", "best-practice"];
switch (level) {
case "A":
levels = ["wcag2a", "best-practice"];
break;
case "AA":
default:
break;
}
// inject the axe script source
await browser.execute(axe_core_1.source);
// run inside browser and get results
// Some truly horrible casting here because of this bug: https://github.com/webdriverio/webdriverio/issues/6206
const results = (await browser.executeAsync(function checkForAccessibilityIssues(levels, done) {
const options = {
runOnly: {
type: "tag",
values: levels,
},
};
window.axe.run(options, function (err, results) {
if (err)
done(err);
else
done(results);
});
}, levels));
// const results = (await browser.executeAsync(
// `function checkForAccessibilityIssues (levels, done) {
// var options = {
// runOnly: {
// type: "tag",
// values: levels,
// },
// };
// axe.run(options, function (err, results) {
// if (err) done(err);
// else done(results);
// });
// }`,
// levels
// )) as AxeResults | Error;
if (results instanceof Error)
throw results;
if (results.violations.length > 0) {
const message = await (0, exports.getErrorMessage)(results.violations);
throw new Error(message);
}
expect(results.violations).toHaveLength(0);
}
exports.checkForAccessibilityIssues = checkForAccessibilityIssues;
const getErrorMessage = async (violations) => {
const errors = violations
.map((violation) => {
const elements = violation.nodes.map((node) => node.html).join(", ");
return `${violation.help} | ${elements}\n (see ${violation.helpUrl})`;
})
.join("\n - ");
return `Found ${violations.length} accessibility errors on ${await browser.getUrl()}:\n - ${errors}`;
};
exports.getErrorMessage = getErrorMessage;
//# sourceMappingURL=checkForAccessibilityIssues.js.map