Count () vs length in Protractor

According to the documentation, there are two ways to get the number of elements inside an ElementArrayFinder (the result of calling element.all() ):

  • $$(".myclass").length , registered here :

... the array has a length equal to the length elements found with the ElementArrayFinder , and each result represents the result of the action on the element.

  • $$(".myclass").count() , registered here :

Count the number of elements represented by the ElementArrayFinder .

What is the difference between the two methods and which one is preferable?

+6
source share
1 answer

$$(".myclass").length

It is necessary to resolve the promise to get the length of the element correctly.

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

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

A wrapper for $$('.myclass').length , which in itself is a promise and does not require resolution of the promise as .length

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

which one is preferable?

If when placing $$(".myclass") and .then(function(items){...}) there is no complicated business, then items.length will give better performance. Otherwise, you should always use $$(".myclass").count() .

+11
source

All Articles