0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-13 22:41:32 -05:00

Improving Casper test error reporting

This commit is contained in:
Hannah Wolfe 2014-06-21 14:56:30 +01:00
parent d159a5cced
commit af31409cb0

View file

@ -143,6 +143,11 @@ casper.fillAndSave = function (selector, data) {
casper.thenClick(selector + ' .button-save'); casper.thenClick(selector + ' .button-save');
}; };
// ## Debugging
var jsErrors = [],
pageErrors = [],
resourceErrors = [];
// ## Echo Concise // ## Echo Concise
// Does casper.echo but checks for the presence of the --concise flag // Does casper.echo but checks for the presence of the --concise flag
casper.echoConcise = function (message, style) { casper.echoConcise = function (message, style) {
@ -151,18 +156,43 @@ casper.echoConcise = function (message, style) {
} }
}; };
// ## Debugging // pass through all console.logs
// output all errors to the console
casper.on('remote.message', function (msg) { casper.on('remote.message', function (msg) {
casper.echoConcise('GOT CONSOLE LOG: ' + msg); casper.echoConcise('CONSOLE LOG: ' + msg, 'INFO');
}); });
casper.on('error', function (msg) { // output any errors
casper.echoConcise('GOT ERROR, ' + msg); casper.on('error', function (msg, trace) {
casper.echoConcise('ERROR, ' + msg, 'ERROR');
if (trace) {
casper.echoConcise('file: ' + trace[0].file, 'WARNING');
casper.echoConcise('line: ' + trace[0].line, 'WARNING');
casper.echoConcise('function: ' + trace[0]['function'], 'WARNING');
}
jsErrors.push(msg);
}); });
casper.on('page.error', function (msg) { // output any page errors
casper.echoConcise('GOT PAGE ERROR: ' + msg, 'ERROR'); casper.on('page.error', function (msg, trace) {
casper.echoConcise('PAGE ERROR: ' + msg, 'ERROR');
if (trace) {
casper.echoConcise('file: ' + trace[0].file, 'WARNING');
casper.echoConcise('line: ' + trace[0].line, 'WARNING');
casper.echoConcise('function: ' + trace[0]['function'], 'WARNING');
}
pageErrors.push(msg);
});
casper.on('resource.received', function(resource) {
var status = resource.status;
if(status >= 400) {
casper.echoConcise('RESOURCE ERROR: ' + resource.url + ' failed to load (' + status + ')', 'ERROR');
resourceErrors.push({
url: resource.url,
status: resource.status
});
}
}); });
casper.captureScreenshot = function (filename, debugOnly) { casper.captureScreenshot = function (filename, debugOnly) {
@ -176,10 +206,8 @@ casper.captureScreenshot = function (filename, debugOnly) {
} }
}; };
// on failure, grab a screenshot
casper.test.on('fail', function captureFailure() {
// on failure, grab a screenshot
casper.test.on('fail', function captureFailure(test) {
casper.captureScreenshot(casper.test.filename || 'casper_test_fail.png', false); casper.captureScreenshot(casper.test.filename || 'casper_test_fail.png', false);
casper.then(function () { casper.then(function () {
console.log(casper.getHTML()); console.log(casper.getHTML());
@ -187,6 +215,26 @@ casper.test.on('fail', function captureFailure(test) {
}); });
}); });
// on exit, output any errors
casper.test.on('exit', function() {
if (jsErrors.length > 0) {
casper.echo(jsErrors.length + ' Javascript errors found', 'WARNING');
} else {
casper.echo(jsErrors.length + ' Javascript errors found', 'INFO');
}
if (pageErrors.length > 0) {
casper.echo(pageErrors.length + ' Page errors found', 'WARNING');
} else {
casper.echo(pageErrors.length + ' Page errors found', 'INFO');
}
if (resourceErrors.length > 0) {
casper.echo(resourceErrors.length + ' Resource errors found', 'WARNING');
} else {
casper.echo(resourceErrors.length + ' Resource errors found', 'INFO');
}
});
var CasperTest = (function () { var CasperTest = (function () {
var _beforeDoneHandler, var _beforeDoneHandler,