-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtest-accessibility.js
115 lines (97 loc) · 3.2 KB
/
test-accessibility.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
106
107
108
109
110
111
112
113
114
115
// Automated accessibility testing script
const pa11y = require('pa11y');
const htmlReporter = require('pa11y-reporter-html');
const puppeteer = require('puppeteer');
const fs = require('fs');
const reportsPath = 'reports/';
function removeExistingReports() {
const path = reportsPath;
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach((file) => {
const curPath = `${path}/${file}`;
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
function writeReportToHTML(hostUrl, htmlText, fileName) {
const failed = htmlText.indexOf('<span class="count error">0 errors</span>') < 0;
if (!fs.existsSync(reportsPath)) {
fs.mkdirSync(reportsPath);
}
fs.appendFile(`${reportsPath}${fileName}_results.html`, htmlText, (err) => {
if (err) {
return console.log(err);
}
console.log(`Tested on ${hostUrl}`);
console.log(failed ? '\x1b[1m\x1b[31mFAILED\x1b[0m' : 'OK');
console.log(`Results added to ${reportsPath}${fileName}.html`);
});
return failed;
}
// Async function required for us to use await
async function runExample() {
let browser;
const result = [];
const pages = [];
let report = [];
const hostPort = process.argv[2] || 4200;
const hostName = 'devbridge.com';
const hostUrl = `https://${hostName}`;
let hasErrors = false;
try {
browser = await puppeteer.launch();
const testableUrls = [
'/',
'/approach/',
'/contact-us/',
];
for (let index = 0; index < testableUrls.length; index++) {
const testUrl = hostUrl + testableUrls[index];
pages[index] = await browser.newPage();
result[index] = await pa11y(testUrl, {
browser,
wait: 500,
method: 'GET',
standard: 'WCAG2AA',
ignore: [
// Ignored because test cannot detect button type if it's not rendered in DOM.
'WCAG2A.Principle3.Guideline3_2.3_2_2.H32.2',
],
headers: {
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
host: hostName,
},
page: pages[index],
});
report = await htmlReporter.results(result[index], testUrl);
hasErrors |= writeReportToHTML(testUrl, report, index);
}
} catch (error) {
hasErrors = true;
// Output an error if it occurred
console.error(error.message);
}
finally {
// Close the browser instance and pages if they exist
if (pages) {
for (const page of pages) {
await page.close();
}
}
if (browser) {
await browser.close();
}
}
if (hasErrors) {
console.log('\x1b[1m\x1b[31mSome tests FAILED\x1b[0m');
process.exit(1);
} else {
console.log('All PASSED');
}
}
runExample();