Selenium webdriver 量角器-在执行下一步之前等待异步承诺

Selenium webdriver 量角器-在执行下一步之前等待异步承诺,selenium-webdriver,webdriver,promise,protractor,angularjs-e2e,Selenium Webdriver,Webdriver,Promise,Protractor,Angularjs E2e,首先,我已经查阅了很多关于这一点的帖子和博客,但我仍然不知道如何正确表达 我尝试了许多不同的组合: 浏览器等待 progrator.controlFlow().execute 量角器.控制流().等待( ……还是没有成功 我的问题 在beforeach函数中,我想调用一个量角器promise,并在执行其余代码之前等待它解析 我的代码 我为任何愿意帮助我的人准备了这个简单的测试 describe('testAsync', function() { beforeEach(function(

首先,我已经查阅了很多关于这一点的帖子和博客,但我仍然不知道如何正确表达

我尝试了许多不同的组合:

  • 浏览器等待
  • progrator.controlFlow().execute
  • 量角器.控制流().等待(
……还是没有成功

我的问题

在beforeach函数中,我想调用一个量角器promise,并在执行其余代码之前等待它解析

我的代码

我为任何愿意帮助我的人准备了这个简单的测试

describe('testAsync', function() {

  beforeEach(function() {
    console.log('beforeEach - step 1 ')

    browser.get("https://angularjs.org/");
    console.log('beforeEach - step 2 ')
    testFunc()
    console.log('beforeEach - after testFunc - step 3')

  });

  var testFunc = function(){

    console.log("testFunc - step 1")

    browser.wait(function() {
      var deferred = protractor.promise.defer();
      element(by.id('twitter-widget-1')).isPresent()
        .then(function (isPresent) {
          console.log("testFunc - step 2")
          deferred.fulfill(isPresent);
      });
      return deferred.promise;
    });

    console.log("testFunc - step 3")

  }

  it('test after BeforeEach', function() {
    console.log("Last trace")
  });

});
电流输出

[launcher] Running 1 instances of WebDriver
beforeEach - step 1
beforeEach - step 2
testFunc - step 1
testFunc - step 3
beforeEach - after testFunc - step 3
testFunc - step 2
Last trace
[launcher] Running 1 instances of WebDriver
beforeEach - step 1
beforeEach - step 2
testFunc - step 1
testFunc - step 2 // <------  This is within the promise resolve
testFunc - step 3
beforeEach - after testFunc - step 3
Last trace
预期产出

[launcher] Running 1 instances of WebDriver
beforeEach - step 1
beforeEach - step 2
testFunc - step 1
testFunc - step 3
beforeEach - after testFunc - step 3
testFunc - step 2
Last trace
[launcher] Running 1 instances of WebDriver
beforeEach - step 1
beforeEach - step 2
testFunc - step 1
testFunc - step 2 // <------  This is within the promise resolve
testFunc - step 3
beforeEach - after testFunc - step 3
Last trace
[launcher]运行1个WebDriver实例
每次之前-步骤1
每次之前-步骤2
testFunc-步骤1

testFunc-步骤2/您的
testFunc
中的两个
控制台.log
s(步骤1和步骤3)未包含在承诺中,当您调用该函数时将立即启动。您的输出证明了这一点。然后您的承诺(看起来工作得很好!)在承诺得到解决时返回步骤2日志(但在日志已经触发之后)


因此,它看起来像是在做你想做的事情?也就是说,你的
在每个
之前确实看起来像是在达到你的第一个规范之前启动了异步函数。

我认为这将得到你想要的输出:

describe('testAsync', function() {

  beforeEach(function() {
    console.log('beforeEach - step 1 ');

    // `get` implicitly registers a promise with the control flow
    browser.get("https://angularjs.org/");

    console.log('beforeEach - step 2 '); // runs "before" get above returns!

    testFunc().then(function() {
       // use a then to explicitly chain a dependency off a promise
       console.log('beforeEach - after testFunc - step 3');
    })

    protractor.promise.controlFlow().execute(function() {
       console.log('beforeEach - after testFunc, via controlFlow - step 4');
    });

    console.log('beforeEach - end of beforeEach - everything registered, nothing done');
  });

  var testFunc = function(){

    console.log("testFunc - step 1")

    // return browser wait promise to caller
    // `wait` also implicitly registers with the control flow
    return browser.wait(function() {
      return element(by.id('twitter-widget-1')).isPresent()
        .then(function (isPresent) {
          console.log("testFunc - step 2")
          return true; // tell wait its done by resolving then promise->element promise->wait
      });
    });
  }

  it('test after BeforeEach', function() {
    console.log("Last trace")
  });

});

“test Func-step 2”发生在
it
中的“Last trace”之前,这是您的问题所说的您希望发生的事情(Beforeach发生在it之前)。如果您希望在Beforeach依赖项中执行步骤,则需要显式地表示(通过
然后
或单独的控制流注册),这就是JavaScript/WebDriver/量角器的工作原理。我试图定义一个控制流,但没有成功…你能给我一个解决方案吗?对于任何来到这里的人,
browser.wait
返回一个承诺。无需在其中构建承诺并返回它。完美。正是我想要的。Thx获取有关browser.wai的解释你能展示一下你从这段代码中得到的实际运行输出吗?