Protractor: How do promises work?

后端 未结 2 1209
情书的邮戳
情书的邮戳 2021-01-02 01:06

I have a decent idea of how the $q library in angular works but I\'m not sure how protractor or web-driver-js uses them. (especially since the utilizations are slightly diff

相关标签:
2条回答
  • 2021-01-02 01:18

    Here is a good article about Protractor control flow, itself inspired by the WebDriver API.

    In short (quoting the article), here is how the ControlFlow of WebDriver works under the hood.

    When you write:

    driver.get('http://www.example.com');
    driver.findElement(elem).sendKeys('hello');
    

    What actually happens is:

    driver.get('http://www.example.com')
      .then(function() {
        return driver.findElement(elem);
      })
      .then(function(q) {
        return q.sendKeys('hello');
      });
    



    Note: I do not intend to be redundant with the accepted answer, but I believe seeing how promises are actually chained internally can help understand.

    0 讨论(0)
  • 2021-01-02 01:22

    Is there some sort of implicit promising going on?

    Looking at https://github.com/angular/protractor/blob/master/docs/control-flow.md , it looks like the answer is yes, there is, by using a queue of promises, called the control flow. So to expand on your example:

    browser.get(url);
    var title = browser.getTitle();
    expect(title).toEqual('My Title');
    

    Each of the lines above adds to the queue. The variable title is actually a promise, which, at the appropriate point in the control flow, expect unwraps.

    0 讨论(0)
提交回复
热议问题