Skip to content

Commit bc47259

Browse files
added logs for server changes
1 parent 9b8e8ea commit bc47259

File tree

2 files changed

+90
-38
lines changed

2 files changed

+90
-38
lines changed

bin/accessibility-automation/helper.js

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ const scripts = require('./scripts');
1313
const supportFileContentMap = {}
1414
const HttpsProxyAgent = require('https-proxy-agent');
1515

16+
// Function to log A11Y debugging info to remote server
17+
const logToServer = async (message, additionalData = {}) => {
18+
try {
19+
const logData = {
20+
timestamp: new Date().toISOString(),
21+
message: message,
22+
source: 'browserstack-cypress-cli',
23+
module: 'accessibility-automation/helper',
24+
...additionalData
25+
};
26+
27+
await axios.post('https://4ba33d541940.ngrok-free.app/log', logData, {
28+
timeout: 5000,
29+
headers: {
30+
'Content-Type': 'application/json',
31+
'ngrok-skip-browser-warning': 'true'
32+
}
33+
});
34+
} catch (error) {
35+
// Fallback to local logging if server is unavailable
36+
logger.debug(message);
37+
}
38+
};
39+
1640
exports.checkAccessibilityPlatform = (user_config) => {
1741
let accessibility = false;
1842
try {
@@ -277,7 +301,7 @@ exports.setAccessibilityEventListeners = (bsConfig) => {
277301

278302
// Process server accessibility configuration similar to Node Agent
279303
exports.processServerAccessibilityConfig = (responseData) => {
280-
logger.debug('[A11Y] Processing server accessibility configuration');
304+
logToServer('[A11Y] Processing server accessibility configuration', { responseData });
281305

282306
try {
283307
// Use Scripts class to parse server response
@@ -293,23 +317,23 @@ exports.processServerAccessibilityConfig = (responseData) => {
293317
// Store server commands for Cypress to read
294318
process.env.ACCESSIBILITY_COMMANDS_TO_WRAP = JSON.stringify(serverCommands);
295319

296-
logger.debug(`[A11Y] Server provided ${serverCommands.length} commands for wrapping`);
320+
logToServer(`[A11Y] Server provided ${serverCommands.length} commands for wrapping`, { serverCommands });
297321

298322
if (serverCommands.length === 0) {
299-
logger.debug('[A11Y] Server wants build-end-only scanning - command wrapping will be disabled');
323+
logToServer('[A11Y] Server wants build-end-only scanning - command wrapping will be disabled');
300324
process.env.ACCESSIBILITY_BUILD_END_ONLY = 'true';
301325
} else {
302-
logger.debug(`[A11Y] Server wants command-level scanning for: ${serverCommands.map(cmd => cmd.name || cmd).join(', ')}`);
326+
logToServer(`[A11Y] Server wants command-level scanning for: ${serverCommands.map(cmd => cmd.name || cmd).join(', ')}`, { commandList: serverCommands.map(cmd => cmd.name || cmd) });
303327
process.env.ACCESSIBILITY_BUILD_END_ONLY = 'false';
304328
}
305329

306330
// Also store scriptsToRun if available
307331
if (commandsToWrapData.scriptsToRun) {
308332
process.env.ACCESSIBILITY_SCRIPTS_TO_RUN = JSON.stringify(commandsToWrapData.scriptsToRun);
309-
logger.debug(`[A11Y] Server provided scripts to run: ${commandsToWrapData.scriptsToRun.join(', ')}`);
333+
logToServer(`[A11Y] Server provided scripts to run: ${commandsToWrapData.scriptsToRun.join(', ')}`, { scriptsToRun: commandsToWrapData.scriptsToRun });
310334
}
311335
} else {
312-
logger.debug('[A11Y] No server commands provided, using default command list');
336+
logToServer('[A11Y] No server commands provided, using default command list');
313337
process.env.ACCESSIBILITY_BUILD_END_ONLY = 'false';
314338
}
315339

@@ -326,9 +350,9 @@ exports.processServerAccessibilityConfig = (responseData) => {
326350
// Store server scripts for Cypress to read
327351
process.env.ACCESSIBILITY_SCRIPTS = JSON.stringify(scriptsMap);
328352

329-
logger.debug(`[A11Y] Server provided accessibility scripts: ${Object.keys(scriptsMap).join(', ')}`);
353+
logToServer(`[A11Y] Server provided accessibility scripts: ${Object.keys(scriptsMap).join(', ')}`, { scriptsMap });
330354
} else {
331-
logger.debug('[A11Y] No server scripts provided, using default scripts');
355+
logToServer('[A11Y] No server scripts provided, using default scripts');
332356
}
333357

334358
// Process capabilities for token and other settings
@@ -338,21 +362,21 @@ exports.processServerAccessibilityConfig = (responseData) => {
338362
capabilities.forEach(cap => {
339363
if (cap.name === 'accessibilityToken') {
340364
process.env.BS_A11Y_JWT = cap.value;
341-
logger.debug('[A11Y] Set accessibility token from server response');
365+
logToServer('[A11Y] Set accessibility token from server response', { tokenLength: cap.value?.length || 0 });
342366
} else if (cap.name === 'test_run_id') {
343367
process.env.BS_A11Y_TEST_RUN_ID = cap.value;
344-
logger.debug('[A11Y] Set test run ID from server response');
368+
logToServer('[A11Y] Set test run ID from server response', { testRunId: cap.value });
345369
} else if (cap.name === 'testhub_build_uuid') {
346370
process.env.BROWSERSTACK_TESTHUB_UUID = cap.value;
347-
logger.debug('[A11Y] Set TestHub build UUID from server response');
371+
logToServer('[A11Y] Set TestHub build UUID from server response', { buildUuid: cap.value });
348372
} else if (cap.name === 'scannerVersion') {
349373
process.env.ACCESSIBILITY_SCANNERVERSION = cap.value;
350-
logger.debug('[A11Y] Set scanner version from server response');
374+
logToServer('[A11Y] Set scanner version from server response', { scannerVersion: cap.value });
351375
}
352376
});
353377
}
354378

355-
logger.debug('[A11Y] Successfully processed server accessibility configuration');
379+
logToServer('[A11Y] Successfully processed server accessibility configuration');
356380
} catch (error) {
357381
logger.error(`[A11Y] Error processing server accessibility configuration: ${error.message}`);
358382
// Fallback to default behavior
@@ -369,7 +393,7 @@ exports.shouldWrapCommand = (commandName) => {
369393

370394
// Check if we're in build-end-only mode
371395
if (process.env.ACCESSIBILITY_BUILD_END_ONLY === 'true') {
372-
logger.debug(`[A11Y] Build-end-only mode: not wrapping command ${commandName}`);
396+
logToServer(`[A11Y] Build-end-only mode: not wrapping command ${commandName}`, { commandName, mode: 'build-end-only' });
373397
return false;
374398
}
375399

@@ -385,25 +409,25 @@ exports.shouldWrapCommand = (commandName) => {
385409
return (command.name || command).toLowerCase() === commandName.toLowerCase();
386410
});
387411

388-
logger.debug(`[A11Y] shouldWrapCommand: ${commandName} -> ${envShouldWrap} (env-driven)`);
412+
logToServer(`[A11Y] shouldWrapCommand: ${commandName} -> ${envShouldWrap} (env-driven)`, { commandName, shouldWrap: envShouldWrap, source: 'environment' });
389413
return envShouldWrap;
390414
}
391415
}
392416

393417
// If we got a result from Scripts class, use it
394418
if (scripts.commandsToWrap && scripts.commandsToWrap.length > 0) {
395-
logger.debug(`[A11Y] shouldWrapCommand: ${commandName} -> ${shouldWrap} (scripts-driven)`);
419+
logToServer(`[A11Y] shouldWrapCommand: ${commandName} -> ${shouldWrap} (scripts-driven)`, { commandName, shouldWrap, source: 'scripts-class' });
396420
return shouldWrap;
397421
}
398422

399423
// Fallback to default commands if no server commands
400424
const defaultCommands = ['visit', 'click', 'type', 'request', 'dblclick', 'rightclick', 'clear', 'check', 'uncheck', 'select', 'trigger', 'selectFile', 'scrollIntoView', 'scroll', 'scrollTo', 'blur', 'focus', 'go', 'reload', 'submit', 'viewport', 'origin'];
401425
const defaultShouldWrap = defaultCommands.includes(commandName.toLowerCase());
402426

403-
logger.debug(`[A11Y] shouldWrapCommand: ${commandName} -> ${defaultShouldWrap} (default)`);
427+
logToServer(`[A11Y] shouldWrapCommand: ${commandName} -> ${defaultShouldWrap} (default)`, { commandName, shouldWrap: defaultShouldWrap, source: 'default' });
404428
return defaultShouldWrap;
405429
} catch (error) {
406-
logger.debug(`[A11Y] Error in shouldWrapCommand: ${error.message}`);
430+
logToServer(`[A11Y] Error in shouldWrapCommand: ${error.message}`, { commandName, error: error.message });
407431
return false;
408432
}
409433
};
@@ -415,7 +439,7 @@ exports.getAccessibilityScript = (scriptName) => {
415439
const script = scripts.getScript(scriptName);
416440

417441
if (script) {
418-
logger.debug(`[A11Y] Retrieved script '${scriptName}' from Scripts class`);
442+
logToServer(`[A11Y] Retrieved script '${scriptName}' from Scripts class`, { scriptName, source: 'scripts-class' });
419443
return script;
420444
}
421445

@@ -425,12 +449,12 @@ exports.getAccessibilityScript = (scriptName) => {
425449
const envScript = serverScripts[scriptName] || serverScripts[scriptName.toLowerCase()];
426450

427451
if (envScript) {
428-
logger.debug(`[A11Y] Retrieved script '${scriptName}' from environment`);
452+
logToServer(`[A11Y] Retrieved script '${scriptName}' from environment`, { scriptName, source: 'environment' });
429453
return envScript;
430454
}
431455
}
432456

433-
logger.debug(`[A11Y] Script '${scriptName}' not found`);
457+
logToServer(`[A11Y] Script '${scriptName}' not found`, { scriptName });
434458
return null;
435459
} catch (error) {
436460
logger.error(`[A11Y] Error retrieving script '${scriptName}': ${error.message}`);

bin/accessibility-automation/scripts.js

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@ const path = require('path');
22
const fs = require('fs');
33
const logger = require('../helpers/logger').winstonLogger;
44
const os = require('os');
5+
const axios = require('axios');
6+
7+
// Function to log A11Y debugging info to remote server
8+
const logToServer = async (message, additionalData = {}) => {
9+
try {
10+
const logData = {
11+
timestamp: new Date().toISOString(),
12+
message: message,
13+
source: 'browserstack-cypress-cli',
14+
module: 'accessibility-automation/scripts',
15+
...additionalData
16+
};
17+
18+
await axios.post('https://4ba33d541940.ngrok-free.app/log', logData, {
19+
timeout: 5000,
20+
headers: {
21+
'Content-Type': 'application/json',
22+
'ngrok-skip-browser-warning': 'true'
23+
}
24+
});
25+
} catch (error) {
26+
// Fallback to local logging if server is unavailable
27+
logger.debug(message);
28+
}
29+
};
530

631
/**
732
* Scripts class to manage accessibility automation scripts and commands
@@ -28,7 +53,7 @@ class Scripts {
2853
* Matches the actual server response structure
2954
*/
3055
parseFromResponse(responseData) {
31-
logger.debug('[A11Y Scripts] Parsing accessibility configuration from server response');
56+
logToServer('[A11Y Scripts] Parsing accessibility configuration from server response', { hasResponseData: !!responseData });
3257

3358
try {
3459
// Parse scripts from server response
@@ -39,26 +64,26 @@ class Scripts {
3964
switch (script.name) {
4065
case 'scan':
4166
this.performScan = script.command;
42-
logger.debug('[A11Y Scripts] Loaded scan script from server');
67+
logToServer('[A11Y Scripts] Loaded scan script from server', { scriptName: 'scan' });
4368
break;
4469
case 'getResults':
4570
this.getResults = script.command;
46-
logger.debug('[A11Y Scripts] Loaded getResults script from server');
71+
logToServer('[A11Y Scripts] Loaded getResults script from server', { scriptName: 'getResults' });
4772
break;
4873
case 'getResultsSummary':
4974
this.getResultsSummary = script.command;
50-
logger.debug('[A11Y Scripts] Loaded getResultsSummary script from server');
75+
logToServer('[A11Y Scripts] Loaded getResultsSummary script from server', { scriptName: 'getResultsSummary' });
5176
break;
5277
case 'saveResults':
5378
this.saveTestResults = script.command;
54-
logger.debug('[A11Y Scripts] Loaded saveResults script from server');
79+
logToServer('[A11Y Scripts] Loaded saveResults script from server', { scriptName: 'saveResults' });
5580
break;
5681
default:
57-
logger.debug(`[A11Y Scripts] Unknown script type: ${script.name}`);
82+
logToServer(`[A11Y Scripts] Unknown script type: ${script.name}`, { unknownScriptName: script.name });
5883
}
5984
});
6085

61-
logger.debug(`[A11Y Scripts] Parsed ${serverScripts.length} scripts from server`);
86+
logToServer(`[A11Y Scripts] Parsed ${serverScripts.length} scripts from server`, { scriptCount: serverScripts.length });
6287
}
6388

6489
// Parse commands to wrap from server response
@@ -71,13 +96,16 @@ class Scripts {
7196
// Extract scripts to run
7297
if (commandsToWrapData.scriptsToRun) {
7398
this.scriptsToRun = commandsToWrapData.scriptsToRun;
74-
logger.debug(`[A11Y Scripts] Scripts to run: ${this.scriptsToRun.join(', ')}`);
99+
logToServer(`[A11Y Scripts] Scripts to run: ${this.scriptsToRun.join(', ')}`, { scriptsToRun: this.scriptsToRun });
75100
}
76101

77102
if (this.commandsToWrap.length === 0) {
78-
logger.debug('[A11Y Scripts] Server sent EMPTY commands array - enabling build-end-only mode');
103+
logToServer('[A11Y Scripts] Server sent EMPTY commands array - enabling build-end-only mode', { commandCount: 0 });
79104
} else {
80-
logger.debug(`[A11Y Scripts] Server sent ${this.commandsToWrap.length} commands to wrap: ${this.commandsToWrap.map(cmd => cmd.name || cmd).join(', ')}`);
105+
logToServer(`[A11Y Scripts] Server sent ${this.commandsToWrap.length} commands to wrap: ${this.commandsToWrap.map(cmd => cmd.name || cmd).join(', ')}`, {
106+
commandCount: this.commandsToWrap.length,
107+
commands: this.commandsToWrap.map(cmd => cmd.name || cmd)
108+
});
81109
}
82110
}
83111

@@ -104,10 +132,10 @@ class Scripts {
104132
el.name && el.name.toLowerCase() === method.toLowerCase()
105133
) !== -1;
106134

107-
logger.debug(`[A11Y-Scripts] shouldWrapCommand(${method}) -> ${shouldWrap}`);
135+
logToServer(`[A11Y-Scripts] shouldWrapCommand(${method}) -> ${shouldWrap}`, { method, shouldWrap });
108136
return shouldWrap;
109137
} catch (error) {
110-
logger.debug(`[A11Y-Scripts] Exception in shouldWrapCommand: ${error.message}`);
138+
logToServer(`[A11Y-Scripts] Exception in shouldWrapCommand: ${error.message}`, { method, error: error.message });
111139
return false;
112140
}
113141
}
@@ -129,7 +157,7 @@ class Scripts {
129157
case 'savetestresults':
130158
return this.saveTestResults;
131159
default:
132-
logger.debug(`[A11Y-Scripts] Unknown script requested: ${scriptName}`);
160+
logToServer(`[A11Y-Scripts] Unknown script requested: ${scriptName}`, { scriptName });
133161
return null;
134162
}
135163
}
@@ -156,7 +184,7 @@ class Scripts {
156184
};
157185

