2015-01-13 81 views
3

我對量角器相對來說比較新,而且我還沒有能夠使量角器等待頁面在測試之前卸載。下面的例子:需要量角器在測試之前等待服務返回

//in loginPage object 
    function login(email, password) { 
     element(by.id('inputEmail')).sendKeys(email); 
     element(by.id('inputPassword')).sendKeys(password); 
     element(by.css('.btn.btn-primary')).click(); 
     browser.driver.sleep(4000); 
     return !(element(by.binding('userCtrl.labels.signIn()')).isPresent()); 
    } 

睡眠語句不然而,工作所看到BU即使在成功登錄和瀏覽器登錄頁面導航離開下面的測試總是失敗:

 //in separate test page 
     it('should allow a valid user to login', function() { 
      expect(loginPage.login('[email protected]', '12345678')).toBe(true); 
     }); 

謝謝!

回答

3

量角器動作(例如isPresent())返回承諾,而不是基礎值。 即這是一個承諾:element(by.binding('userCtrl.labels.signIn()')).isPresent()

請閱讀這個https://github.com/angular/protractor/blob/master/docs/control-flow.md

這應該通過:

function login(email, password) { 
    element(by.id('inputEmail')).sendKeys(email); 
    element(by.id('inputPassword')).sendKeys(password); 
    element(by.css('.btn.btn-primary')).click(); 
    browser.driver.sleep(4000); 
    return element(by.binding('userCtrl.labels.signIn()')).isPresent(); 
} 

-

//in separate test page 
it('should allow a valid user to login', function() { 
    expect(loginPage.login('[email protected]', '12345678')).toBe(false); 
}); 

什麼指望也被解開的承諾,這樣你才能堅持對其基礎價值。

+0

如果登錄成功,我需要登錄函數返回值在expect語句中解析爲true,如果登錄函數沒有解析爲false,則解析爲false。我理解你的推理,並且解決了你提到的問題,但問題仍然存在。 – fortesl

+0

無視我之前的評論。您的解決方案完全按照給定的工作。謝謝! – fortesl

相關問題