Protractor count elements and store the integer

13,395

Solution 1

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);

Solution 2

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?

Share:
13,395
Osan
Author by

Osan

Updated on June 27, 2022

Comments

  • Osan
    Osan almost 2 years

    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.