From 36e0e0aaf090b0c9b5450fa59ba2da4c4237442a Mon Sep 17 00:00:00 2001 From: Hank Duan Date: Fri, 20 Jun 2014 12:07:58 -0700 Subject: [PATCH] fix(protractor): allow exceptions from actions to be catchable See https://github.com/angular/protractor/issues/959 --- lib/protractor.js | 29 +++++++++++++++++------------ spec/basic/elements_spec.js | 13 +++++++++++++ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/protractor.js b/lib/protractor.js index 4535dc5be..5a211df2e 100644 --- a/lib/protractor.js +++ b/lib/protractor.js @@ -110,12 +110,14 @@ var buildElementHelper = function(ptor) { return parentWebElement.findElements(this.locator_); } } else { - ptor.waitForAngular(); - if (this.locator_.findElementsOverride) { - return this.locator_.findElementsOverride(ptor.driver); - } else { - return ptor.driver.findElements(this.locator_); - } + var self = this; + return ptor.waitForAngular().then(function() { + if (self.locator_.findElementsOverride) { + return self.locator_.findElementsOverride(ptor.driver); + } else { + return ptor.driver.findElements(self.locator_); + } + }); } }; @@ -470,8 +472,13 @@ var buildElementHelper = function(ptor) { WEB_ELEMENT_FUNCTIONS.forEach(function(fnName) { if(!self[fnName]) { self[fnName] = function() { - var webElem = self.getWebElement(); - var actionResult = webElem[fnName].apply(webElem, arguments); + var args = arguments; + var webElem = self.getWebElement(); + var actionResult = webElem.then( + function(webElem_) { + return webElem_[fnName].apply(webElem_, args); + }); + return new ElementFinder( locator, opt_parentElementFinder, actionResult, opt_index); @@ -722,11 +729,9 @@ var buildElementHelper = function(ptor) { * @return {webdriver.promise.Promise} Promise which contains the results of * evaluating fn. */ - ElementFinder.prototype.then = function(fn) { + ElementFinder.prototype.then = function(fn, errorFn) { if (this.opt_actionResult_) { - return this.opt_actionResult_.then(function() { - return fn.apply(null, arguments); - }); + return this.opt_actionResult_.then(fn, errorFn); } else { return fn(this); } diff --git a/spec/basic/elements_spec.js b/spec/basic/elements_spec.js index 9808b3469..5696a02a8 100644 --- a/spec/basic/elements_spec.js +++ b/spec/basic/elements_spec.js @@ -292,6 +292,19 @@ describe('ElementFinder', function() { expect(element(byCss).locator()).toEqual(byCss); expect(element(byBinding).locator()).toEqual(byBinding); }); + + it('should propagate exceptions', function() { + browser.get('index.html#/form'); + var successful = protractor.promise.defer(); + + var invalidElement = element(by.binding('INVALID')); + invalidElement.getText().then(function(value) { + successful.fulfill(true); + }, function(err) { + successful.fulfill(false); + }); + expect(successful).toEqual(false); + }); }); describe('evaluating statements', function() {