forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
155 lines (142 loc) · 4.79 KB
/
runner.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
(() => {
// this line is replaced with the test object in preprocessor.js
const testObj = {}; /*tests*/
const ruleId = testObj.rule;
const testName = testObj.description || ruleId + ' test';
function flattenResult(results) {
return {
passes: results.passes[0],
violations: results.violations[0],
incomplete: results.incomplete[0]
};
}
function waitForFrames(context, cb) {
let loaded = 0;
const frames = context.querySelectorAll('iframe');
function loadListener() {
loaded++;
if (loaded === length) {
cb();
}
}
if (!frames.length) {
return cb();
}
for (let index = 0, length = frames.length; index < length; index++) {
frames[index].addEventListener('load', loadListener);
}
}
const fixture = document.getElementById('fixture');
const rule = axe.utils.getRule(ruleId);
if (!rule) {
return;
}
// don't run rules that are deprecated and disabled
const deprecated = rule.tags.indexOf('deprecated') !== -1;
(deprecated && !rule.enabled ? describe.skip : describe)(ruleId, () => {
describe(testName, () => {
function runTest(test, collection) {
if (!test[collection]) {
return;
}
describe(collection, () => {
let nodes;
before(() => {
if (typeof results[collection] === 'object') {
nodes = results[collection].nodes;
}
});
test[collection].forEach(selector => {
it('should find ' + JSON.stringify(selector), () => {
if (!nodes) {
assert(false, 'there are no ' + collection);
return;
}
const matches = nodes.filter(node => {
for (let i = 0; i < selector.length; i++) {
if (node.target[i] !== selector[i]) {
return false;
}
}
return node.target.length === selector.length;
});
matches.forEach(node => {
// remove each node we find
nodes.splice(nodes.indexOf(node), 1);
});
if (matches.length === 0) {
assert(false, 'Element not found');
} else if (matches.length === 1) {
assert(true, 'Element found');
} else {
assert(
false,
'Found ' + matches.length + ' elements which match the target'
);
}
});
});
it('should not return other results', () => {
if (typeof nodes !== 'undefined') {
const targets = nodes.map(node => {
return node.target;
});
// check that all nodes are removed
assert.equal(JSON.stringify(targets), '[]');
} else {
assert.lengthOf(
test[collection],
0,
'there are no ' + collection
);
}
});
});
}
let results;
before(done => {
fixture.innerHTML = testObj.content;
waitForFrames(fixture, () => {
// The setTimeout is a workaround for a Firefox bug. See:
// - https://github.com/dequelabs/axe-core/issues/4556
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1912115
setTimeout(() => {
axe.run(
fixture,
{
/**
* The debug flag helps log errors in a fairly detailed fashion,
* when tests fail in webdriver
*/
debug: true,
performanceTimer: false,
runOnly: { type: 'rule', values: [ruleId] }
},
(err, r) => {
// assert that there are no errors - if error exists a stack trace is logged.
const errStack = err && err.stack ? err.stack : '';
assert.isNull(err, 'Error should be null. ' + errStack);
// assert that result is defined
assert.isDefined(r, 'Results are defined.');
// assert that result has certain keys
assert.hasAnyKeys(r, ['incomplete', 'violations', 'passes']);
// assert incomplete(s) does not have error
r.incomplete.forEach(incomplete => {
assert.isUndefined(incomplete.error);
});
// flatten results
results = flattenResult(r);
done();
}
);
}, 0);
});
});
runTest(testObj, 'passes');
runTest(testObj, 'violations');
if (testObj.incomplete) {
runTest(testObj, 'incomplete');
}
});
});
})();