Ad

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):

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

...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.

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

Count the number of elements represented by the ElementArrayFinder.

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

Ad

Answer


$$(".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.

Ad
source: stackoverflow.com
Ad