最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - count() vs length in Protractor - Stack Overflow

programmeradmin8浏览0评论

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?

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?

Share Improve this question asked Jan 11, 2016 at 2:15 alecxealecxe 474k127 gold badges1.1k silver badges1.2k bronze badges
Add a comment  | 

1 Answer 1

Reset to default 15

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

发布评论

评论列表(0)

  1. 暂无评论