2014-02-13 52 views
52

在測試規範中,我需要點擊網頁上的按鈕,然後等待新頁面完全加載。量角器:單擊按鈕後如何等待頁面完成?

emailEl.sendKeys('jack'); 
passwordEl.sendKeys('123pwd'); 

btnLoginEl.click(); 

// ...Here need to wait for page complete... How? 

ptor.waitForAngular(); 
expect(ptor.getCurrentUrl()).toEqual(url + 'abc#/efg'); 
+2

你能更具體地在你正在等待的頁面上嗎?你只是在等待換頁或進行一些異步加載?理論上是 – Furzel

回答

67

取決於你想做什麼,你可以嘗試:

browser.waitForAngular();

btnLoginEl.click().then(function() { 
    // do some stuff 
}); 

解決的承諾。如果你能在beforeEach中做到這一點會更好。

注意:我注意到expect()在比較之前等待內部的承諾(即getCurrentUrl)被解決。

+0

感謝分享,幫助識別一些奇怪的間歇性問題。 –

+0

我仍然有這個問題:超時等待異步腳本結果 – Emna

+0

這並沒有解決我的問題,等待JavaScript完成按鈕單擊後修改頁面。按照下面的建議添加'browser.driver.sleep(1000)'(https://stackoverflow.com/a/27551014/2745116)。 – CGFoX

23

您不需要等待。量角器自動等待角度準備就緒,然後執行控制流程中的下一步。

+2

是的,因爲'waitForAngular()'在內部被調用,但我也必須爲它調用一些規格。 – glepretre

+7

它會自動等待嗎?看着'browser.get'之類的東西,它明確地說它存在覆蓋它所包裝的東西。如果'ElementFinder'沒有'click'方法,那麼它似乎只是委託給'webDriver.WebElement'。 https://angular.github.io/protractor/#/api?view=ElementFinder – Snekse

+3

@Snekse量角器通過ElementFinder和ElementArrayFinder同步所有操作。因此,無論何時您的測試嘗試查找任何元素,查找本身都會等待角度來完成消解循環,然後將調用委託給webdriver。 expect()調用中會發生同樣的情況。 – Max

21

我只看了一下源代碼 - 量角器只在少數情況下等待Angular(例如調用element.all或設置/獲取位置)。

所以量角器不會等待角度在每個命令後穩定下來。

此外,它看起來就像有時在我的測試中我有角之間的競爭消化週期和點擊事件,所以有時候我必須做:用睡眠來等待執行進入AngularJS上下文(

elm.click(); 
browser.driver.sleep(1000); 
browser.waitForAngular(); 

click事件觸發)。

+1

量角器不會同步.click(),它會同步下一個涉及到ElementFinder/ElementArrayFinder的操作。 – Max

+0

browser.driver.sleep();是我的解決方案,thx:D – Arcagully

+0

我從經驗中得知,當使用'expect'評估期望值時,量角器將等待角度。在其他情況下,我明確使用'waitForAngular'。 – jrharshath

2

我通常只是添加了一些控制流程,即:

it('should navigate to the logfile page when attempting ' + 
    'to access the user login page, after logging in', function() { 
    userLoginPage.login(true); 
    userLoginPage.get(); 
    logfilePage.expectLogfilePage(); 
}); 

logfilePage:

function login() { 
    element(by.buttonText('Login')).click(); 

    // Adding this to the control flow will ensure the resulting page is loaded before moving on 
    browser.getLocationAbsUrl(); 
} 
2

在這種情況下,你可以使用:

頁面對象:

waitForURLContain(urlExpected: string, timeout: number) { 
     try { 
      const condition = browser.ExpectedConditions; 
      browser.wait(condition.urlContains(urlExpected), timeout); 
     } catch (e) { 
      console.error('URL not contain text.', e); 
     }; 
    } 

頁測試:

page.waitForURLContain('abc#/efg', 30000); 
-1
browser.waitForAngular(); 

btnLoginEl.click().then(function() { Do Something }); 

解決的承諾。

1

使用此我認爲這是更好

*isAngularSite(false);* 
    browser.get(crmUrl); 


    login.username.sendKeys(username); 
    login.password.sendKeys(password); 
    login.submit.click(); 

    *isAngularSite(true);* 

供您使用isAngularSite此設置應該把這個在您的protractor.conf。JS這裏:

 global.isAngularSite = function(flag) { 
     browser.ignoreSynchronization = !flag; 
     }; 
0

用量角器,您可以用下面的辦法

var EC = protractor.ExpectedConditions; 
// Wait for new page url to contain newPageName 
browser.wait(EC.urlContains('newPageName'), 10000); 

所以,你的代碼將看起來像,

emailEl.sendKeys('jack'); 
passwordEl.sendKeys('123pwd'); 

btnLoginEl.click(); 

var EC = protractor.ExpectedConditions; 
// Wait for new page url to contain efg 
ptor.wait(EC.urlContains('efg'), 10000); 

expect(ptor.getCurrentUrl()).toEqual(url + 'abc#/efg'); 

注:這並不意味着新的一頁已完成加載並準備好了DOM。隨後的expect()語句將確保Protractor等待DOM可用於測試。

參考:Protractor ExpectedConditions

相關問題