alecxe
alecxe

Reputation: 473833

count() vs length in Protractor

According to the documentation, there are 2 ways to get how many elements are inside the ElementArrayFinder (the result of element.all() call):

...the array has length equal to the length of the elements found by the ElementArrayFinder and each result represents the result of performing the action on the element.

Count the number of elements represented by the ElementArrayFinder.

What is the difference between these two methods and which one should be preferred?

Upvotes: 9

Views: 16088

Answers (1)

Linh Pham
Linh Pham

Reputation: 3025


$$(".myclass").length

Need to resolve the promise to get the length of element correctly.

// WORK
$$(".myclass").then(function(items){
  items.length;
});

// DOES NOT WORK
$$(".myclass").length; 

$$(".myclass").count()

A wrapper for $$('.myclass').length which being a promise itself and doesn't require to resolve promise like .length

$$(".myclass").count(); 

which one should be preferred?

Unless there some complex business when locating $$(".myclass") and .then(function(items){...}) involved then items.length will give better performance.

Otherwise $$(".myclass").count() should always be used.

Upvotes: 15

Related Questions