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

Commit 3508335

Browse files
committed
fix(element): ElementArrayFinder.count was slow
The bug fix for #1903 (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(#2359)
1 parent 69404aa commit 3508335

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

lib/element.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -350,23 +350,7 @@ ElementArrayFinder.prototype.toElementFinder_ = function() {
350350
*/
351351
ElementArrayFinder.prototype.count = function() {
352352
return this.getWebElements().then(function(arr) {
353-
var list = arr.map(function(webElem) {
354-
// Calling any method forces a staleness check
355-
return webElem.isEnabled().then(function() {
356-
return 1; // is present
357-
}, function(err) {
358-
if (err.code == webdriver.error.ErrorCode.STALE_ELEMENT_REFERENCE) {
359-
return 0; // not present
360-
} else {
361-
throw err;
362-
}
363-
});
364-
});
365-
return webdriver.promise.all(list).then(function(presenceArr) {
366-
return presenceArr.reduce(function(acc, isPresent) {
367-
return acc + isPresent;
368-
}, 0);
369-
});
353+
return arr.length;
370354
}, function(err) {
371355
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ELEMENT) {
372356
return 0;
@@ -922,8 +906,25 @@ ElementFinder.prototype.$ = function(selector) {
922906
* the element is present on the page.
923907
*/
924908
ElementFinder.prototype.isPresent = function() {
925-
return this.parentElementArrayFinder.count().then(function(count) {
926-
return !!count;
909+
return this.parentElementArrayFinder.getWebElements().then(function(arr) {
910+
if (arr.length == 0) {
911+
return false;
912+
}
913+
return arr[0].isEnabled().then(function() {
914+
return true; // is present, whether it is enabled or not
915+
}, function(err) {
916+
if (err.code == webdriver.error.ErrorCode.STALE_ELEMENT_REFERENCE) {
917+
return false;
918+
} else {
919+
throw err;
920+
}
921+
});
922+
}, function(err) {
923+
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ELEMENT) {
924+
return false;
925+
} else {
926+
throw err;
927+
}
927928
});
928929
};
929930

0 commit comments

Comments
 (0)