-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
pa11y.js
148 lines (130 loc) · 3.86 KB
/
pa11y.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* eslint-disable no-console */
const puppeteer = require('puppeteer');
const pa11y = require('pa11y');
const shitlistCheck = require('./pa11y-utilities.js').shitlistCheck;
const shitlist = require('./../a11y_shitlist.json');
const NUMBER_OF_BROWSERS = 5;
async function runPa11y() {
const browsers = [
{
browser: await puppeteer.launch(),
taken: new Promise((resolve) => {
resolve();
}),
},
{
browser: await puppeteer.launch(),
taken: new Promise((resolve) => {
resolve();
}),
},
{
browser: await puppeteer.launch(),
taken: new Promise((resolve) => {
resolve();
}),
},
{
browser: await puppeteer.launch(),
taken: new Promise((resolve) => {
resolve();
}),
},
{
browser: await puppeteer.launch(),
taken: new Promise((resolve) => {
resolve();
}),
},
];
await browsers.forEach(async (instance) => {
instance.page = await instance.browser.newPage();
});
let browserIndex = 0;
const results = [];
const setupBrowser = browsers[0].browser;
const page = await setupBrowser.newPage();
await page.goto('http://localhost:3000');
const urls = Array.from(
await page.evaluate(() =>
[...document.querySelectorAll('a')].map((element) =>
element.getAttribute('href'),
),
),
);
urls.forEach((path) => {
const currentBrowser = browsers[browserIndex % NUMBER_OF_BROWSERS];
browserIndex++;
currentBrowser.taken = currentBrowser.taken.then(async () => {
console.log('Testing ', path);
results.push(
await pa11y(`http://localhost:3000${path}`, {
browser: currentBrowser.browser,
ignore: ['WCAG2AA.Principle1.Guideline1_4.1_4_3.G18.Fail'],
}),
);
});
});
await Promise.all(browsers.map((instance) => instance.taken));
await Promise.all(browsers.map((instance) => instance.browser.close()));
return results;
}
(async () => {
let rawResults;
try {
rawResults = await runPa11y();
if (rawResults.length === 0) {
throw new Error('Component URLs could not be crawled');
}
} catch (error) {
console.log(error);
process.exit(1);
}
const {results, remainingIssues} = shitlistCheck(rawResults, shitlist);
if (remainingIssues) {
console.log(
`
========================================================================
The following items were fixed, and therefore should be removed from the shitlist.
Please edit the file a11y_shitlist.json to remove them and run these tests again.',
========================================================================
`,
);
remainingIssues.forEach((issue) => {
console.log(
'------------------------------------------------------------------------',
);
console.log(issue.pageUrl);
console.log(
'------------------------------------------------------------------------',
);
console.log(JSON.stringify(issue.issues, null, 2));
});
}
console.log(
`
========================================================================
The following issues were discovered and need to be fixed before this code can be merged
========================================================================
`,
);
if (results.length) {
results.forEach((result) => {
console.log(
'------------------------------------------------------------------------',
);
console.log(result.pageUrl);
console.log(
'------------------------------------------------------------------------',
);
console.log(JSON.stringify(result.issues, null, 2));
});
} else {
console.log('No issues!');
}
if (results.length || remainingIssues) {
// This is temporarily returning 0 while we ensure pa11y doesn't throw any false positives
process.exit(0);
}
process.exit(0);
})();