等待量角器/Webdriver中的页面重定向 [英] Wait for page redirection in Protractor / Webdriver

查看:68
本文介绍了等待量角器/Webdriver中的页面重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试,该测试单击一个按钮并重定向到用户仪表板.发生这种情况时,Webdriver返回: javascript error: document unloaded while waiting for result.

I have a test that clicks a button and redirects to a user dashboard. When this happens Webdriver returns: javascript error: document unloaded while waiting for result.

要解决此问题,我在发生重定向的位置插入browser.sleep(2000)并假设我的CPU使用率很低,这可以解决此问题.但是,2000 ms是任意的并且很慢.是否有类似browser.waitForAngular()的东西会在expect(..)之前等待角度加载到重定向页面上?

To fix this I insert browser.sleep(2000) at the point where redirection occurs and assuming my CPU usage is low, this solves the issue. However, 2000 ms is arbitrary and slow. Is there something like browser.waitForAngular() that will wait for the angular to load on the redirected page before the expect(..)?

it('should create a new user', () => {
  $signUp.click();
  $email.sendKeys((new Date().getTime()) + '@.com');
  $password.sendKeys('12345');
  $submit.click();

  browser.sleep(2000); // Need alternative to sleep...

  // This doesn't do it...
  // browser.sleep(1);
  // browser.waitForAngular();

  $body.evaluate('user')
  .then((user) => {
    expect(user).toBe(true);
  });
});

推荐答案

您认为这样的方法对您有用吗?最多需要等待10秒钟,URL才能包含文本"pageTwo"或您输入的任何内容.

do you think something like this could work for you? This will wait up to 10 seconds for the url to include the text 'pageTwo', or whatever you put in.

var nextPageButton = $('#nextPage');
nextPageButton.click().then(function(){
    return browser.driver.wait(function() {
      return browser.driver.getCurrentUrl().then(function(url) {
        return /pageTwo/.test(url);
      });
    }, 10000);
};

只需粘贴您期望的网址的正则表达式即可.

Just stick in the regex of the url you are expecting.

或者,您也可以等待下一页中的某个元素出现:

Alternatively, you could wait for an element from the next page to appear as well:

var nextPageButton = $('#nextPage');
nextPageButton.click();

var elementFromSecondPage = $('#coolElement');
browser.wait(protractor.until.elementIsVisible(elementFromSecondPage), 5000, 'Error: Element did not display within 5 seconds');

使用.click时,量角器自然会等待角度操作来完成附加到该点击的动作,例如更改页面.但是,在页面更改时,您可能仍需要加载特定的内容,因此在该部分可用之前测试失败.使用它,它应该等待单击部分完成,然后等待元素出现.

When using .click, protractor will naturally wait for angular to finish the action attached to the click, such as changing the page. But, while the page change, you may still be needing something specific to be loaded, so the test fails before that part is available. Using this, it should wait for the click part to finish, then wait for the element to appear.

这篇关于等待量角器/Webdriver中的页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