forked from elastic/eui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha11y-testing.js
105 lines (90 loc) · 2.8 KB
/
a11y-testing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const chalk = require('chalk');
const puppeteer = require('puppeteer');
const { AxePuppeteer } = require('@axe-core/puppeteer');
const docsPages = async (root, page) => {
const pagesToSkip = [
`${root}#/display/aspect-ratio`, // Has issues with the embedded audio player
`${root}#/layout/accordion` // Has an issue with ARIA attributes
];
return [
root,
...(await page.$$eval('nav a', (anchors) => anchors.map((a) => a.href))),
].filter((link) => !pagesToSkip.includes(link));
};
const printResult = (violations) => {
const violationData = violations.map(
({ id, impact, description, nodes }) => ({
id,
impact,
description,
nodes: nodes.length
}));
console.table(violationData);
}
(async () => {
let totalViolationsCount = 0;
let root = 'http://localhost:9999/';
let browser;
let page;
try {
browser = await puppeteer.launch({ args: ['--no-sandbox'] });
page = await browser.newPage();
await page.setBypassCSP(true);
} catch (e) {
console.log(chalk.red('Failed to setup puppeteer'));
console.log(e);
process.exit(1);
}
try {
await page.goto(root);
} catch (e) {
root = 'http://localhost:8030/';
try {
await page.goto(root);
} catch (e) {
console.log(
chalk.red(
'No local server found. Expecting localhost:9999 or localhost:8030 to resolve.'
)
);
process.exit(1);
}
}
const links = await docsPages(root, page);
for (const link of links) {
await page.goto(link);
const { violations } = await new AxePuppeteer(page)
.options({
runOnly: {
type: 'tag',
values: ['section508', 'wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa']
},
rules: {
'color-contrast': { enabled: false },
'scrollable-region-focusable': { enabled: false },
'frame-title': { enabled: false }, // axe reports 18 page violations, but no <iframe> or <frame> on pages
},
})
.analyze();
if (violations.length > 0) {
totalViolationsCount += violations.length;
const pageName = link.length > 24 ? link.substr(2) : 'the home page';
console.log(chalk.red(`Errors on ${pageName}`));
printResult(violations);
}
}
await page.close();
await browser.close();
if (totalViolationsCount > 0) {
const errorsCount = chalk.red(
`${totalViolationsCount} accessibility errors`
);
console.log(`${errorsCount}
Install axe for Chrome or Firefox to debug:
Chrome: https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd
Firefox: https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/`);
process.exit(1);
} else {
console.log(chalk.green('axe found no accessibility errors!'));
}
})().catch((e) => console.error(e));