javascript 量角器计数元素并存储整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33114091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protractor count elements and store the integer
提问by Osan
I want to count the elements in a list and then access the integer, not a Promiseobject. So starting with:
我想计算列表中的元素,然后访问整数,而不是Promise对象。所以开始:
var questionList = questionContainer.all(by.className('someclass'));
If there are three child elements with that class, I want console.log(questionList.count())to output the integer 3, not a Promiseobject. Is this even possible? Even if it is some operation on the Promise.
如果该类有三个子元素,我想console.log(questionList.count())输出 integer 3,而不是一个Promise对象。这甚至可能吗?即使是对Promise.
回答by alecxe
protractorhas the count()method available on ElementArrayFinder:
protractor有count()可用的方法ElementArrayFinder:
expect(questionList.count()).toEqual(3);
Note that count()returns a promise, expect()is "patched" to resolve promises implicitly.
请注意,count()返回一个 promise,expect()被“修补”以隐式解决承诺。
If you need the actual value to be, for instance, printed on the console - resolve the promise explicitly with then():
例如,如果您需要将实际值打印在控制台上 - 使用then()以下命令明确解析承诺:
questionList.count().then(function (count) {
console.log(count);
});
Or, even simpler:
或者,更简单:
questionList.count().then(console.log);
回答by winlinuz
And, for instance, store the integer for using if statement?
并且,例如,存储整数以使用 if 语句?
questionList.count().then(function (count) {
var res = count;
});
if (res < 3) ...
It would be this way?
会是这样吗?

![javascript [CORS]跨源请求仅支持以下协议方案:http、data、chrome、chrome-extension、https、chrome-extension-resource](/res/img/loading.gif)