Skip to content

Instantly share code, notes, and snippets.

@dcypherthis
Last active September 15, 2017 15:50
Show Gist options
  • Save dcypherthis/5afe5e18cb15e451b4be54561bb9dc6e to your computer and use it in GitHub Desktop.
Save dcypherthis/5afe5e18cb15e451b4be54561bb9dc6e to your computer and use it in GitHub Desktop.
severe browser console logs
// Declare in the top of your config before anty params are set.
const errorLogs = [];
let feature, scenario, step, status, timestamp, allTags, caseId;
// add these hooks to the body of your config
afterStep: function(stepResult) { //eslint-disable-line object-shorthand
/**
* Assign Step Status of Passed/Failed/Skipped to a variable.
*/
status = stepResult.status;
/**
* If the step passes or is skipped, skip browser console logging.
*/
if (status !== `failed`) {
return;
}
/**
* Assign current step data to variables for error logging purposes
*/
feature = stepResult.step.scenario.feature.name;
scenario = stepResult.step.scenario.name;
step = stepResult.step.name;
/**
* gets the current browser logs from selenium and assigns them to a variable
*/
const logs = browser.log('browser');
/**
* Filters out everything except error messages and adds step/scenario/feature info to the errorLogs array
*/
logs.value.forEach(log => {
/**
* only print error messages
*/
if (!log || typeof log.level !== 'string' || log.level.toLowerCase() !== 'severe') {
return;
}
/**
* Assign time error was incurred to a variable
*/
timestamp = log.timestamp.toString();
/**
* Add The test path and browser console message to the errorLogs array
*/
errorLogs.push({
name: `${status}: ${feature} ${scenario} ${step}`,
message: log.message,
});
});
},
after: function (result, capabilities, spec) { //eslint-disable-line object-shorthand
/* eslint-disable no-console */
/**
* Skip if there are no severe browser errors to report
*/
if (status === 'passed') {
return;
} else if ((status === 'failed' || status === 'skipped') && errorLogs.length === 0) {
console.error(`\n<==== NO SEVERE BROWSER ERRORS DETECTED ====>`);
return;
}
console.error('\n====> SEVERE BROWSER LOGS OF FAILING TESTS <====');
console.error(`Case ID: ${caseId}`);
console.error(`Feature: ${feature}`);
console.error(`Scenario: ${scenario}`);
console.error(`Step: ${step}`);
console.error(`TimeStamp: ${timestamp}`);
console.error(`Capabilities: ${JSON.stringify(capabilities)}`);
errorLogs.forEach((log, i) => {
if (i === 0 || errorLogs[i - 1].name !== log.name) {
console.error(`\n> ${log.name}`);
}
console.error(`BROWSER CONSOLE LOG: ${log.message}`);
});
console.error('====> END OF BROWSER LOGS <====');
/* eslint-enable no-console */
},
/* eslint-disable no-shadow */
afterFeature: function (feature) { //eslint-disable-line object-shorthand
console.log(feature.tags[0].name);
allTags = feature.tags;
caseId = allTags[0].name;
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment