Dinny
Dinny

Reputation: 59

When do I have to wait for a promise in Protractor?

I know there is similar questions on here about this, but I cannot make sense of them for the life of me.

Here's an example, where I need to click a button and check the url.

My initial thought is I would write it as

element(by.id('button')).click();
expect(browser.getCurrentUrl()).toContain('asyncisconfusing');

I know the expect handles its promise but what about the .click? Shouldn't I have to write it like this?

element(by.id('button')).click().then(() => {
    expect(browser.getCurrentUrl()).toContain('asyncisconfusing')
})

Or is protractor/webdriver auto-magically doing this?

Upvotes: 1

Views: 1213

Answers (2)

martin770
martin770

Reputation: 1288

http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/promise.html

The first section talks about how the control flow is used to manage promises without having to chain together every single command.

Upvotes: 0

alecxe
alecxe

Reputation: 474271

In theory, since Protractor maintains a queue of promises via Control Flow and works in sync with an AngularJS application under test, you should not resolve promises explicitly unless you need a real value for further processing. In other words, this should be the prefferred form:

element(by.id('button')).click();
expect(browser.getCurrentUrl()).toContain('asyncisconfusing');

In practice though, explicitly resolving click() promises, or adding explicit waits via browser.wait() helps to deal with occasional and random timing issues.

Upvotes: 5

Related Questions