简体   繁体   中英

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?

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM