Skip to content

fix: cy.now with cy.task #984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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");
Expand Down
94 changes: 41 additions & 53 deletions bin/testObservability/cypress/index.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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,
Expand All @@ -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': {
Expand All @@ -74,85 +78,69 @@ 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(' ');
}

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) => {
});
});
3 changes: 1 addition & 2 deletions bin/testObservability/helper/cleanupQueueSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
20 changes: 10 additions & 10 deletions bin/testObservability/helper/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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',
Expand All @@ -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;
Expand All @@ -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',
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion bin/testObservability/helper/requestQueueHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ class RequestQueueHandler {
}
}

module.exports = RequestQueueHandler;
module.exports = new RequestQueueHandler();
2 changes: 1 addition & 1 deletion bin/testObservability/plugin/ipcServer.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
1 change: 0 additions & 1 deletion bin/testObservability/reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const {
mapTestHooks,
debug,
isBrowserstackInfra,
requestQueueHandler,
getHookSkippedTests,
getOSDetailsFromSystem,
findGitConfig,
Expand Down