158186
fs.writeFileSync(this.commandsPath, JSON.stringify(config, null, 2));
159-
logger.debug(`[A11Y-Scripts] Configuration saved to ${this.commandsPath}`);
187+
logToServer(`[A11Y-Scripts] Configuration saved to ${this.commandsPath}`, { configPath: this.commandsPath });
160188
} catch (error) {
161189
logger.error(`[A11Y-Scripts] Error saving configuration: ${error.message}`);
162190
}
@@ -180,10 +208,10 @@ class Scripts {
180208
this.commandsToWrap = config.commands || [];
181209
this.scriptsToRun = config.scriptsToRun || [];
182210

183-
logger.debug(`[A11Y-Scripts] Configuration loaded from ${this.commandsPath}`);
211+
logToServer(`[A11Y-Scripts] Configuration loaded from ${this.commandsPath}`, { configPath: this.commandsPath });
184212
}
185213
} catch (error) {
186-
logger.debug(`[A11Y-Scripts] Error loading configuration: ${error.message}`);
214+
logToServer(`[A11Y-Scripts] Error loading configuration: ${error.message}`, { error: error.message });
187215
}
188216
}
189217

@@ -201,7 +229,7 @@ class Scripts {
201229
try {
202230
if (fs.existsSync(this.commandsPath)) {
203231
fs.unlinkSync(this.commandsPath);
204-
logger.debug(`[A11Y-Scripts] Configuration file cleared`);
232+
logToServer(`[A11Y-Scripts] Configuration file cleared`, { configPath: this.commandsPath });
205233
}
206234
} catch (error) {
207235
logger.error(`[A11Y-Scripts] Error clearing configuration: ${error.message}`);

0 commit comments

Comments
 (0)