-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
246 lines (238 loc) · 7.87 KB
/
script.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
/*
© 2024 CVS Health and/or one of its affiliates. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
script.js
Creates and returns a script to perform the tests for issues.
*/
// IMPORTS
// Module to handle secrets.
require('dotenv').config();
// Devices recognized by Playwright.
const {devices} = require('playwright');
// Utility module.
const {isToolID, tools} = require('./procs/util');
// ########## FUNCTIONS
// Returns whether a device ID is recognized by Playwright.
const isDeviceID = exports.isDeviceID = deviceID => deviceID === 'default' || !! devices[deviceID];
// Returns options for a new browser context (window).
const getWindowOptions = deviceID => {
// If the argument is valid:
if (isDeviceID(deviceID)) {
// Set the reducedMotion option.
const options = {
reducedMotion: 'no-preference'
};
// If the default device was specified:
if (deviceID === 'default') {
// Return the set option.
return options;
}
// Otherwise, i.e. if a non-default device was specified:
else {
// Get its properties.
const deviceProperties = devices[deviceID];
// Return the reduce-motion and device options.
return {
... options,
... deviceProperties
};
}
}
// Otherwise, i.e. if the arguments are not valid:
else {
// Return this.
return null;
}
};
// Creates and returns a script.
exports.script = (id, what, deviceID, options = {}) => {
// If the arguments are valid:
if (id && what && isDeviceID(deviceID)) {
const toolsRulesData = {};
// If options are specified:
const {type, specs} = options;
if (type && specs) {
// If the option type is tools and is valid:
if (
type === 'tools'
&& Array.isArray(specs)
&& specs.length
&& specs.every(spec => isToolID(spec))
) {
// Initialize the data on tools and rules.
specs.forEach(spec => {
toolsRulesData[spec] = [];
});
}
// Otherwise, if the option type is issues and is valid:
else if (
type === 'issues'
&& typeof specs === 'object'
&& specs.issues
&& specs.issueIDs
&& typeof specs.issues === 'object'
&& Array.isArray(specs.issueIDs)
&& specs.issueIDs.length
) {
const {issueIDs, issues} = specs;
// For each specified issue:
issueIDs.forEach(issueID => {
const issueData = issues[issueID];
// If it exists in the classification:
if (issueData) {
const issueToolIDs = Object.keys(issueData.tools);
// For each tool that tests for the issue:
issueToolIDs.forEach(issueToolID => {
toolsRulesData[issueToolID] ??= [];
const toolRuleIDs = toolsRulesData[issueToolID];
const toolData = issueData.tools[issueToolID];
// For each of the rules of the tool for the issue:
Object.keys(toolData).forEach(ruleID => {
// Add the rule to the data on tools and rules.
let rulePrefix = '';
if (issueToolID === 'nuVal') {
rulePrefix = toolData[ruleID].variable ? '~' : '=';
}
const fullRuleID = `${rulePrefix}${ruleID}`;
if (! toolRuleIDs.includes(fullRuleID)) {
toolRuleIDs.push(fullRuleID);
}
});
});
}
// Otherwise, i.e. if it does not exist in the classification:
else {
// Report this and quit.
console.log(`ERROR: Issue ${issueID} not in issue classification`);
return null;
}
});
}
// Otherwise, i.e. if the option specification is invalid:
else {
// Report this and quit.
console.log(`ERROR: Options invalid`);
return null;
}
}
// Otherwise, i.e. if options are not specified:
else {
// Populate the data on tools and rules.
Object.keys(tools).forEach(toolID => {
toolsRulesData[toolID] = [];
});
}
// Initialize a script.
const scriptObj = {
id,
what,
strict: false,
standard: 'only',
observe: false,
device: {
id: deviceID,
windowOptions: {}
},
browserID: 'webkit',
timeLimit: Math.round(50 + 30 * Object.keys(toolsRulesData).length),
creationTimeStamp: '',
executionTimeStamp: '',
target: {
what: '',
url: ''
},
sources: {
script: id,
batch: '',
mergeID: '',
requester: ''
},
acts: []
};
// Add the window options to the script.
scriptObj.device.windowOptions = getWindowOptions(deviceID);
// For each tool used:
Object.keys(toolsRulesData).forEach(toolID => {
// Initialize a test act for it.
const toolAct = {
type: 'test',
launch: {},
which: toolID,
what: tools[toolID]
};
// If rules were specified:
const ruleIDs = toolsRulesData[toolID];
if (ruleIDs.length) {
// Add a rules array as a property to the act.
toolAct.rules = ruleIDs;
// If the tool is QualWeb:
if (toolID === 'qualWeb') {
// For each QualWeb module:
const specs = [];
const prefixes = {
act: 'QW-ACT-R',
wcag: 'QW-WCAG-T',
best: 'QW-BP'
};
Object.keys(prefixes).forEach(prefix => {
// Specify the rules of that module to be tested for.
const ids = toolAct.rules.filter(id => id.startsWith(prefixes[prefix]));
const integers = ids.map(id => id.slice(prefixes[prefix].length));
specs.push(`${prefix}:${integers.join(',')}`);
});
// Replace the generic rule list with the QualWeb-format list.
toolAct.rules = specs;
}
// Otherwise, if the tool is Testaro:
else if (toolID === 'testaro') {
// Prepend the inclusion option to the rule array.
toolAct.rules.unshift('y');
}
}
// Add any needed option defaults to the act.
if (toolID === 'axe') {
toolAct.detailLevel = 2;
}
else if (toolID === 'ibm') {
toolAct.withItems = true;
toolAct.withNewContent = true;
}
else if (toolID === 'qualWeb') {
toolAct.withNewContent = false;
}
else if (toolID === 'testaro') {
toolAct.withItems = true;
toolAct.stopOnFail = false;
}
else if (toolID === 'wave') {
toolAct.reportType = 4;
}
// Add the act to the script.
scriptObj.acts.push(toolAct);
});
// Return the script.
return scriptObj;
}
// Otherwise, i.e. if the arguments are not valid:
else {
// Report this and quit.
console.log(`ERROR: Arguments invalid`);
return null;
}
}