-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathhelper.js
More file actions
410 lines (360 loc) · 16.6 KB
/
helper.js
File metadata and controls
410 lines (360 loc) · 16.6 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
const logger = require("../helpers/logger").winstonLogger;
const { API_URL } = require('./constants');
const utils = require('../helpers/utils');
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const os = require('os');
const glob = require('glob');
const helper = require('../helpers/helper');
const { CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS } = require('../helpers/constants');
const { consoleHolder } = require("../testObservability/helper/constants");
const scripts = require('./scripts');
const supportFileContentMap = {}
const HttpsProxyAgent = require('https-proxy-agent');
// Function to log A11Y debugging info to remote server
exports.checkAccessibilityPlatform = (user_config) => {
let accessibility = false;
try {
user_config.browsers.forEach(browser => {
if (browser.accessibility) {
accessibility = true;
}
})
} catch {}
return accessibility;
}
exports.setAccessibilityCypressCapabilities = async (user_config, accessibilityResponse) => {
if (utils.isUndefined(user_config.run_settings.accessibilityOptions)) {
user_config.run_settings.accessibilityOptions = {}
}
user_config.run_settings.accessibilityOptions["authToken"] = accessibilityResponse.data.accessibilityToken;
user_config.run_settings.accessibilityOptions["auth"] = accessibilityResponse.data.accessibilityToken;
user_config.run_settings.accessibilityOptions["scannerVersion"] = accessibilityResponse.data.scannerVersion;
user_config.run_settings.system_env_vars.push(`ACCESSIBILITY_AUTH=${accessibilityResponse.data.accessibilityToken}`)
user_config.run_settings.system_env_vars.push(`ACCESSIBILITY_SCANNERVERSION=${accessibilityResponse.data.scannerVersion}`)
}
exports.isAccessibilitySupportedCypressVersion = (cypress_config_filename) => {
const extension = cypress_config_filename.split('.').pop();
return CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS.includes(extension);
}
exports.createAccessibilityTestRun = async (user_config, framework) => {
try {
if (!this.isAccessibilitySupportedCypressVersion(user_config.run_settings.cypress_config_file) ){
logger.warn(`Accessibility Testing is not supported on Cypress version 9 and below.`)
process.env.BROWSERSTACK_TEST_ACCESSIBILITY = 'false';
user_config.run_settings.accessibility = false;
return;
}
const userName = user_config["auth"]["username"];
const accessKey = user_config["auth"]["access_key"];
let settings = utils.isUndefined(user_config.run_settings.accessibilityOptions) ? {} : user_config.run_settings.accessibilityOptions
const {
buildName,
projectName,
buildDescription
} = helper.getBuildDetails(user_config);
const data = {
'projectName': projectName,
'buildName': buildName,
'startTime': (new Date()).toISOString(),
'description': buildDescription,
'source': {
frameworkName: "Cypress",
frameworkVersion: helper.getPackageVersion('cypress', user_config),
sdkVersion: helper.getAgentVersion(),
language: 'javascript',
testFramework: 'cypress',
testFrameworkVersion: helper.getPackageVersion('cypress', user_config)
},
'settings': settings,
'versionControl': await helper.getGitMetaData(),
'ciInfo': helper.getCiInfo(),
'hostInfo': {
hostname: os.hostname(),
platform: os.platform(),
type: os.type(),
version: os.version(),
arch: os.arch()
},
'browserstackAutomation': process.env.BROWSERSTACK_AUTOMATION === 'true'
};
const config = {
auth: {
username: userName,
password: accessKey
},
headers: {
'Content-Type': 'application/json'
}
};
const response = await nodeRequest(
'POST', 'v2/test_runs', data, config, API_URL
);
if(!utils.isUndefined(response.data)) {
process.env.BS_A11Y_JWT = response.data.data.accessibilityToken;
process.env.BS_A11Y_TEST_RUN_ID = response.data.data.id;
}
if (process.env.BS_A11Y_JWT) {
process.env.BROWSERSTACK_TEST_ACCESSIBILITY = 'true';
}
logger.debug(`BrowserStack Accessibility Automation Test Run ID: ${response.data.data.id}`);
this.setAccessibilityCypressCapabilities(user_config, response.data);
if(user_config.run_settings.auto_import_dev_dependencies != true) helper.setBrowserstackCypressCliDependency(user_config);
} catch (error) {
if (error.response) {
logger.error("Incorrect Cred");
logger.error(
`Exception while creating test run for BrowserStack Accessibility Automation: ${
error.response.status
} ${error.response.statusText} ${JSON.stringify(error.response.data)}
`
);
} else if (error.message === 'Invalid configuration passed.') {
logger.error("Invalid configuration passed.");
logger.error(
`Exception while creating test run for BrowserStack Accessibility Automation: ${
error.message || error.stack
}`
);
for (const errorkey of error.errors) {
logger.error(errorkey.message);
}
} else {
logger.error(
`Exception while creating test run for BrowserStack Accessibility Automation: ${
error.message || error.stack
}`
);
}
// since create accessibility session failed
process.env.BROWSERSTACK_TEST_ACCESSIBILITY = 'false';
user_config.run_settings.accessibility = false;
}
}
const nodeRequest = (type, url, data, config) => {
return new Promise(async (resolve, reject) => {
const options = {
...config,
method: type,
url: `${API_URL}/${url}`,
data: data
};
if(process.env.HTTP_PROXY){
options.proxy = false
options.httpsAgent = new HttpsProxyAgent(process.env.HTTP_PROXY);
} else if (process.env.HTTPS_PROXY){
options.proxy = false
options.httpsAgent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
}
axios(options).then(response => {
if(!(response.status == 201 || response.status == 200)) {
logger.info("response.status in nodeRequest", response.status);
reject(response && response.data ? response.data : `Received response from BrowserStack Server with status : ${response.status}`);
} else {
try {
if(typeof(response.data) !== 'object') body = JSON.parse(response.data);
} catch(e) {
if(!url.includes('/stop')) {
reject('Not a JSON response from BrowserStack Server');
}
}
resolve({
data: response.data
});
}
}).catch(error => {
logger.info("error in nodeRequest", error);
reject(error);
})
});
}
exports.supportFileCleanup = () => {
logger.debug("Cleaning up support file changes added for accessibility.")
Object.keys(supportFileContentMap).forEach(file => {
try {
if(typeof supportFileContentMap[file] === 'object') {
let fileOrDirpath = file;
if(supportFileContentMap[file].deleteSupportDir) {
fileOrDirpath = path.join(process.cwd(), 'cypress', 'support');
}
helper.deleteSupportFileOrDir(fileOrDirpath);
} else {
fs.writeFileSync(file, supportFileContentMap[file], {encoding: 'utf-8'});
}
} catch(e) {
logger.debug(`Error while replacing file content for ${file} with it's original content with error : ${e}`, true, e);
}
});
}
const getAccessibilityCypressCommandEventListener = (extName) => {
return extName == 'js' ? (
`require('browserstack-cypress-cli/bin/accessibility-automation/cypress');`
) : (
`import 'browserstack-cypress-cli/bin/accessibility-automation/cypress'`
)
}
exports.setAccessibilityEventListeners = (bsConfig) => {
try {
const supportFilesData = helper.getSupportFiles(bsConfig, true);
if(!supportFilesData.supportFile) return;
const isPattern = glob.hasMagic(supportFilesData.supportFile);
if(!isPattern) {
logger.debug(`Using user defined support file: ${supportFilesData.supportFile}`);
let file;
try {
file = process.cwd() + supportFilesData.supportFile;
const defaultFileContent = fs.readFileSync(file, {encoding: 'utf-8'});
let cypressCommandEventListener = getAccessibilityCypressCommandEventListener(path.extname(file));
const alreadyIncludes = defaultFileContent.includes(cypressCommandEventListener);
if(!alreadyIncludes) {
let newFileContent = defaultFileContent +
'\n' +
cypressCommandEventListener +
'\n';
fs.writeFileSync(file, newFileContent, {encoding: 'utf-8'});
supportFileContentMap[file] = supportFilesData.cleanupParams ? supportFilesData.cleanupParams : defaultFileContent;
}
} catch(e) {
logger.debug(`Unable to modify file contents for ${file} to set event listeners with error ${e}`, true, e);
}
return;
}
const globPattern = process.cwd() + supportFilesData.supportFile;
glob(globPattern, {}, (err, files) => {
if(err) {
logger.debug('EXCEPTION IN BUILD START EVENT : Unable to parse cypress support files');
return;
}
files.forEach(file => {
try {
const fileName = path.basename(file);
if(['e2e.js', 'e2e.ts', 'component.ts', 'component.js'].includes(fileName) && !file.includes('node_modules')) {
const defaultFileContent = fs.readFileSync(file, {encoding: 'utf-8'});
let cypressCommandEventListener = getAccessibilityCypressCommandEventListener(path.extname(file));
if(!defaultFileContent.includes(cypressCommandEventListener)) {
let newFileContent = defaultFileContent +
'\n' +
cypressCommandEventListener +
'\n';
fs.writeFileSync(file, newFileContent, {encoding: 'utf-8'});
supportFileContentMap[file] = supportFilesData.cleanupParams ? supportFilesData.cleanupParams : defaultFileContent;
}
}
} catch(e) {
logger.debug(`Unable to modify file contents for ${file} to set event listeners with error ${e}`, true, e);
}
});
});
} catch(e) {
logger.debug(`Unable to parse support files to set event listeners with error ${e}`, true, e);
}
}
// Process server accessibility configuration similar to Node Agent
exports.processServerAccessibilityConfig = (responseData) => {
logger.debug('[A11Y] Processing server accessibility configuration', { responseData });
try {
// Use Scripts class to parse server response
scripts.parseFromResponse(responseData);
// Handle the commandsToWrap structure from the server response
if (responseData.accessibility?.options?.commandsToWrap) {
const commandsToWrapData = responseData.accessibility.options.commandsToWrap;
// Extract the actual commands array from the nested structure
const serverCommands = commandsToWrapData.commands || [];
// Store server commands for Cypress to read
process.env.ACCESSIBILITY_COMMANDS_TO_WRAP = JSON.stringify(serverCommands);
logger.debug(`[A11Y] Server provided ${serverCommands.length} commands for wrapping`, { serverCommands });
if (serverCommands.length === 0) {
logger.debug('[A11Y] Server wants build-end-only scanning - command wrapping will be disabled');
process.env.ACCESSIBILITY_BUILD_END_ONLY = 'true';
} else {
logger.debug(`[A11Y] Server wants command-level scanning for: ${serverCommands.map(cmd => cmd.name || cmd).join(', ')}`, { commandList: serverCommands.map(cmd => cmd.name || cmd) });
process.env.ACCESSIBILITY_BUILD_END_ONLY = 'false';
}
// Log scriptsToRun if available (Scripts class handles the actual storage)
if (commandsToWrapData.scriptsToRun) {
logger.debug(`[A11Y] Server provided scripts to run: ${commandsToWrapData.scriptsToRun.join(', ')}`, { scriptsToRun: commandsToWrapData.scriptsToRun });
}
} else {
logger.debug('[A11Y] No server commands provided, using default command list');
process.env.ACCESSIBILITY_BUILD_END_ONLY = 'false';
}
// Process scripts from server response
if (responseData.accessibility?.options?.scripts) {
const serverScripts = responseData.accessibility.options.scripts;
// Convert array of script objects to a map for easier access
const scriptsMap = {};
serverScripts.forEach(script => {
scriptsMap[script.name] = script.command;
});
logger.debug(`[A11Y] Server provided accessibility scripts: ${Object.keys(scriptsMap).join(', ')}`, { scriptsMap });
} else {
logger.debug('[A11Y] No server scripts provided, using default scripts');
}
// Process capabilities for token and other settings
if (responseData.accessibility?.options?.capabilities) {
const capabilities = responseData.accessibility.options.capabilities;
capabilities.forEach(cap => {
if (cap.name === 'accessibilityToken') {
process.env.BS_A11Y_JWT = cap.value;
logger.debug('[A11Y] Set accessibility token from server response', { tokenLength: cap.value?.length || 0 });
} else if (cap.name === 'test_run_id') {
process.env.BS_A11Y_TEST_RUN_ID = cap.value;
logger.debug('[A11Y] Set test run ID from server response', { testRunId: cap.value });
} else if (cap.name === 'testhub_build_uuid') {
process.env.BROWSERSTACK_TESTHUB_UUID = cap.value;
logger.debug('[A11Y] Set TestHub build UUID from server response', { buildUuid: cap.value });
} else if (cap.name === 'scannerVersion') {
process.env.ACCESSIBILITY_SCANNERVERSION = cap.value;
logger.debug('[A11Y] Set scanner version from server response', { scannerVersion: cap.value });
}
});
}
logger.debug('[A11Y] Successfully processed server accessibility configuration');
} catch (error) {
logger.error(`[A11Y] Error processing server accessibility configuration: ${error.message}`);
// Fallback to default behavior
process.env.ACCESSIBILITY_BUILD_END_ONLY = 'false';
}
};
// Check if command should be wrapped based on server response
exports.shouldWrapCommand = (commandName) => {
try {
if (!commandName) {
return false;
}
// Check if we're in build-end-only mode
if (process.env.ACCESSIBILITY_BUILD_END_ONLY === 'true') {
logger.debug(`[A11Y] Build-end-only mode: not wrapping command ${commandName}`, { commandName, mode: 'build-end-only' });
return false;
}
// Use Scripts class to check if command should be wrapped
const shouldWrap = scripts.shouldWrapCommand(commandName);
// If Scripts class has no commands configured, fallback to checking environment
if (!shouldWrap && process.env.ACCESSIBILITY_COMMANDS_TO_WRAP) {
const serverCommands = JSON.parse(process.env.ACCESSIBILITY_COMMANDS_TO_WRAP);
if (Array.isArray(serverCommands) && serverCommands.length > 0) {
const envShouldWrap = serverCommands.some(command => {
return (command.name || command).toLowerCase() === commandName.toLowerCase();
});
logger.debug(`[A11Y] shouldWrapCommand: ${commandName} -> ${envShouldWrap} (env-driven)`, { commandName, shouldWrap: envShouldWrap, source: 'environment' });
return envShouldWrap;
}
}
// If we got a result from Scripts class, use it
if (scripts.commandsToWrap && scripts.commandsToWrap.length > 0) {
logger.debug(`[A11Y] shouldWrapCommand: ${commandName} -> ${shouldWrap} (scripts-driven)`, { commandName, shouldWrap, source: 'scripts-class' });
return shouldWrap;
}
// Fallback to default commands if no server commands
const defaultCommands = ['visit', 'click', 'type', 'request', 'dblclick', 'rightclick', 'clear', 'check', 'uncheck', 'select', 'trigger', 'selectFile', 'scrollIntoView', 'scroll', 'scrollTo', 'blur', 'focus', 'go', 'reload', 'submit', 'viewport', 'origin'];
const defaultShouldWrap = defaultCommands.includes(commandName.toLowerCase());
logger.debug(`[A11Y] shouldWrapCommand: ${commandName} -> ${defaultShouldWrap} (default)`, { commandName, shouldWrap: defaultShouldWrap, source: 'default' });
return defaultShouldWrap;
} catch (error) {
logger.debug(`[A11Y] Error in shouldWrapCommand: ${error.message}`, { commandName, error: error.message });
return false;
}
};
// Export the Scripts instance for direct access
exports.scripts = scripts;