forked from E1Edatatracker/protractor-axe-report-plugin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
351 lines (286 loc) · 10.6 KB
/
index.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var handlebars = require('handlebars');
var AxeBuilder = require('@axe-core/webdriverjs');
/**
* When testing your website agains the aXe plugin, you can generate different things in the report
* enabling this plugin in your config file:
*
* exports.config = {
* ...
* plugins: [{
* displayHelpUrl: true|false,
* displayContext: true|false,
* displayPasses: true|false,
* displayViolations: true|false,
* standardsToReport: ['wcag2a', 'wcag2aa'],
* ignoreAxeFailures: true|false,
* package: 'protractor-axe-report-plugin',
* htmlReportPath: '/path/to/reports'|null
* }]
* }
*
*/
const allTestResults = [];
var currentTestResults = [];
var browserName = '';
var pluginConfig = {};
var fileName = '';
const green = '\x1b[32m';
const red = '\x1b[31m';
const grey = '\x1b[37m';
const normalColor = '\x1b[39m';
const indent = ' ';
function setup() {
browser.runAxeTest = runAxeTest;
}
function onPrepare() {
pluginConfig = this.config;
pluginConfig.ignoreAxeFailures = getDefault(pluginConfig.ignoreAxeFailures, false);
pluginConfig.displayHelpUrl = getDefault(pluginConfig.displayHelpUrl, true);
pluginConfig.displayContext = getDefault(pluginConfig.displayContext, true);
pluginConfig.displayPasses = getDefault(pluginConfig.displayPasses, true);
pluginConfig.displayViolations = getDefault(pluginConfig.displayViolations, true);
pluginConfig.standardsToReport = getDefault(pluginConfig.standardsToReport, []);
pluginConfig.htmlReportPath = getDefault(pluginConfig.htmlReportPath, null);
pluginConfig.globalParams = getDefault(pluginConfig.globalParams, {});
}
runAxeTest = function(testName, selector) {
var params = pluginConfig.globalParams;
const builder = new AxeBuilder(browser.driver);
return new Promise((resolve, reject) => {
browser.driver.getCapabilities()
.then((capabilities) => {
browser.getProcessedConfig().then((conf) => {
browserName = capabilities.get('browserName');
specName = conf.specs[0].split('/');
fileName = specName[specName.length - 1].split('.')[0];
if (browserName === 'chrome' || browserName === 'firefox') {
if (params.include) ensureArray(params.include).forEach((item) => builder.include(item));
if (selector) ensureArray(selector).forEach((item) => builder.include(item));
if (params.exclude) ensureArray(params.exclude).forEach((item) => builder.exclude(item));
if (params.options) builder.options(params.options);
builder.analyze().then((results) => {
addResults(testName, browserName, results);
resolve(results);
});
} else {
console.log(`Skipping aXe tests in unsupported browser (${browserName}).`);
resolve();
}
});
});
});
}
function ensureArray(potentialArray) {
return Array.isArray(potentialArray) ? potentialArray : [potentialArray];
}
function postTest(passed, testInfo) {
const pluginConfig = this.config;
const context = this;
if (pluginConfig.ignoreAxeFailures) {
return;
}
var testHeader = 'aXe - ';
// Process the result to remove any standards that we are not interested in
currentTestResults.forEach((result) => {
filterTestResultByStandard(result, pluginConfig.standardsToReport);
});
// If we have > 0 violations, the test fails
// If we have > 0 passes, log a pass
// If we had 0 passes and 0 violations, we can't really say whether this test passed or failed, so say nothing.
var passCount = 0;
var violationCount = 0;
currentTestResults.forEach((result) => {
passCount += result.axeResults.passes.length;
violationCount += result.axeResults.violations.length;
});
if (violationCount > 0) {
// Ideally we'd just log the errorMessage as specName, but the test doesn't let us match by specName - just by error message
var errorMessage = testHeader + testInfo.category + ' - ' + testInfo.name;
context.addFailure(errorMessage, {specName: errorMessage});
} else if (passCount > 0) {
context.addSuccess({specName: testHeader + testInfo.category + ' - ' + testInfo.name});
}
// Clear out the current test results, ready for the next test
currentTestResults = [];
}
function postResults() {
logAllTestResults.bind(this)();
saveReport.bind(this)();
}
function mkdir(dir) {
path.dirname(dir)
.split(path.sep)
.reduce((currentPath, pathSegment) => {
currentPath += pathSegment + path.sep;
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath);
}
return currentPath;
}, '');
}
function getDefault(parameter, defaultValue) {
return parameter == null ? defaultValue : parameter;
}
function addResults(testName, browserName, results) {
allTestResults.push({ name: testName, browser: browserName, axeResults: results });
currentTestResults.push({ name: testName, browser: browserName, axeResults: results });
}
function filterRuleResultsByStandard(ruleResults, standards) {
return ruleResults.reduce(
(results, result) => {
const isResultForStandard = standards.length > 0
? result.tags.some(Array.prototype.includes.bind(standards))
: true;
if (isResultForStandard) {
// remove any references to standards that aren't specified
if (standards.length > 0) {
result.tags = result.tags.filter(Array.prototype.includes.bind(standards));
}
results.push(result);
}
return results;
},
[]
);
}
function filterTestResultByStandard(testResult, standards) {
testResult.axeResults.passes = filterRuleResultsByStandard(testResult.axeResults.passes, standards);
testResult.axeResults.violations = filterRuleResultsByStandard(testResult.axeResults.violations, standards);
return testResult;
}
function getSummaryString(passesLength, violationsLength) {
var passLabel = passesLength === 1 ? ' pass ' : ' passes ';
var violationLabel = violationsLength === 1 ? ' violation' : ' violations';
return passesLength + passLabel + ' and ' + violationsLength + violationLabel;
}
function logViolation(result, displayHelpUrl, displayContext) {
var label = result.nodes.length === 1 ? ' element ' : ' elements ';
console.log(red, 'Fail:', result.help, normalColor);
if (displayHelpUrl) {
console.log(grey + indent + result.helpUrl, normalColor);
}
if (displayContext) {
var htmlTargets = result.nodes.reduce((msg, node) => msg + indent + node.html + '\n\n', '\n');
var msg = grey + indent + result.nodes.length + label + 'failed:' + '\n';
msg += htmlTargets + normalColor;
console.log('');
console.log(msg);
}
}
function logPass(result) {
console.log(green,'Pass:', result.help, normalColor);
}
function logStandardsMessage(standards) {
if (standards == null || standards.length === 0) {
console.log('No filters specified - reporting on all standards');
} else {
console.log('Only returning results for the following standards:', standards);
}
}
function logAllTestResults() {
const testResults = allTestResults.reduce(
(results, testResult) => {
testResult = filterTestResultByStandard(testResult, this.config.standardsToReport);
if (testResult.axeResults.passes.length > 0 || testResult.axeResults.violations.length > 0) {
results.push(testResult);
}
return results;
},
[]
);
console.log('');
console.log('--- Accessibilty test results by test ---');
logStandardsMessage(this.config.standardsToReport);
testResults.forEach((testResult) => {
console.log('');
console.log(normalColor, 'Test:', testResult.name);
console.log(
normalColor,
indent,
getSummaryString(testResult.axeResults.passes.length, testResult.axeResults.violations.length)
);
if (this.config.displayPasses) {
testResult.axeResults.passes.forEach(logPass);
}
if (this.config.displayViolations) {
testResult.axeResults.violations.forEach((result) => {
logViolation(result, this.config.displayHelpUrl, this.config.displayContext);
});
}
});
}
function saveReport() {
if (this.config.htmlReportPath === null) {
return;
}
const htmlTemplateFilename = path.resolve(__dirname, 'report.hbs');
const htmlReportFilename = path.resolve(process.cwd(), this.config.htmlReportPath, `a11y-${browserName}-${fileName}.html`);
const impactSortWeight = [
'minor',
'moderate',
'serious',
'critical'
];
const ruleResultsSortCompare = function(a, b) {
if (a === b) {
return 0;
}
const aIndex = impactSortWeight.indexOf(a.impact);
const bIndex = impactSortWeight.indexOf(b.impact);
return aIndex < bIndex ? 1 : -1;
};
const testResults = allTestResults.reduce(
(results, testResult) => {
testResult = filterTestResultByStandard(testResult, this.config.standardsToReport);
testResult.axeResults.passes.sort(ruleResultsSortCompare);
testResult.axeResults.violations.sort(ruleResultsSortCompare);
if (testResult.axeResults.passes.length > 0 || testResult.axeResults.violations.length > 0) {
results.push(testResult);
}
return results;
},
[]
);
if (testResults.length === 0) {
return;
}
let templateContent;
try {
templateContent = fs.readFileSync(htmlTemplateFilename, 'utf-8');
} catch (e) {
throw new Error(`Something went wrong while trying to load htmlTemplateFilename from ${htmlTemplateFilename} (${e.message})!`);
}
handlebars.registerHelper('link', function(text, url) {
text = handlebars.Utils.escapeExpression(text);
url = handlebars.Utils.escapeExpression(url);
var result = `<a href="${url}" target="_blank">${text}</a>`;
return new handlebars.SafeString(result);
});
handlebars.registerHelper('startCase', function(value) {
return _.startCase(value);
});
handlebars.registerHelper('commaDelimitedList', function(value) {
return value.join(', ');
});
const template = handlebars.compile(templateContent);
const html = template({
testResults,
browserName,
displayViolations: this.config.displayViolations,
displayPasses: this.config.displayPasses
});
try {
mkdir(htmlReportFilename);
fs.writeFileSync(htmlReportFilename, html, 'utf-8');
} catch (e) {
throw new Error(`Something went wrong while trying to write htmlReportFilename to ${htmlReportFilename} (${e.message})!`);
}
}
exports.setup = setup;
exports.onPrepare = onPrepare;
exports.postTest = postTest;
exports.postResults = postResults;
exports.runAxeTest = runAxeTest;