Protractor在忽略同步期间等待,浏览器implicitTimeout vs browser.wait timeout

时间:2016-02-03 17:25:30

标签: javascript selenium selenium-webdriver webdriver protractor

我有一个应用程序在单击按钮时启动$ timeout,因此我必须将ignoreSynchronization设置为true。在此期间,我需要等待将元素添加到页面中,并且在等待期间我遇到了一些有趣的行为:

在browser.wait(元素,超时,错误消息)期间传入的等待超时不执行任何操作。唯一重要的等待是在浏览器上设置的implicitTimeout。最重要的是,将使用ENTIRE隐式超时。如果发现该元素存在,它将继续检查直到超时结束。这意味着测试将始终缓慢运行,并给出最大时间。

describe('Cool Page', () =>{
  beforeEach(function(){
    browser.ignoreSynchronization = true;
    return browser.sleep(250);
  });

  afterEach(function(){
    browser.ignoreSynchronization = false;
    return browser.sleep(250);
  });

  it('can open menu with timeout', function(){
    // No timeout at this point
    coolPage.wait.ellipsesIcons().then(() =>{
       // Clicking the ellipses icons kicks off a $timeout of 100 seconds
       coolPage.ellipsesIcons.click().then(() =>{
          coolPage.wait.dropdownOptions().then(() => {
             expect(coolPage.dropdownOptions.getText()).toContain('Option 1');
          });
       });
    });
  });
})

  export = new CoolPage;
  class CoolPageextends PageObject {
    private elements;

    constructor(){
        super();
        ... // Lots of other stuff
        this.initWait();
    }

    ... // Initializing elements and other things

    private initWait() {
      this.wait = {
        ellipsesIcons: () => {
          // Timeout of 5 seconds will be used - regardless of isPresent resolving as true or false, the entire 5 seconds will be used
          browser.manage().timeouts().implicitlyWait(5000);
          // The 2 milliseconds passed in here does nothing at all
          browser.wait(element(by.css('.fa-ellipses-h')).isPresent(), 2, 'Ellipses Icon(...) was not present in time');
          // Must reset implicit wait back to the original 25 seconds it was set too in the conf
          browser.manage().timeouts().implicitlyWait(25000);
          return browser.sleep(150);
        },
        dropdownOptions: () => {
          // This two seconds wait WILL be used
          browser.manage().timeouts().implicitlyWait(2000);
          // This five second wait WILL NOT be used
          browser.wait(element(by.css('[name="change-status"]')).isPresent(), 5000, 'Options actions menu item was not present in time');
          browser.manage().timeouts().implicitlyWait(25000);
          return browser.sleep(150);
        },
      }
    }

通过browser.wait传递的超时对此没有影响。我的问题是:

  • browser.wait超时有什么作用?
  • 何时使用隐式等待?我以为它只是在等待页面加载
  • 有没有办法传递实际使用的超时?
  • 如果没有,是否有任何方法可以在条件满足时立即退出,而不是使用整个超时?

1 个答案:

答案 0 :(得分:0)

尝试在浏览器等待时使用expected conditions。使用函数textToBePresentInElement查看it('should wait for a series of periodic increments'示例。要检查元素是否存在,我可以尝试visibilityOfpresenceOf