【问题标题】:Protractor no waiting for web page to change量角器无需等待网页更改
【发布时间】:2018-08-31 19:23:54
【问题描述】:

将 Angular 5 与量角器结合使用。

等待浏览器 url 更改以查看用户是否登录然后登陆内部仪表板页面时遇到问题。

规格:

fdescribe('Suite 1', function() {
  it('should login and land on the dashboard page', function() {
    // Arrange
    browser.ignoreSynchronization = true;

    const baseUrl = 'confidentialUrl';
    const email = 'email';
    const password = 'password';

    // Act
    browser.get(baseUrl);
    element(by.id('username')).sendKeys(email);
    element(by.id('password')).sendKeys(password);
    element(by.css('.submit-btn')).click();

    // Assert
    browser.wait(5000);
    const currentUrl = browser.getCurrentUrl();
    expect(currentUrl).toEqual('confidentialUrl/dashboard');
  });

执行“量角器 conf.js”时出现错误:

失败:等待条件必须是类似 Promise 的对象、函数或 条件对象

【问题讨论】:

  • 因为你关闭了同步,你需要自己解决promise。

标签: angular selenium protractor


【解决方案1】:

browser.wait 需要一个对象作为第一个参数,它是条件对象或类似承诺的对象。例如,您可以从protractor 创建一个expected condition

// Expected condition object
const EC = protractor.ExpectedConditions;
// wait 5 seconds for expected condition
// in this case it will wait for url contains a given value
browser.wait(EC.urlContains('foo'), 5000).then(() => {
   // then you can assert here
   const currentUrl = browser.getCurrentUrl();
   return expect(currentUrl).toEqual('confidentialUrl/dashboard');
});

我们通过使用ExpectedConditions解决了这类问题。

我编辑了你的代码,所以在你的情况下:

fdescribe('Suite 1', function() {
  it('should login and land on the dashboard page', function() {
    // Arrange
    browser.ignoreSynchronization = true;

    const baseUrl = 'confidentialUrl';
    const email = 'email';
    const password = 'password';

    // Act
    browser.get(baseUrl);
    element(by.id('username')).sendKeys(email);
    element(by.id('password')).sendKeys(password);
    element(by.css('.submit-btn')).click();

    // Assert
    const EC = protractor.ExpectedConditions;
    browser.wait(EC.urlContains('confidentialUrl/dashboard'), 5000).then(() => {
      const currentUrl = browser.getCurrentUrl();
      return expect(currentUrl).toEqual('confidentialUrl/dashboard');
    })

  }); 

或者你可以:

fdescribe('Suite 1', function() {
      it('should login and land on the dashboard page', function() {
        // Arrange
        browser.ignoreSynchronization = true;

        const baseUrl = 'confidentialUrl';
        const email = 'email';
        const password = 'password';

        // Act
        browser.get(baseUrl);
        element(by.id('username')).sendKeys(email);
        element(by.id('password')).sendKeys(password);
        element(by.css('.submit-btn')).click();

        // Assert
        return browser.driver.wait(function() {
          return browser.driver.getCurrentUrl().then(function(url) {
            return expect(url).toEqual('confidentialUrl/dashboard');
          });
        }, 10000);

      }); 

【讨论】:

  • 您能否在问题中编辑我上面的代码,然后尝试一下。上面的例子有点困惑。
  • 感谢您的更新。我运行了测试并得到了这个错误:失败:5004 毫秒后等待超时
  • 你能尝试增加更多的秒数吗?
  • 是的,我增加了 10 秒并得到了同样的错误。我确实注意到网址没有改变,例如从 /login 到 /dashboard 页面。似乎它会暂停浏览器而不是等待它改变
  • 您确定您的应用在登录到页面confidentialUrl/dashboard 后正在重定向吗?
猜你喜欢
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
相关资源
最近更新 更多