Skip to content

Commit 7b61f1e

Browse files
authored
Fixes for errors (#24)
1 parent 8bd426c commit 7b61f1e

File tree

5 files changed

+51
-42
lines changed

5 files changed

+51
-42
lines changed

nightwatch/globals.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ eventHelper.eventEmitter.on(EVENTS.LOG, (loggingData) => {
3434
const handleScreenshotUpload = async (data) => {
3535
try {
3636
const {args, uuid} = data;
37-
await testObservability.createScreenshotLogEvent(uuid, args.path, Date.now());
37+
await testObservability.createScreenshotLogEvent(uuid, args.path, Date.now());
3838
} catch (error) {
3939
CrashReporter.uploadCrashReport(error.message, error.stack);
4040
}
@@ -102,7 +102,7 @@ module.exports = {
102102
await worker.process.send({testCaseStartedId, uuid});
103103
}
104104
});
105-
});
105+
});
106106

107107
const testMetaData = {
108108
uuid: uniqueId,
@@ -301,7 +301,7 @@ module.exports = {
301301
const specs = process.env.BROWSERSTACK_RERUN_TESTS.split(',');
302302
await helper.handleNightwatchRerun(specs);
303303
}
304-
}
304+
}
305305
} catch (error) {
306306
Logger.error(`Could not configure or launch test observability - ${error}`);
307307
}
@@ -313,7 +313,9 @@ module.exports = {
313313
const [jwtToken, testRunId] = await accessibilityAutomation.createAccessibilityTestRun();
314314
process.env.BS_A11Y_JWT = jwtToken;
315315
process.env.BS_A11Y_TEST_RUN_ID = testRunId;
316-
accessibilityAutomation.setAccessibilityCapabilities(settings);
316+
if (helper.isAccessibilitySession()) {
317+
accessibilityAutomation.setAccessibilityCapabilities(settings);
318+
}
317319
}
318320
}
319321
} catch (error) {
@@ -346,7 +348,7 @@ module.exports = {
346348
} catch (error) {
347349
Logger.error(`Exception in stop accessibility test run: ${error}`);
348350
}
349-
351+
350352
}
351353
},
352354

