-
Notifications
You must be signed in to change notification settings - Fork 14
/
TestA11y.js
52 lines (43 loc) · 2.05 KB
/
TestA11y.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
/// Accessibility Testing
///
/// Uses Axe library
/// References:
/// https://github.com/dequelabs/axe-core/blob/develop/doc/API.md
/// https://dequeuniversity.com/rules/axe/4.2
/* eslint no-undef: "off" */
const axeSource = require('axe-core').source;
/// (Must be executed in webdriver.io context - browser object must exist in global scope.)
/// Performs accessibility testing on the specified URL. If url is undefined/null, will run tests on currently opened page.
module.exports = async function runAccessibilityTest(url) {
if (url) {
await browser.url(url);
}
//console.log(`Running accessibility tests for url [${await browser.getUrl()}]...`);
//---[ Inject the AXE script
await browser.execute(axeSource);
//(If we have sporadic "axe is not defined errors" when doing the run below... we could try waiting until it's available...)
/*await browser.waitUntil(async () => {
return await browser.execute(() => {
return (typeof(axe) !== 'undefined');
}) === true;
}, {
timeout: 5000,
timeoutMsg: 'Timeout (5s) reached waiting for axe to be installed on current browser web page'
});*/
const axeOptions = {runOnly: { type: 'tag', values: ['wcag2a', 'wcag2aa'] }};
//---[ Run axe in browser and get results/error
const results = await browser.executeAsync((options, done) => {
axe.run(options, (err, runResults) => {
if (err) throw err; //done(err);
done(runResults);
});
}, axeOptions);
//---[ Fail test if violations were detected
if (results.violations && results.violations.length > 0) {
console.error(`ACCESSIBILITY TEST for [${results.url}]: ${results.violations.length} rule violations detected (${results.passes.length} passes OK; ${results.inapplicable.length} innaplicable)`);
for (let i=0; i<results.violations.length; i++) {
console.error('> ', results.violations[i]);//, 'nodes:', results.violations[i].nodes);
}
throw new Error('Accessibility test failed.');
}
}