2017-09-27 40 views
0

我現在有在我的測試規格之一量角器下面的代碼:量角器時序問題

.then(function() { 
    return button.click(); 
}) 
.then(function() { 
    return element(otherButton).isDisplayed(); 
}) 
.then(function(otherButtonIsPresent) { 
    if(otherButtonIsPresent) { 
     return browser.wait(EC.elementToBeClickable(element(otherButton)), getWaitTime()) 
      .then(function() { 
       element(otherButton).click(); 
       return element(continueButton).isPresent(); 
      }) 
    } 
}) 

當我使用Chrome使用--debug-brk--inspect標誌調試,我能夠通過這些檢查和恢復像平常一樣。當我在沒有標誌的情況下運行相同的測試時,測試失敗並在嘗試點擊它之前查找otherButton期間停頓。

我想知道是否這是因爲在調試過程中,我設置了斷點並等待按鈕顯示在屏幕上,然後再嘗試單擊它們。

我需要確保這個元素在頁面上可見之前試圖點擊它,並想知道是否有另一種方式,如果完成此?

謝謝

+0

嘗試使用browser.wait()與ExpectedConditions沿着使的webdriver到直到某些特定條件。請參閱http://www.protractortest.org/#/api?view=ProtractorExpectedConditions。 –

回答

0

我使用的答案,因爲我還不能評論。

你基本上是自己提到的:點擊一個按鈕後,你只是想等到下一個按鈕可以點擊。 因此,您的.then()函數應該從它所依賴的按鈕開始。對我來說,似乎三個排隊的.then()函數依賴於相同的條件,所以在你.click()第一個按鈕之後,第二個.then()立即執行,而不是等待之前的.click()完成。

因此把.then()後面直接相關.click()和內前.then()功能全,這應該工作:

.then(function() { 
    element(button).click().then(function(){ 
     element(otherButton).click().then(function(){ 
      return element(continueButton).isPresent(); 
     }); 
    }); 
}); 

或者,如果你有ExpectedConitions去,你不應該需要.then() -functions。由於量角器應該管理ControlFlow,讓你把它寫不鏈接.then() -functions:

.then(function() { 
    browser.wait(EC.elementToBeClickable(element(button)), getWaitTime()); 
    element(button).click(); 
    browser.wait(EC.elementToBeClickable(element(otherButton)), getWaitTime()); 
    element(otherButton).click(); 
    browser.wait(EC.elementToBeClickable(element(continueButton)), getWaitTime()); 
    return element(continueButton).isPresent(); 
}); 

This nice post闡述了異步寫了一點,但由於量角器同步執行。

作爲替代的示例組合我的兩個輸入端,種雙固定測試:

.then(function() { 
    browser.wait(EC.elementToBeClickable(element(button)), getWaitTime()); 
    element(button).click().then(function(){ 
     browser.wait(EC.elementToBeClickable(element(otherButton)), getWaitTime()); 
     element(otherButton).click().then(function(){ 
      browser.wait(EC.elementToBeClickable(element(continueButton)), getWaitTime()); 
      return element(continueButton).isPresent(); 
     }); 
    }); 
});