This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
285 lines (240 loc) · 8.21 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
var q = require('q'),
fs = require('fs'),
path = require('path'),
_ = require('lodash'),
request = require('request'),
AxeBuilder = require('axe-webdriverjs'),
Entities = require('html-entities').XmlEntities;
/**
* You can audit your website against the Chrome Accessibility Developer Tools,
* Tenon.io, or both by enabling this plugin in your config file:
*
* // Chrome Accessibility Developer Tools:
* exports.config = {
* ...
* plugins: [{
* chromeA11YDevTools: {
* treatWarningsAsFailures: true
* },
* path: 'node_modules/protractor.plugins/accessiblity'
* }]
* }
*
* // Tenon.io:
*
* // Read about the Tenon.io settings and API requirements:
* // -http://tenon.io/documentation/overview.php
*
* exports.config = {
* ...
* plugins: [{
* tenonIO: {
* options: {
* // See http://tenon.io/documentation/understanding-request-parameters.php
* // options.src will be added by the test.
* },
* printAll: false, // whether the plugin should log API response
* },
* chromeA11YDevTools: false,
* path: 'node_modules/protractor/plugins/accessiblity'
* }]
* }
*
*/
var AUDIT_FILE = require.resolve('accessibility-developer-tools/dist/js/axs_testing.js');
var TENON_URL = 'http://www.tenon.io/api/';
/**
* Checks the information returned by the accessibility audit(s) and
* displays passed/failed results as console output.
*
* @this {Object} The plugin context object
* @return {q.Promise} A promise which resolves when all audits are finished
* @public
*/
function teardown() {
var audits = [];
if (this.config.axe) {
audits.push(runAxe(this));
}
if (this.config.chromeA11YDevTools) {
audits.push(runChromeDevTools(this));
}
// check for Tenon config and an actual API key, not the placeholder
if (this.config.tenonIO && /[A-Za-z][0-9]/.test(
this.config.tenonIO.options.key)) {
audits.push(runTenonIO(this));
}
return q.all(audits);
}
var entities = new Entities();
/**
* Audits page source against the Tenon API, if configured. Requires an API key:
* more information about licensing and configuration available at
* http://tenon.io/documentation/overview.php.
*
* @param {Object} context The plugin context object
* @return {q.Promise} A promise which resolves when the audit is finished
* @private
*/
function runTenonIO(context) {
return browser.driver.getPageSource().then(function(source) {
var options = _.assign(context.config.tenonIO.options, {src: source});
// setup response as a deferred promise
var deferred = q.defer();
request.post({
url: TENON_URL,
form: options
},
function(err, httpResponse, body) {
if (err) { return resolve.reject(new Error(err)); }
else { return deferred.resolve(JSON.parse(body)); }
});
return deferred.promise.then(function(response) {
return processTenonResults(response);
});
});
function processTenonResults(response) {
var testHeader = 'Tenon.io - ';
if (!response.resultSet) {
if (response.code === 'daily_limit_reached') {
console.log(testHeader, 'Daily limit reached');
console.log(response.moreInfo);
}
else {
console.log('Tenon.io error');
}
return;
}
var numResults = response.resultSet.length;
if (numResults === 0) {
context.addSuccess();
return;
}
if (context.config.tenonIO.printAll) {
console.log('\x1b[32m', testHeader + 'API response', '\x1b[39m');
console.log(response);
}
return response.resultSet.forEach(function(result) {
var ref = (result.ref === null) ? '' : result.ref;
context.addFailure(result.errorDescription + '\n\n' +
'\t\t' +entities.decode(result.errorSnippet) +
'\n\n\t\t' + ref, {specName: testHeader + result.errorTitle});
});
}
}
/**
* Audits page source against the Chrome Accessibility Developer Tools, if configured.
*
* @param {Object} context The plugin context object
* @return {q.Promise} A promise which resolves when the audit is finished
* @private
*/
function runChromeDevTools(context) {
var data = fs.readFileSync(AUDIT_FILE, 'utf-8');
data = data + ' var configuration = new axs.AuditConfiguration(' + JSON.stringify(context.config.chromeA11YDevTools.auditConfiguration) + '); return axs.Audit.run(configuration);';
var elementPromises = [],
elementStringLength = 200;
function trimText(text) {
if (text.length > elementStringLength) {
return text.substring(0, elementStringLength / 2) + ' ... '
+ text.substring(text.length - elementStringLength / 2);
} else {
return text;
}
}
var testHeader = 'Chrome A11Y - ';
return browser.executeScriptWithDescription(data, 'a11y developer tool rules').then(function(results) {
var audit = results.map(function(result) {
var DOMElements = result.elements;
if (DOMElements !== undefined) {
DOMElements.forEach(function(elem) {
// get elements from WebDriver, add to promises array
elementPromises.push(
elem.getAttribute('outerHTML').then(function(text) {
return {
code: result.rule.code,
list: trimText(text)
};
},
function(reason){
return {
code: result.rule.code,
list: reason
};
})
);
});
result.elementCount = DOMElements.length;
}
return result;
});
// Wait for element names to be fetched
return q.all(elementPromises).then(function(elementFailures) {
return audit.forEach(function(result, index) {
if (result.result === 'FAIL') {
var label = result.elementCount === 1 ? ' element ' : ' elements ';
if (result.rule.severity !== 'Warning'
|| context.config.chromeA11YDevTools.treatWarningsAsFailures) {
result.warning = false;
} else {
result.warning = true;
result.rule.heading = '\x1b[33m(WARNING) '
+ result.rule.heading + ' (' + result.elementCount
+ label + 'failed)';
}
result.output = '\n\t\t' + result.elementCount + label + 'failed:';
// match elements returned via promises
// by their failure codes
elementFailures.forEach(function(element, index) {
if (element.code === result.rule.code) {
result.output += '\n\t\t' + elementFailures[index].list;
}
});
result.output += '\n\n\t\t' + result.rule.url;
(result.warning ? context.addWarning : context.addFailure)(
result.output, {specName: testHeader + result.rule.heading});
}
else {
context.addSuccess({specName: testHeader + result.rule.heading});
}
});
});
});
}
/**
* Audits page source against aXe: https://github.com/dequelabs/axe-core
*
* @param {Object} context The plugin context object
* @return {q.Promise} A promise which resolves when the audit is finished
* @private
*/
function runAxe(context) {
var deferred = q.defer();
AxeBuilder(browser.driver).options(context.config.axe)
.analyze(function(results) {
deferred.resolve(results);
});
return deferred.promise.then(function(results) {
return processAxeResults(results);
});
function processAxeResults(results) {
var testHeader = 'aXe - '
var numResults = results.violations.length;
if (numResults === 0) {
return context.addSuccess();
}
results.passes.forEach(function(result) {
context.addSuccess({specName: testHeader + result.help});
});
return results.violations.forEach(function(result) {
var label = result.nodes.length === 1 ? ' element ' : ' elements ';
var msg = result.nodes.reduce(function(msg, node) {
return msg + '\t\t' + node.html + '\n';
}, '\n');
msg = '\n\t\t' + result.nodes.length + label + 'failed:' + msg + '\n\n\t\t' + result.helpUrl
context.addFailure(msg, {specName: testHeader + result.help});
});
}
}
// Export
exports.teardown = teardown;