简体   繁体   中英

Protractor count elements and store the integer

I want to count the elements in a list and then access the integer, not a Promise object. So starting with:

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 Promise object. Is this even possible? Even if it is some operation on the Promise .

protractor has the count() method available on ElementArrayFinder :

expect(questionList.count()).toEqual(3);

Note that count() returns a promise , expect() is "patched" to resolve promises implicitly.

If you need the actual value to be, for instance, printed on the console - resolve the promise explicitly with then() :

questionList.count().then(function (count) {
    console.log(count);
});

Or, even simpler:

questionList.count().then(console.log);

And, for instance, store the integer for using if statement?

questionList.count().then(function (count) {
     var res = count; 
});

if (res < 3) ...

It would be this way?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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