@@ -355,7 +357,7 @@ module.exports = {
355357
browser.getAccessibilityResultsSummary = () => { return accessibilityAutomation.getAccessibilityResultsSummary() };
356358
// await accessibilityAutomation.beforeEachExecution(browser);
357359
},
358-
360+
359361
// This will be run after each test suite is finished
360362
async afterEach(settings) {
361363
// await accessibilityAutomation.afterEachExecution(browser);
@@ -396,14 +398,14 @@ const cucumberPatcher = () => {
396398
constructor(...args) {
397399
super(...args);
398400
}
399-
401+
400402
startWorker(...args) {
401403
const workerData = super.startWorker(...args);
402404
workerList = this.workers;
403-
405+
404406
return workerData;
405407
}
406-
408+
407409
parseWorkerMessage(...args) {
408410
if ([EVENTS.LOG, EVENTS.LOG_INIT].includes(args[1]?.eventType)) {return}
409411

src/accessibilityAutomation.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class AccessibilityAutomation {
3434
}
3535

3636
if (this._settings.accessibility || this._bstackOptions) {
37-
this._user = helper.getUserName(this._settings);
38-
this._key = helper.getAccessKey(this._settings);
37+
this._user = helper.getUserName(settings, this._settings);
38+
this._key = helper.getAccessKey(settings, this._settings);
3939
}
4040
}
4141

@@ -99,6 +99,7 @@ class AccessibilityAutomation {
9999

100100
return [responseData.accessibilityToken, responseData.id];
101101
} catch (error) {
102+
process.env.BROWSERSTACK_ACCESSIBILITY = 'false';
102103
if (error.response) {
103104
Logger.error(
104105
`Exception while creating test run for BrowserStack Accessibility Automation: ${
@@ -316,7 +317,7 @@ class AccessibilityAutomation {
316317
try {
317318
const capabilityConfig = driver.desiredCapabilities || {};
318319
const deviceName = driver.capabilities.deviceName || (capabilityConfig['bstack:options'] ? capabilityConfig['bstack:options'].deviceName : capabilityConfig.device) || '';
319-
320+
320321
if (deviceName !== '') {
321322
Logger.warn('Accessibility Automation will run only on Desktop browsers.');
322323

@@ -334,7 +335,7 @@ class AccessibilityAutomation {
334335

335336
return false;
336337
}
337-
338+
338339
const chromeOptions = capabilityConfig.chromeOptions || capabilityConfig['goog:chromeOptions'] || {};
339340
if (chromeOptions.args?.includes('--headless') || chromeOptions.args?.includes('headless')) {
340341
Logger.warn('Accessibility Automation will not run on legacy headless mode. Switch to new headless mode or avoid using headless mode.');

src/testObservability.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ class TestObservability {
2727
if (settings && settings.desiredCapabilities && settings.desiredCapabilities['bstack:options']) {
2828
this._bstackOptions = settings.desiredCapabilities['bstack:options'];
2929
}
30-
30+
3131
if (this._settings.test_observability || this._bstackOptions) {
3232
this._user = helper.getObservabilityUser(this._settings.test_observability, this._bstackOptions);
3333
this._key = helper.getObservabilityKey(this._settings.test_observability, this._bstackOptions);
34+
if (!this._user || !this._key) {
35+
Logger.error('Could not start Test Observability : Missing authentication token');
36+
process.env.BROWSERSTACK_TEST_OBSERVABILITY = 'false';
37+
38+
return;
39+
}
3440
CrashReporter.setCredentialsForCrashReportUpload(this._user, this._key);
3541
CrashReporter.setConfigDetails(settings);
3642
}
@@ -349,7 +355,7 @@ class TestObservability {
349355
const provider = helper.getCloudProvider(testFileReport.host);
350356
testData.integrations[provider] = helper.getIntegrationsObject(testFileReport.sessionCapabilities, testFileReport.sessionId);
351357
}
352-
358+
353359
if (eventType === 'TestRunStarted') {
354360
testData.type = 'test';
355361
testData.integrations = {};
@@ -440,7 +446,7 @@ class TestObservability {
440446
result = 'skipped';
441447

442448
return false;
443-
}
449+
}
444450

445451
return true;
446452
});

src/utils/helper.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ exports.isTestObservabilitySession = () => {
5252
};
5353

5454
exports.getObservabilityUser = (config, bstackOptions={}) => {
55-
return process.env.BROWSERSTACK_USERNAME || config.user || bstackOptions.userName;
55+
return process.env.BROWSERSTACK_USERNAME || config?.user || bstackOptions?.userName;
5656
};
5757

5858
exports.getObservabilityKey = (config, bstackOptions={}) => {
59-
return process.env.BROWSERSTACK_ACCESS_KEY || config.key || bstackOptions.accessKey;
59+
return process.env.BROWSERSTACK_ACCESS_KEY || config?.key || bstackOptions?.accessKey;
6060
};
6161

6262
exports.isAccessibilitySession = () => {
@@ -73,7 +73,7 @@ exports.getProjectName = (options, bstackOptions={}, fromProduct={}) => {
7373
}
7474

7575
return '';
76-
76+
7777
};
7878

7979
exports.getBuildName = (options, bstackOptions={}, fromProduct={}) => {
@@ -218,7 +218,7 @@ exports.getCiInfo = () => {
218218
build_number: env.APPVEYOR_BUILD_NUMBER
219219
};
220220
}
221-
// Azure CI
221+
// Azure CI
222222
if (env.AZURE_HTTP_USER_AGENT && env.TF_BUILD) {
223223
return {
224224
name: 'Azure CI',
@@ -477,7 +477,7 @@ exports.uploadEventData = async (eventData) => {
477477
if (process.env.BS_TESTOPS_JWT && process.env.BS_TESTOPS_JWT !== 'null') {
478478
requestQueueHandler.pending_test_uploads += 1;
479479
}
480-
480+
481481
if (process.env.BS_TESTOPS_BUILD_COMPLETED === 'true') {
482482
if (process.env.BS_TESTOPS_JWT === 'null') {
483483
Logger.info(`EXCEPTION IN ${log_tag} REQUEST TO TEST OBSERVABILITY : missing authentication token`);
@@ -487,10 +487,10 @@ exports.uploadEventData = async (eventData) => {
487487
status: 'error',
488488
message: 'Token/buildID is undefined, build creation might have failed'
489489
};
490-
}
491-
let data = eventData;
490+
}
491+
let data = eventData;
492492
let event_api_url = 'api/v1/event';
493-
493+
494494
requestQueueHandler.start();
495495
const {
496496
shouldProceed,
@@ -511,7 +511,7 @@ exports.uploadEventData = async (eventData) => {
511511
'X-BSTACK-TESTOPS': 'true'
512512
}
513513
};
514-
514+
515515
try {
516516
const response = await makeRequest('POST', event_api_url, data, config);
517517
if (response.data.error) {
@@ -540,8 +540,12 @@ exports.uploadEventData = async (eventData) => {
540540
}
541541
};
542542

543-
exports.getAccessKey = (settings) => {
544-
let accessKey = null;
543+
exports.getAccessKey = (settings, nwConfig) => {
544+
let accessKey = process.env.BROWSERSTACK_ACCESS_KEY || nwConfig?.accessKey;
545+
if (!this.isUndefined(accessKey)) {
546+
return accessKey;
547+
}
548+
545549
if (this.isObject(settings.desiredCapabilities)) {
546550
if (settings.desiredCapabilities['browserstack.key']) {
547551
accessKey = settings.desiredCapabilities['browserstack.key'];
@@ -550,15 +554,15 @@ exports.getAccessKey = (settings) => {
550554
}
551555
}
552556

553-
if (this.isUndefined(accessKey)) {
554-
accessKey = process.env.BROWSERSTACK_ACCESS_KEY;
555-
}
556-
557557
return accessKey;
558558
};
559559

560-
exports.getUserName = (settings) => {
561-
let userName = null;
560+
exports.getUserName = (settings, nwConfig) => {
561+
let userName = process.env.BROWSERSTACK_USERNAME || nwConfig?.userName;
562+
if (!this.isUndefined(userName)) {
563+
return userName;
564+
}
565+
562566
if (this.isObject(settings.desiredCapabilities)) {
563567
if (settings.desiredCapabilities['browserstack.user']) {
564568
userName = settings.desiredCapabilities['browserstack.user'];
@@ -567,10 +571,6 @@ exports.getUserName = (settings) => {
567571
}
568572
}
569573

570-
if (this.isUndefined(userName)) {
571-
userName = process.env.BROWSERSTACK_USERNAME;
572-
}
573-
574574
return userName;
575575
};
576576

@@ -624,7 +624,7 @@ const sleep = (ms = 100) => new Promise((resolve) => setTimeout(resolve, ms));
624624

625625
exports.uploadPending = async (
626626
waitTimeout = DEFAULT_WAIT_TIMEOUT_FOR_PENDING_UPLOADS,
627-
waitInterval = DEFAULT_WAIT_INTERVAL_FOR_PENDING_UPLOADS
627+
waitInterval = DEFAULT_WAIT_INTERVAL_FOR_PENDING_UPLOADS
628628
) => {
629629
if (requestQueueHandler.pending_test_uploads <= 0 || waitTimeout <= 0) {
630630
return;

src/utils/logPatcher.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ class LogPatcher extends Transport {
2929
});
3030
testLogs = [];
3131
};
32-
32+
3333
logToTestOps = (level = LOG_LEVELS.INFO, message = ['']) => {
3434
try {
3535
let eventType = EVENTS.LOG;
36-
if (!message[0].match(PID_MAPPING_REGEX)) {
37-
consoleHolder[level.toLowerCase()](...message);
38-
} else {
36+
if (typeof message[0] === 'string' && message[0].match(PID_MAPPING_REGEX)) {
3937
eventType = EVENTS.LOG_INIT;
38+
} else {
39+
consoleHolder[level.toLowerCase()](...message);
4040
}
4141
const pid = process.pid;
4242
const loggingData = {

0 commit comments

Comments
 (0)