Skip to content

Commit

Permalink
fix(jasminewd): include full pre-async-call stack trace in expectatio…
Browse files Browse the repository at this point in the history
…n failure message
  • Loading branch information
DavidMikeSimon authored and juliemr committed Feb 28, 2014
1 parent 41feaca commit 767c306
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
5 changes: 5 additions & 0 deletions debugging/failure_spec.js
Expand Up @@ -15,6 +15,11 @@ describe('modes of failure', function() {
var nonExistant = element(by.binding('nopenopenope')).getText();
});

it('should fail to click a hidden element', function () {
browser.get('index.html#/form');
element(by.id("hiddenbutton")).click();
});

it('should fail to use protractor on a non-Angular site', function() {
browser.get('http://www.google.com');
}, 20000);
Expand Down
32 changes: 23 additions & 9 deletions jasminewd/index.js
Expand Up @@ -19,7 +19,6 @@ function seal(fn) {
};
};


/**
* Wraps a function so it runs inside a webdriver.promise.ControlFlow and
* waits for the flow to complete before continuing.
Expand All @@ -28,6 +27,20 @@ function seal(fn) {
*/
function wrapInControlFlow(globalFn) {
return function() {
var driverError = new Error();
driverError.stack = driverError.stack.replace(/ +at.+jasminewd.+\n/, '');

function asyncTestFn(fn) {
return function(done) {
var thing = flow.execute(function() {
fn.call(jasmine.getEnv().currentSpec);
}).then(seal(done), function(e) {
e.stack = driverError.stack;
done(e);
});
};
};

switch (arguments.length) {
case 1:
globalFn(asyncTestFn(arguments[0]));
Expand All @@ -47,14 +60,6 @@ function wrapInControlFlow(globalFn) {
throw Error('Invalid # arguments: ' + arguments.length);
}
};

function asyncTestFn(fn) {
return function(done) {
flow.execute(function() {
fn.call(jasmine.getEnv().currentSpec);
}).then(seal(done), done);
};
};
};

global.it = wrapInControlFlow(global.it);
Expand All @@ -73,6 +78,8 @@ global.afterEach = wrapInControlFlow(global.afterEach);
function wrapMatcher(matcher, actualPromise, not) {
return function() {
var originalArgs = arguments;
var matchError = new Error("Failed expectation");
matchError.stack = matchError.stack.replace(/ +at.+jasminewd.+\n/, '');
actualPromise.then(function(actual) {
var expected = originalArgs[0];
if (expected instanceof webdriver.promise.Promise) {
Expand All @@ -92,7 +99,14 @@ function wrapMatcher(matcher, actualPromise, not) {
if (not) {
expectation = expectation.not;
}
var origFn = expectation.spec.addMatcherResult;
var error = matchError;
expectation.spec.addMatcherResult = function(result) {
result.trace = error;
jasmine.Spec.prototype.addMatcherResult.call(this, result);
}
expectation[matcher].apply(expectation, originalArgs);
expectation.spec.addMatcherResult = origFn;
}
});
};
Expand Down
1 change: 1 addition & 0 deletions testapp/form/form.html
Expand Up @@ -66,6 +66,7 @@ <h4>Buttons</h4>
<button id="exacttext">Exact text</button>
<button id="otherbutton">Partial button text</button>
<button id="trapbutton">No match</button>
<button id="hiddenbutton" style="display:none">Can't see me!</button>
<input type="submit" value="Exact text" id="submitbutton"/>
<input type="button" value="Hello text" id="inputbutton"/>
</div>

0 comments on commit 767c306

Please sign in to comment.