2

In the official documentation you can find the following code:

var history = element.all(by.repeater('result in memory'));
expect(history.count()).toEqual(2);

But you can also find examples using promises

element.all(by.repeater('app in userApps')).count().then(function(count) {
    console.log(count);
});

So why does Protractor sometimes returns a promise and sometimes it returns a value?

Community
  • 1
  • 1
Pablo
  • 2,540
  • 1
  • 18
  • 26

3 Answers3

3

history.count() does return a promise, but Protractor adapts Jasmine expect to understand promises.

https://github.com/angular/protractor/blob/master/docs/control-flow.md

Jkarttunen
  • 6,764
  • 4
  • 27
  • 29
3

It always returns a promise, it's just that expect is patched to handle them by adding them to the control-flow so that they get executed and resolved in the proper order.

Delian Mitankin
  • 3,691
  • 26
  • 24
2

the protractor folks "patched" jasmine to be promise aware. that is, the expect statement does duck-typing -- if it is a promise, it waits for it to resolve and executes the underlying assertion. if it is any other type, it executes the assertion as it would in any other jasmine world.

source

sylvain
  • 1,951
  • 2
  • 19
  • 34