2015-06-20 211 views
10

首先,我已經檢查過關於該點的各種帖子和博客,但我仍然無法弄清楚如何正確設置它。量角器 - 在做下一步之前等待異步承諾

我已經嘗試了許多不同的combinaison:

  • 瀏覽器等
  • protractor.controlFlow()執行
  • protractor.controlFlow()等待(

...。仍然沒有成功..

我的問題

在我的beforeEach函數中,我想調用量角器承諾並等待它在執行其餘代碼之前解決。

我的代碼

我準備的人願意這個簡單的測試,以幫助我

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 
+0

「測試Func鍵 - 步2」之前在'it'「最後一絲」,這是你的問題說什麼要發生的事情發生(beforeEach發生在它之前)。如果您想在beforeEach依賴項中進行步驟*,您需要明確地(通過'then'或單獨的控制流注冊)表示,這就是JavaScript/WebDriver/Protractor的工作方式。 –

+0

我試圖定義一個沒有成功的控制流程...你能告訴我一個解決方案嗎? – aorfevre

+0

對於任何人來到這裏,'browser.wait'返回一個承諾。沒有必要在內部建立一個承諾並將其退回。 – nilesh

回答

10

我認爲這會得到你想要的輸出:

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") 
    }); 

}); 
+0

完美。正是我在找什麼。 Thx爲解釋有關browser.wait承諾 – aorfevre

+0

你能顯示你從這段代碼得到的實際運行輸出嗎? –

0

兩個console.log S(步驟1 & 3)在你的testFunc,未包裹的承諾,將立即觸發。你的輸出證明了這一點。然後你的承諾(看起來工作起來很棒!)在解決承諾時(但在日誌已經被解僱之後)返回步驟2的日誌。

因此,它看起來像這是做你想要的?即你的beforeEach的確看起來像是在觸發你的第一個規範之前觸發異步函數。