-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
139 lines (130 loc) · 5.15 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
const axeSource = require('axe-core').source;
const axe = require('axe-core');
const _ = require('lodash');
const defaultErrorMessage = 'wdio-axe encountered an error. Check the config and try to re run again.';
class wdioAxe {
parseResult(axeResults, pageUrl, pageTitle, calledMethod = '') {
if (_.has(axeResults, 'violations') && axeResults.violations.length > 0) {
axeResults.violations.map((violation) => {
delete violation.tags;
violation.nodes.map((nodeData) => {
delete nodeData.all;
delete nodeData.none;
if (calledMethod !== 'getBestPractice' && calledMethod !== 'analyseWithTag') {
if (nodeData.any.length > 0) {
violation.message = nodeData.any[0].message;
} else {
violation.message = nodeData.failureSummary;
}
}
violation.html = nodeData.html;
violation.target = JSON.stringify(nodeData.target);
});
delete violation.nodes;
violation.pageUrl = pageUrl;
violation.pageTitle = pageTitle;
});
return axeResults.violations;
}
if (calledMethod === 'getViolations' || calledMethod === 'analyseWithTag'
|| calledMethod === 'analyseWithContext') {
return `No Violations found in this page "${pageUrl}"`;
} if (calledMethod === 'getBestPractice') {
return `Page ("${pageUrl}") is aligned with best practice standards.`;
}
return '';
}
getViolations() {
try {
browser.execute(axeSource);
const pageUrl = browser.getUrl();
const pageTitle = browser.getTitle();
const axeResults = browser.execute(async() => await axe.run({runOnly: {type: 'tags', values: ['wcag2a', 'wcag2aa']}}));
return this.parseResult(axeResults, pageUrl, pageTitle, 'getViolations');
} catch (e) {
throw new Error(defaultErrorMessage);
}
}
getBestPractice() {
try {
browser.execute(axeSource);
const pageUrl = browser.getUrl();
const pageTitle = browser.getTitle();
const axeResults = browser.execute(async() => await axe.run({runOnly: {type: 'tags', values: ['best-practice']}}));
return this.parseResult(axeResults, pageUrl, pageTitle, 'getBestPractice');
} catch (e) {
throw new Error(defaultErrorMessage);
}
}
analyseWithTag(tags) {
if (!Array.isArray(tags)) {
throw new Error('wdio-axe\'s analyseWithTag function require input tags as Array.');
}
try {
const runOnly = {runOnly: {type: 'tags', values: tags}};
browser.execute(axeSource);
const pageUrl = browser.getUrl();
const pageTitle = browser.getTitle();
const axeResults = browser.executeAsync((runOnlyConfig, done) => {
axe.run(runOnlyConfig, ((error, results) => {
if (error) done(error);
done(results);
}));
}, runOnly);
return this.parseResult(axeResults, pageUrl, pageTitle, 'analyseWithTag');
} catch (e) {
throw new Error(defaultErrorMessage);
}
}
analyseWithContext(context) {
if (!Array.isArray(context)) {
throw new Error('wdio-axe\'s analyseWithContext function require input context as Array.');
}
try {
const runOnly = context[0];
browser.execute(axeSource);
const pageUrl = browser.getUrl();
const pageTitle = browser.getTitle();
const axeResults = browser.executeAsync((runOnlyConfig, done) => {
axe.run(runOnlyConfig, ((error, results) => {
if (error) done(error);
done(results);
}));
}, runOnly);
return this.parseResult(axeResults, pageUrl, pageTitle, 'analyseWithContext');
} catch (e) {
throw new Error(defaultErrorMessage);
}
}
getRules(tags = []) {
if (!Array.isArray(tags)) {
throw new Error('wdio-axe\'s getRules function require input tags as Array.');
}
try {
browser.execute(axeSource);
return axe.getRules(tags);
} catch (e) {
throw new Error(defaultErrorMessage);
}
}
runConfig(config) {
if (Array.isArray(config) && (typeof config) === 'object') {
throw new Error('wdio-axe\'s runConfig function require input config as Object.');
}
try {
return axe.configure(config);
} catch (e) {
throw new Error(defaultErrorMessage);
}
}
reset() {
try {
browser.execute(axeSource);
return axe.reset();
} catch (e) {
throw new Error(defaultErrorMessage);
}
}
}
// eslint-disable-next-line new-cap
module.exports = new wdioAxe();