Samantha J T Star
Samantha J T Star

Reputation: 32808

Do I need to check for promises before an expect check with Protractor and AngularJS?

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?

Upvotes: 1

Views: 243

Answers (1)

urish
urish

Reputation: 9033

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.

Upvotes: 2

Related Questions