From 2a765c76e121de13ff86a279fe3f21cb15c17783 Mon Sep 17 00:00:00 2001 From: Hank Duan Date: Fri, 6 Mar 2015 12:34:44 -0800 Subject: [PATCH] fix(element): return not present when an element disappears --- lib/element.js | 18 +++++++++++++++++- spec/basic/elements_spec.js | 11 +++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/element.js b/lib/element.js index baf6d527b..32e5c78b3 100644 --- a/lib/element.js +++ b/lib/element.js @@ -353,7 +353,23 @@ ElementArrayFinder.prototype.toElementFinder_ = function() { */ ElementArrayFinder.prototype.count = function() { return this.getWebElements().then(function(arr) { - return arr.length; + var list = arr.map(function(webElem) { + // Calling any method forces a staleness check + return webElem.isEnabled().then(function() { + return 1; // is present + }, function(err) { + if (err.code == webdriver.error.ErrorCode.STALE_ELEMENT_REFERENCE) { + return 0; // not present + } else { + throw err; + } + }); + }); + return webdriver.promise.all(list).then(function(presenceArr) { + return presenceArr.reduce(function(acc, isPresent) { + return acc + isPresent; + }, 0); + }); }, function(err) { if (err.code == webdriver.error.ErrorCode.NO_SUCH_ELEMENT) { return 0; diff --git a/spec/basic/elements_spec.js b/spec/basic/elements_spec.js index f67ef569d..093d4dc8e 100644 --- a/spec/basic/elements_spec.js +++ b/spec/basic/elements_spec.js @@ -364,6 +364,17 @@ describe('ElementArrayFinder', function() { expect(element.all(by.binding('doesnotexist')).count()).toEqual(0); }); + it('should return not present when an element disappears within an array', + function() { + browser.get('index.html#/form'); + element.all(by.model('color')).then(function(elements) { + var disappearingElem = elements[0]; + expect(disappearingElem.isPresent()).toBeTruthy(); + browser.get('index.html#/bindings'); + expect(disappearingElem.isPresent()).toBeFalsy(); + }); + }); + it('should get an element from an array', function() { var colorList = element.all(by.model('color'));