adbarads
adbarads

Reputation: 1303

Protractor - variable assigned to element.all.locator.count is returning NaN why?

I am using Protractor, and trying to grab all elements within a drop down list.

I have the following and this part appears to be working:

var counter = element.all(By.className('class')).count();
console.log(counter);

I expected the value to be 43 instead I received this from the console log:

{ then: [Function: then],
  cancel: [Function: cancel],
  isPending: [Function: isPending] }

So not giving up, I then decided to see if I can output this value to a search bar inside the web application I'm testing on. To see what value it displays:

element(by.className('search_bar')).sendKeys(counter);

When I run it again: I see the value in the search bar become populated with 43. This is what I am expecting to be what counter is equal to.

I then proceed to use the variable 'counter' in other places, and I keep getting values back as:

NaN

How do I get it to pass the actual number value of 43 but as a variable.

Upvotes: 1

Views: 1417

Answers (1)

alecxe
alecxe

Reputation: 474271

protractor is a wrapper around WebdriverJS which is completely asynchronous and is based on a concept of promises. Basically, when you write protractor tests, think about everything as a promise. See Promises and the Control Flow.

count() returns a promise. In order to see or use an actual count value, resolve it:

var counter = element.all(By.className('class')).count();
counter.then(function (value) {
    element(by.className('search_bar')).sendKeys(value);
});

Note that if you pass a promise to expect() it would make an expectation only when the promise is resolved. This is because it is enhanced to do so in jasminewd which protractor depends on. In other words, you can write:

expect(counter).toEqual(10);

and let expect() worry about the promise.

Upvotes: 2

Related Questions