2017-04-13 142 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.

相关问题