2017-04-13 69 views
1

我有一個無棱角的登錄頁面,以我的應用程序,我想第一次登錄:如何在繼續之前讓量角器等待登錄?

describe('Authentication', function() { 
    it('should authenticate a user', function() { 
    browser.driver.get('https://example.com') 

    browser.driver.findElement(by.id('username')).sendKeys("user"); 
    browser.driver.findElement(by.id('password')).sendKeys("mypass"); 
    browser.driver.findElement(by.tagName('input')).click() 
    var url = browser.getLocationAbsUrl() 
    browser.driver.sleep(1) 
    browser.waitForAngular() 

    return 
    }) 
}) 

然而,這給出了一個錯誤:

Failed: Error while waiting for Protractor to sync with the page: "window.angular is undefined. This could be either because this is a non-angular page or bec 
ause your test involves client-side navigation, which can interfere with Protractor's bootstrapping. See http://git.io/v4gXM for details" 

我能做些什麼來解決這個?

+1

非角度,您需要在該頁面上執行命令之前設置'browser.ignoreSynchronization = false'。此外,僅供參考,「睡眠」以毫秒爲單位,而不是秒。你正在等待0.001秒,目前 – Gunderson

+0

我加了'browser.ignoreSynchronization = false'並得到了同樣的錯誤 – Shamoon

+0

哇,對不起,我的意思是'true' ... – Gunderson

回答

1

我寫在過去的一些助手讓我E2E檢驗這項工作:

waitForUrlToChangeTo: function (urlToMatch) { 
    var currentUrl; 
    return browser.getCurrentUrl().then(function storeCurrentUrl(url) { 
      currentUrl = url; 
     }) 
     .then(function waitForUrlToChangeTo() { 
      browser.ignoreSynchronization = true; 
      return browser.wait(function waitForUrlToChangeTo() { 
       return browser.getCurrentUrl().then(function compareCurrentUrl(url) { 
        browser.ignoreSynchronization = false; 
        return url.indexOf(urlToMatch) !== -1; 
       }); 
      }); 
     } 
    ); 
}, 
login : function (username, password, url) { 
    browser.get('#/login'); 
    element(by.model('username')).sendKeys(username); 
    element(by.model('password')).sendKeys(password); 
    element(by.buttonText('LOGIN')).click(); 
    return this.waitForUrlToChangeTo(url); 
} 

而且然後在測試中:

describe('when I login with valid credentials', function() { 
    it('should redirect to dashboard', function() { 
     helper.login('user', 'pass', '#/dashboard').then(function() { 
      expect(browser.getTitle()).toMatch('Dashboard'); 
     }); 
    }); 
}); 
+0

我得到同樣的錯誤'失敗:等待量角器時出錯與頁面同步:「window.angular是未定義的,這可能是因爲這是一個非角度頁面,或者因爲你的測試涉及客戶端導航,這可能會干擾量角器的自引導,請參閱http:// git。 io/v4gXM的詳細信息「' – Shamoon

1

我會說等待登錄頁面,直到它顯示正確,比做動作。例如,對於

  • 以登錄頁面中的某個元素爲目標並等待它。
  • 等待網址變更等

login -> browser.sleep(500)/wait for logged in page's element/URL change -> other action

browser.driver.wait(function(){ 
    expectedElement.isDisplayed().then(function (isVisible){ 
      return isVisible === true; 
      },50000, 'Element not present '); 
},50000); 

if that element is not present within specified time, timeout error would display & you would know unitl that time it's not logged in.

相關問題