Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
fix(util): properly handle exceptions from onPrepare and onExit
Browse files Browse the repository at this point in the history
  • Loading branch information
hankduan committed Nov 26, 2014
1 parent 2572feb commit 289dbb9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 65 deletions.
4 changes: 2 additions & 2 deletions lib/configParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var path = require('path'),
glob = require('glob'),
util = require('util'),
_ = require('lodash'),
protractor = require('./protractor.js');
helper = require('./util');

// Coffee is required here to enable config files written in coffee-script.
try {
Expand Down Expand Up @@ -32,7 +32,7 @@ var ConfigParser = function() {
isVerbose: false,
showColors: true,
includeStackTrace: true,
stackFilter: protractor.filterStackTrace,
stackFilter: helper.filterStackTrace,
defaultTimeoutInterval: (30 * 1000)
},
seleniumArgs: [],
Expand Down
57 changes: 6 additions & 51 deletions lib/protractor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var url = require('url');
var util = require('util');
var webdriver = require('selenium-webdriver');
var helper = require('./util');

var clientSideScripts = require('./clientsidescripts.js');
var ProtractorBy = require('./locators.js').ProtractorBy;
Expand All @@ -14,17 +15,6 @@ var WEB_ELEMENT_FUNCTIONS = [
'getSize', 'getLocation', 'isEnabled', 'isSelected', 'submit', 'clear',
'isDisplayed', 'getOuterHtml', 'getInnerHtml', 'getId'];

var STACK_SUBSTRINGS_TO_FILTER = [
'node_modules/minijasminenode/lib/',
'node_modules/selenium-webdriver',
'at Module.',
'at Object.Module.',
'at Function.Module',
'(timers.js:',
'jasminewd/index.js',
'protractor/lib/'
];

var DEFAULT_RESET_URL = 'data:text/html,<html></html>';
var DEFAULT_GET_PAGE_TIMEOUT = 10000;

Expand Down Expand Up @@ -1478,21 +1468,11 @@ Protractor.prototype.pause = function(opt_debugPort) {

var getDescriptions = function(frameOrTask, descriptions) {
if (frameOrTask.getDescription) {
var getRelevantStack = function(stack) {
return stack.filter(function(line) {
var include = true;
for (var i = 0; i < STACK_SUBSTRINGS_TO_FILTER.length; ++i) {
if (line.toString().indexOf(STACK_SUBSTRINGS_TO_FILTER[i]) !==
-1) {
include = false;
}
}
return include;
});
};
var stacktrace = frameOrTask.snapshot_.getStacktrace();
stacktrace = stacktrace ? stacktrace.join('\n').trim() : '';
descriptions.push({
description: frameOrTask.getDescription(),
stack: getRelevantStack(frameOrTask.snapshot_.getStacktrace())
stack: helper.filterStackTrace(stacktrace)
});
} else {
for (var i = 0; i < frameOrTask.children_.length; ++i) {
Expand All @@ -1510,8 +1490,8 @@ Protractor.prototype.pause = function(opt_debugPort) {
var asString = '-- WebDriver control flow schedule \n';
for (var i = 0; i < descriptions.length; ++i) {
asString += ' |- ' + descriptions[i].description;
if (descriptions[i].stack.length) {
asString += '\n |---' + descriptions[i].stack.join('\n |---');
if (descriptions[i].stack) {
asString += '\n |---' + descriptions[i].stack.replace(/\n/g, '\n |---');
}
if (i != (descriptions.length - 1)) {
asString += '\n';
Expand Down Expand Up @@ -1571,28 +1551,3 @@ exports.setInstance = function(ptor) {
exports.getInstance = function() {
return instance;
};

/**
* Utility function that filters a stack trace to be more readable. It removes
* Jasmine test frames and webdriver promise resolution.
* @param {string} text Original stack trace.
* @return {string}
*/
exports.filterStackTrace = function(text) {
if (!text) {
return text;
}
var lines = [];
text.split(/\n/).forEach(function(line) {
var include = true;
for (var i = 0; i < STACK_SUBSTRINGS_TO_FILTER.length; ++i) {
if (line.indexOf(STACK_SUBSTRINGS_TO_FILTER[i]) !== -1) {
include = false;
}
}
if (include) {
lines.push(line);
}
});
return lines.join('\n');
};
65 changes: 53 additions & 12 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
var q = require('q'),
path = require('path');
path = require('path'),
webdriver = require('selenium-webdriver');

var STACK_SUBSTRINGS_TO_FILTER = [
'node_modules/minijasminenode/lib/',
'node_modules/selenium-webdriver',
'at Module.',
'at Object.Module.',
'at Function.Module',
'(timers.js:',
'jasminewd/index.js',
'protractor/lib/'
];


/**
* Utility function that filters a stack trace to be more readable. It removes
* Jasmine test frames and webdriver promise resolution.
* @param {string} text Original stack trace.
* @return {string}
*/
exports.filterStackTrace = function(text) {
if (!text) {
return text;
}
var lines = text.split(/\n/).filter(function(line) {
for (var i = 0; i < STACK_SUBSTRINGS_TO_FILTER.length; ++i) {
if (line.indexOf(STACK_SUBSTRINGS_TO_FILTER[i]) !== -1) {
return false;
}
}
return true;
});
return lines.join('\n');
};

/**
* Internal helper for abstraction of polymorphic filenameOrFn properties.
Expand All @@ -8,20 +42,27 @@ var q = require('q'),
* @return {q.Promise} A promise that will resolve when filenameOrFn completes.
*/
exports.runFilenameOrFn_ = function(configDir, filenameOrFn, args) {
var returned = null;
if (filenameOrFn) {
if (typeof filenameOrFn === 'function') {
returned = filenameOrFn.apply(null, args);
} else if (typeof filenameOrFn === 'string') {
return q.promise(function(resolve) {
if (filenameOrFn &&
!(typeof filenameOrFn === 'string' || typeof filenameOrFn === 'function')) {
throw 'filenameOrFn must be a string or function';
}

if (typeof filenameOrFn === 'string') {
filenameOrFn = require(path.resolve(configDir, filenameOrFn));
if (typeof filenameOrFn === 'function') {
returned = filenameOrFn.apply(null, args);
}
}
if (typeof filenameOrFn === 'function') {
var results = webdriver.promise.controlFlow().execute(function() {
return filenameOrFn.apply(null, args);
}, 'executing onPrepare').then(null, function(err) {
err.stack = exports.filterStackTrace(err.stack);
throw err;
});
resolve(results);
} else {
throw 'filenameOrFn must be a string or function';
resolve();
}
}
return q(returned);
});
};

/**
Expand Down

0 comments on commit 289dbb9

Please sign in to comment.