Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 917
alecxe Avatar asked Jan 11 '16 02:01

alecxe


1 Answers


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

like image 94
Linh Pham Avatar answered Sep 28 '22 09:09

Linh Pham