diff --git a/bin/commands/runs.js b/bin/commands/runs.js index 0a439ff6..dd04775f 100644 --- a/bin/commands/runs.js +++ b/bin/commands/runs.js @@ -192,6 +192,10 @@ module.exports = function run(args, rawArgs) { logger.debug("Completed setting the configs"); if(!isBrowserstackInfra) { + if(process.env.BS_TESTOPS_BUILD_COMPLETED) { + setEventListeners(bsConfig); + } + return runCypressTestsLocally(bsConfig, args, rawArgs); } @@ -203,7 +207,7 @@ module.exports = function run(args, rawArgs) { setAccessibilityEventListeners(bsConfig); } if(process.env.BS_TESTOPS_BUILD_COMPLETED) { - // setEventListeners(bsConfig); + setEventListeners(bsConfig); } markBlockEnd('validateConfig'); logger.debug("Completed configs validation"); diff --git a/bin/testObservability/cypress/index.js b/bin/testObservability/cypress/index.js index 78482442..168ec306 100644 --- a/bin/testObservability/cypress/index.js +++ b/bin/testObservability/cypress/index.js @@ -1,21 +1,31 @@ /* Event listeners + custom commands for Cypress */ /* Used to detect Gherkin steps */ + +const browserStackLog = (message) => { + if (!Cypress.env('BROWSERSTACK_LOGS')) return; + cy.task('browserstack_log', message); +} + +const shouldSkipCommand = (command) => { + return command.attributes.name == 'log' || (command.attributes.name == 'task' && (['test_observability_platform_details', 'test_observability_step', 'test_observability_command', 'browserstack_log'].some(event => command.attributes.args.includes(event)))); +} + Cypress.on('log:added', (log) => { - return () => { - return cy.now('task', 'test_observability_step', { - log - }, {log: false}) - } - }); - + return () => { + return cy.task('test_observability_step', { + log + }, { log: false }) + } +}); + Cypress.on('command:start', (command) => { - if(!command || !command.attributes) return; - if(command.attributes.name == 'log' || (command.attributes.name == 'task' && (command.attributes.args.includes('test_observability_command') || command.attributes.args.includes('test_observability_log')))) { + if (!command || !command.attributes) return; + if (shouldSkipCommand(command)) { return; } /* Send command details */ - cy.now('task', 'test_observability_command', { + cy.task('test_observability_command', { type: 'COMMAND_START', command: { attributes: { @@ -25,27 +35,23 @@ Cypress.on('command:start', (command) => { }, state: 'pending' } - }, {log: false}).then((res) => { - }).catch((err) => { - }); + }, { log: false }); /* Send platform details */ - cy.now('task', 'test_observability_platform_details', { - testTitle: Cypress.currentTest.title, + cy.task('test_observability_platform_details', { + testTitle: Cypress.currentRunnable?.title || '', browser: Cypress.browser, platform: Cypress.platform, cypressVersion: Cypress.version - }, {log: false}).then((res) => { - }).catch((err) => { - }); + }, { log: false }); }); Cypress.on('command:retry', (command) => { - if(!command || !command.attributes) return; - if(command.attributes.name == 'log' || (command.attributes.name == 'task' && (command.attributes.args.includes('test_observability_command') || command.attributes.args.includes('test_observability_log')))) { + if (!command || !command.attributes) return; + if (shouldSkipCommand(command)) { return; } - cy.now('task', 'test_observability_command', { + cy.task('test_observability_command', { type: 'COMMAND_RETRY', command: { _log: command._log, @@ -54,17 +60,15 @@ Cypress.on('command:retry', (command) => { isDefaultAssertionErr: command && command.error ? command.error.isDefaultAssertionErr : null } } - }, {log: false}).then((res) => { - }).catch((err) => { - }); + }, { log: false }); }); Cypress.on('command:end', (command) => { - if(!command || !command.attributes) return; - if(command.attributes.name == 'log' || (command.attributes.name == 'task' && (command.attributes.args.includes('test_observability_command') || command.attributes.args.includes('test_observability_log')))) { + if (!command || !command.attributes) return; + if (shouldSkipCommand(command)) { return; } - cy.now('task', 'test_observability_command', { + cy.task('test_observability_command', { 'type': 'COMMAND_END', 'command': { 'attributes': { @@ -74,13 +78,11 @@ Cypress.on('command:end', (command) => { }, 'state': command.state } - }, {log: false}).then((res) => { - }).catch((err) => { - }); + }, { log: false }); }); Cypress.Commands.overwrite('log', (originalFn, ...args) => { - if(args.includes('test_observability_log') || args.includes('test_observability_command')) return; + if (args.includes('test_observability_log') || args.includes('test_observability_command')) return; const message = args.reduce((result, logItem) => { if (typeof logItem === 'object') { return [result, JSON.stringify(logItem)].join(' '); @@ -88,71 +90,57 @@ Cypress.Commands.overwrite('log', (originalFn, ...args) => { return [result, logItem ? logItem.toString() : ''].join(' '); }, ''); - cy.now('task', 'test_observability_log', { + cy.task('test_observability_log', { 'level': 'info', message, - }, {log: false}).then((res) => { - }).catch((err) => { - }); + }, { log: false }); originalFn(...args); }); Cypress.Commands.add('trace', (message, file) => { - cy.now('task', 'test_observability_log', { + cy.task('test_observability_log', { level: 'trace', message, file, - }).then((res) => { - }).catch((err) => { }); }); Cypress.Commands.add('logDebug', (message, file) => { - cy.now('task', 'test_observability_log', { + cy.task('test_observability_log', { level: 'debug', message, file, - }).then((res) => { - }).catch((err) => { }); }); Cypress.Commands.add('info', (message, file) => { - cy.now('task', 'test_observability_log', { + cy.task('test_observability_log', { level: 'info', message, file, - }).then((res) => { - }).catch((err) => { }); }); Cypress.Commands.add('warn', (message, file) => { - cy.now('task', 'test_observability_log', { + cy.task('test_observability_log', { level: 'warn', message, file, - }).then((res) => { - }).catch((err) => { }); }); Cypress.Commands.add('error', (message, file) => { - cy.now('task', 'test_observability_log', { + cy.task('test_observability_log', { level: 'error', message, file, - }).then((res) => { - }).catch((err) => { }); }); Cypress.Commands.add('fatal', (message, file) => { - cy.now('task', 'test_observability_log', { + cy.task('test_observability_log', { level: 'fatal', message, file, - }).then((res) => { - }).catch((err) => { }); }); diff --git a/bin/testObservability/helper/cleanupQueueSync.js b/bin/testObservability/helper/cleanupQueueSync.js index aa2603f4..3cf76c10 100644 --- a/bin/testObservability/helper/cleanupQueueSync.js +++ b/bin/testObservability/helper/cleanupQueueSync.js @@ -2,10 +2,9 @@ * Sending all the remaining queues for synchronous manner */ -const RequestQueueHandler = require('./requestQueueHandler'); +const requestHandler = require('./requestQueueHandler'); const shutdown = async () => { - const requestHandler = new RequestQueueHandler(); requestHandler.queue = require(process.argv[2].trim()); await requestHandler.shutdown(); } diff --git a/bin/testObservability/helper/helper.js b/bin/testObservability/helper/helper.js index 467f090c..2898e6a6 100644 --- a/bin/testObservability/helper/helper.js +++ b/bin/testObservability/helper/helper.js @@ -117,6 +117,7 @@ exports.printBuildLink = async (shouldStopSession, exitCode = null) => { } const nodeRequest = (type, url, data, config) => { + const requestQueueHandler = require('./requestQueueHandler'); return new Promise(async (resolve, reject) => { const options = { ...config, @@ -140,8 +141,8 @@ const nodeRequest = (type, url, data, config) => { options.proxy = false options.httpsAgent = new HttpsProxyAgent(process.env.HTTPS_PROXY); } - - if(url === exports.requestQueueHandler.screenshotEventUrl) { + + if(url === requestQueueHandler.screenshotEventUrl) { options.agent = httpsScreenshotsKeepAliveAgent; } axios(options) @@ -521,10 +522,9 @@ exports.batchAndPostEvents = async (eventUrl, kind, data) => { } } -const RequestQueueHandler = require('./requestQueueHandler'); -exports.requestQueueHandler = new RequestQueueHandler(); - exports.uploadEventData = async (eventData, run=0) => { + + const requestQueueHandler = require('./requestQueueHandler'); exports.debugOnConsole(`[uploadEventData] ${eventData.event_type}`); const log_tag = { ['TestRunStarted']: 'Test_Start_Upload', @@ -550,8 +550,8 @@ exports.uploadEventData = async (eventData, run=0) => { } else { let data = eventData, event_api_url = 'api/v1/event'; - exports.requestQueueHandler.start(); - const { shouldProceed, proceedWithData, proceedWithUrl } = exports.requestQueueHandler.add(eventData); + requestQueueHandler.start(); + const { shouldProceed, proceedWithData, proceedWithUrl } = requestQueueHandler.add(eventData); exports.debugOnConsole(`[Request Queue] ${eventData.event_type} with uuid ${eventData.test_run ? eventData.test_run.uuid : (eventData.hook_run ? eventData.hook_run.uuid : null)} is added`) if(!shouldProceed) { return; @@ -576,7 +576,7 @@ exports.uploadEventData = async (eventData, run=0) => { if(response.data.error) { throw({message: response.data.error}); } else { - exports.debug(`${event_api_url !== exports.requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'}[${run}] event successfull!`) + exports.debug(`${event_api_url !== requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'}[${run}] event successfull!`) exports.pending_test_uploads.count = Math.max(0,exports.pending_test_uploads.count - (event_api_url === 'api/v1/event' ? 1 : data.length)); return { status: 'success', @@ -586,9 +586,9 @@ exports.uploadEventData = async (eventData, run=0) => { } catch(error) { exports.debugOnConsole(`[Request Error] Error in sending request ${util.format(error)}`); if (error.response) { - exports.debug(`EXCEPTION IN ${event_api_url !== exports.requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'} REQUEST TO TEST OBSERVABILITY : ${error.response.status} ${error.response.statusText} ${JSON.stringify(error.response.data)}`, true, error); + exports.debug(`EXCEPTION IN ${event_api_url !== requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'} REQUEST TO TEST OBSERVABILITY : ${error.response.status} ${error.response.statusText} ${JSON.stringify(error.response.data)}`, true, error); } else { - exports.debug(`EXCEPTION IN ${event_api_url !== exports.requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'} REQUEST TO TEST OBSERVABILITY : ${error.message || error}`, true, error); + exports.debug(`EXCEPTION IN ${event_api_url !== requestQueueHandler.eventUrl ? log_tag : 'Batch-Queue'} REQUEST TO TEST OBSERVABILITY : ${error.message || error}`, true, error); } exports.pending_test_uploads.count = Math.max(0,exports.pending_test_uploads.count - (event_api_url === 'api/v1/event' ? 1 : data.length)); return { diff --git a/bin/testObservability/helper/requestQueueHandler.js b/bin/testObservability/helper/requestQueueHandler.js index 424e1a20..245a4bd8 100644 --- a/bin/testObservability/helper/requestQueueHandler.js +++ b/bin/testObservability/helper/requestQueueHandler.js @@ -98,4 +98,4 @@ class RequestQueueHandler { } } -module.exports = RequestQueueHandler; +module.exports = new RequestQueueHandler(); diff --git a/bin/testObservability/plugin/ipcServer.js b/bin/testObservability/plugin/ipcServer.js index 62e84394..9ac1b398 100644 --- a/bin/testObservability/plugin/ipcServer.js +++ b/bin/testObservability/plugin/ipcServer.js @@ -1,6 +1,6 @@ const ipc = require('node-ipc'); const { consoleHolder } = require('../helper/constants'); -const { requestQueueHandler } = require('../helper/helper'); +const requestQueueHandler = require('../helper/requestQueueHandler'); exports.startIPCServer = (subscribeServerEvents, unsubscribeServerEvents) => { if (ipc.server) { diff --git a/bin/testObservability/reporter/index.js b/bin/testObservability/reporter/index.js index 396ad0e3..949d3046 100644 --- a/bin/testObservability/reporter/index.js +++ b/bin/testObservability/reporter/index.js @@ -52,7 +52,6 @@ const { mapTestHooks, debug, isBrowserstackInfra, - requestQueueHandler, getHookSkippedTests, getOSDetailsFromSystem, findGitConfig,