2016-05-12 40 views
1

我一直在使用/學習量角器超過一個月了。量角器asnchronus執行問題

我知道,量角器文件說,它等待的角度調用來完成(http://www.protractortest.org/#/),這將確保所有 步驟同步執行等。

但我不找到它的方式。或者至少,我在腳本中找不到這種方式 許多時間量角器都在前面運行,例如,如果我點擊一個鏈接,獲取當前網址,然後驗證網址。

大多數情況下,URL值將會陳舊,即點擊鏈接後不會執行。下面是我的頁面對象代碼示例和相應的測試。

請建議如何確保所有測試步驟均以連續順序執行

Page Object 
this.getSPageLink(){ 
    return element(by.xpath("//a[@title='S']")); 
}; 
this.getLPageLink(){ 
    return element(by.xpath("//a[@title='L']")); 
}; 
this.goToSPage = function() { 
    (this.getSPageLink()).click(); 
    *//ERROR here, sometimes second click (below) doesn't wait for first 
    click to complete, and complains that link for 2 click (below) is 
    not found* 
    (this.getSLPageLink()).click(); 
    return browser.currentURL(); 
    *//ERROR HERE url line (e) is sometimes executed before* 
} 

Test Class 
it('::test SL page', function() { 
     pageObject.goToSpage(); 
     var currentURL=browser.getCurrentURL(); 
     expect(currentURL).toContain("SLink"); 
     *//ERROR HERE value in this variable "currentURL" is most of the 
     times Stale* 
}); 

it('::test SL2 page', function() { 
     pageObject.goToLpage(); 
     var currentURL=browser.getCurrentURL(); 
     expect(currentURL).toContain("Link"); 
     console.log("this line is always executed first")); 
     //ERROR here , this print line is always executed first 
}); 

回答

1

這是相當混亂和量角器文檔不是對這個很清楚,但量角器等待頁面使用getrefresh命令時,只有同步。在你的情況下,你正在使用click命令加載一個新頁面,所以在繼續之前,Protractor不會等待新頁面同步。 See the reasoning來自Protractor開發人員的此行爲。

所以在你的情況下,你需要照顧你的測試或頁面對象。在這兩種情況下,你可以點擊使用後是這樣的:

browser.wait(function() { 
    return browser.driver.getCurrentUrl().then(function (url) { 
     return /SLink/.test(url); 
    }); 
}, 10000); 

這將經常輪詢當前URL越好(在實踐中每500ms左右一次)長達10秒。當它在當前URL中找到「Slink」字符串時,它將繼續執行下一個命令。

+0

謝謝你的很好的解釋。 – sathya

+0

另外,有沒有一種首選的方法來確保所有測試行(IT塊)都是連續執行的?例如,console.log始終首先執行,而不是按它所在的順序執行。 – sathya

+0

實際上webdriverjs/protractor綁定中沒有500 ms的輪詢間隔.http://stackoverflow.com/a/34377095/771848。謝謝。 – alecxe