简体   繁体   English

Protractor - 通过 count() 遍历所有元素 function 对我不起作用

[英]Protractor - iterate through all elements by count() function doesn't work for me

I want to iterate through elements by:我想通过以下方式遍历元素:

let newsCards = element.all(by.css('.design-article--without-image.design-article.design-tile'));

  for(let card = 0; card <= newsCards.count(); card++) {} 

and it doesn't work for me.它对我不起作用。

It doesn't call any error, my test passes but don't do what it should.它没有调用任何错误,我的测试通过但没有做它应该做的事情。 I solve it by simple number iteration, but I would be happy to know why there is an issue to iterate with count().我通过简单的数字迭代来解决它,但我很高兴知道为什么用 count() 进行迭代会出现问题。

Can somebody help me with this?有人可以帮我吗?

newsCards.count() is a Promise. newsCards.count()是一个 Promise。 You need to resolve it first to get a number eventually.您需要先解决它才能最终获得一个数字。 Normally you do it this way using await keyword通常你使用await关键字这样做

let newsCards = element.all(by.css('.design-article--without-image.design-article.design-tile'));

let count = await newsCards.count()
for(let card = 0; card < count; card++) {}

Keep in mind, that what was said in comments is also right and you should be using < instead of <=请记住,评论中所说的也是正确的,您应该使用<而不是<=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM