chore(testing): updated jasmine.Reporter

See #670
This commit is contained in:
Jay Traband 2016-01-04 15:01:08 -08:00 committed by Ward Bell
parent 9895595869
commit 3460f35a9f
1 changed files with 73 additions and 54 deletions

View File

@ -7,6 +7,7 @@
var fs = require('fs'); var fs = require('fs');
var path = require('canonical-path'); var path = require('canonical-path');
var _ = require('lodash');
exports.config = { exports.config = {
@ -33,14 +34,14 @@ exports.config = {
// resultJsonOutputFile: "foo.json", // resultJsonOutputFile: "foo.json",
onPrepare: function() { onPrepare: function() {
// SpecReporter //// SpecReporter
//var SpecReporter = require('jasmine-spec-reporter'); //var SpecReporter = require('jasmine-spec-reporter');
//// add jasmine spec reporter
//jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'none'})); //jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'none'}));
// jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'all'})); //// jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'all'}));
// debugging // debugging
console.log('browser.params:' + JSON.stringify(browser.params)); // console.log('browser.params:' + JSON.stringify(browser.params));
var appDir = browser.params.appDir; var appDir = browser.params.appDir;
if (appDir) { if (appDir) {
if (appDir.match('/ts') != null) { if (appDir.match('/ts') != null) {
@ -73,7 +74,7 @@ function describeIf(cond, name, func) {
if (cond) { if (cond) {
describe(name, func); describe(name, func);
} else { } else {
xdescribe('*** Skipped *** - ' + name, func); xdescribe(name, func);
} }
} }
@ -81,7 +82,7 @@ function itIf(cond, name, func) {
if (cond) { if (cond) {
it(name, func); it(name, func);
} else { } else {
xit('*** Skipped *** - ' + name, func); xit(name, func);
} }
} }
@ -97,75 +98,93 @@ function sendKeys(element, str) {
function Reporter(options) { function Reporter(options) {
this.defaultOutputFile = path.resolve(process.cwd(), "../../", 'protractor-results.txt'); var _defaultOutputFile = path.resolve(process.cwd(), "../../", 'protractor-results.txt');
var _output = []; options.outputFile = options.outputFile || _defaultOutputFile;
var _logDepth = 0;
var __pad = ' '; var _root = { appDir: options.appDir, suites: [] };
log('AppDir: ' + options.appDir, +1);
var _currentSuite; var _currentSuite;
options.outputFile = options.outputFile || this.defaultOutputFile;
log('AppDir: ' + options.appDir);
this.suiteStarted = function(suite) { this.suiteStarted = function(suite) {
_logDepth++; _currentSuite = { description: suite.description, status: null, specs: [] };
log('Suite: ' + suite.description); _root.suites.push(_currentSuite);
// debugging info log('Suite: ' + suite.description, +1);
// log('Suite stringify:' + JSON.stringify(suite));
// log('argv stringify:' + JSON.stringify(process.argv));
_currentSuite = suite;
_currentSuite.ok = true;
}; };
this.suiteDone = function(suite) { this.suiteDone = function(suite) {
var status = _currentSuite.ok ? 'Suite passed: ' : 'Suite failed: '; var statuses = _currentSuite.specs.map(function(spec) {
log(status + suite.description); return spec.status;
_logDepth--; });
statuses = _.uniq(statuses);
var status = statuses.indexOf('failed') >= 0 ? 'failed' : statuses.join(', ');
_currentSuite.status = status;
log('Suite ' + _currentSuite.status + ': ' + suite.description, -1);
}; };
this.specStarted = function(spec) { this.specStarted = function(spec) {
_logDepth++;
}; };
this.specDone = function(spec) { this.specDone = function(spec) {
log(spec.status + ": " + spec.description); var currentSpec = {
logFailedSpec(spec); description: spec.description,
_logDepth--; status: spec.status
};
if (spec.failedExpectations.length > 0) {
currentSpec.failedExpectations = spec.failedExpectations;
}
_currentSuite.specs.push(currentSpec);
log(spec.status + ' - ' + spec.description);
}; };
this.jasmineDone = function() { this.jasmineDone = function() {
outputFile = options.outputFile; outputFile = options.outputFile;
// console.log('Completed - appending output to: ' + outputFile); //// Alternate approach - just stringify the _root - not as pretty
fs.appendFileSync(outputFile, _output.join('\n')+ '\n\n'); //// but might be more useful for automation.
// var output = JSON.stringify(_root, null, 2);
var output = formatOutput(_root);
fs.appendFileSync(outputFile, output);
}; };
function logFailedSpec(spec) { // for output file output
if (spec.failedExpectations.length == 0) return; function formatOutput(output) {
_logDepth++; var indent = ' ';
spec.failedExpectations.forEach(function(exp) { var pad = ' ';
log('detail: ' + exp.message); var results = [];
_currentSuite.ok = false; results.push('AppDir:' + output.appDir);
output.suites.forEach(function(suite) {
results.push(pad + 'Suite: ' + suite.description + ' -- ' + suite.status);
pad+=indent;
suite.specs.forEach(function(spec) {
results.push(pad + spec.status + ' - ' + spec.description);
if (spec.failedExpectations) {
pad+=indent;
spec.failedExpectations.forEach(function (fe) {
results.push(pad + 'message: ' + fe.message);
}); });
_logDepth--; pad=pad.substr(2);
}
});
pad = pad.substr(2);
results.push('');
});
results.push('');
return results.join('\n');
} }
function log(msg) { // for console output
msg = lpad(msg, 3 * _logDepth); var _pad;
console.log(msg); function log(str, indent) {
_output.push(msg); _pad = _pad || '';
if (indent == -1) {
_pad = _pad.substr(2);
}
console.log(_pad + str);
if (indent == 1) {
_pad = _pad + ' ';
}
} }
function lpad(value, length) { }
return __pad.substr(0,length) + value;
}
};
// To be copied into e2e-tests experiencing sendKeys bug.
//// Hack - because of bug with send keys
//function sendKeys(element, str) {
// return str.split('').reduce(function (promise, char) {
// return promise.then(function () {
// return element.sendKeys(char);
// });
// }, element.getAttribute('value'));
// // better to create a resolved promise here but ... don't know how with protractor;
//}