1

I have testing code that looks like this:

pageLogin(userName: string, password: string) {
    element(by.id('loginUserName')).clear();
    element(by.id('loginPassword')).clear();
    element(by.id('loginUserName')).sendKeys(userName);
    element(by.id('loginPassword')).sendKeys(password);
    element(by.id('loginButton')).click();
    expect(element(by.id('ngApp')).isPresent()).toBe(true);
}

Can someone explain to me. Do I need to wait for promises to be returned from the first five rows of my function before the expect? Are the lines inside this function executed async?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • Is this Javascript? `pageLogin(userName: string, password: string) {` is pageLogin() call done within an `it()` Jasmine spec? – Leo Gallucci Sep 08 '14 at 16:31

1 Answers1

2

Protractor uses a method called control flows to let you write tests in a way that looks synchronous. What actually happens is that call of the calls you make inside your function queue up and only start executing when the function returns.

So when your function finishes running, protractor looks at the queue and executes the first item, which is the element(by.id('loginUserName')).clear() statement. When the browser has finished clearing the loginUserName textbox, it signals protractor, and protractor goes to execute the next item in queue.

In your example, element(by.id('ngApp')).isPresent() would be the last item in queue. This calls also returns a promise that will be resolved after this item will have been removed from queue, executed and returned a value.

Jasmine's expect() function was adapted so instead of checking the value synchronously, it notices that you passed in a promise, so it waits for that promise to resolve before checking the expectation.

Finally, once all of the items in the queue have been executed, protractor finishes running the text and moves on to the next one.

urish
  • 8,943
  • 8
  • 54
  • 75
  • Yes I understand that .isPresent() returns a promise. But what about the lines above that? – Samantha J T Star Sep 08 '14 at 18:02
  • thank you for your detailed answer. I have another question related to this but a bit more in depth. Rather than ask more in this question I will open another question to ask about that. – Samantha J T Star Sep 09 '14 at 08:01
  • Here is the link to the other question that has an open bounty of 500: http://stackoverflow.com/questions/25689090/is-there-any-way-to-optimize-speed-up-the-sending-of-data-to-a-ui-with-protrac – Samantha J T Star Sep 09 '14 at 08:07