Skip to content

Commit

Permalink
fix(element): ElementArrayFinder.count was slow
Browse files Browse the repository at this point in the history
The bug fix for angular#1903
(angular@2a765c7)
was causing ElementArrayFinder.prototype.count to be slow because of
the extranous checks that must be performed. However, the checks could be
delayed into the isPresent check, which will mitigate this issue

fixes(angular#2359)
  • Loading branch information
hankduan committed Jul 21, 2015
1 parent 69404aa commit 3508335
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions lib/element.js
Expand Up @@ -350,23 +350,7 @@ ElementArrayFinder.prototype.toElementFinder_ = function() {
*/
ElementArrayFinder.prototype.count = function() {
return this.getWebElements().then(function(arr) {
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);
});
return arr.length;
}, function(err) {
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ELEMENT) {
return 0;
Expand Down Expand Up @@ -922,8 +906,25 @@ ElementFinder.prototype.$ = function(selector) {
* the element is present on the page.
*/
ElementFinder.prototype.isPresent = function() {
return this.parentElementArrayFinder.count().then(function(count) {
return !!count;
return this.parentElementArrayFinder.getWebElements().then(function(arr) {
if (arr.length == 0) {
return false;
}
return arr[0].isEnabled().then(function() {
return true; // is present, whether it is enabled or not
}, function(err) {
if (err.code == webdriver.error.ErrorCode.STALE_ELEMENT_REFERENCE) {
return false;
} else {
throw err;
}
});
}, function(err) {
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ELEMENT) {
return false;
} else {
throw err;
}
});
};

Expand Down

0 comments on commit 3508335

Please sign in to comment.