【问题标题】:Protractor: wait method isn't work量角器:等待方法不起作用
【发布时间】:2015-10-27 23:21:00
【问题描述】:

我尝试使用 wait() 方法而不是 sleep(),但它不起作用。 我有代码:

 browser.actions().click(filter_field).perform();
 browser.sleep(3000);
 if (baloon_info.isPresent()) { //some expections }
 else { expect(true).toBe(false); }

现在我想做这样的事情:

 var present_pri = browser.wait(function () {
   return balloon_info.isPresent();
 }, 3000);
 if (present_pri) { //some expections }
 else { expect(true).toBe(false); }

但如果气球不存在,我会收到错误消息:Wait timed out after 3117ms 而不是 expected true to be false (present_pri == false)

我试着写:

var EC = protractor.ExpectedConditions;
browser.wait(EC.presenceOf(balloon_warning), 3000);
expect(balloon_warning.isPresent()).toBeTruthy();

但我总是有同样的错误。我做错了什么?

【问题讨论】:

  • 第一种方法总是会失败,因为 browser.wait 返回的是一个承诺,而不是你所期望的布尔值。(阅读 API 文档了解更多信息)

标签: angularjs asynchronous timeout jasmine protractor


【解决方案1】:

您需要处理等待超时错误

browser.wait(EC.presenceOf(balloon_warning), 3000).then(function () {
    // success handler
}, function (error) {
    expect(true).toBe(false);
});

【讨论】:

    【解决方案2】:

    我终于找到了另一种解决方案:

    browser.wait(function () {
       return balloon_info.isPresent();
    }, 3000).then(function () {
       // success handler
    }).thenCatch(function () {
       expect(true).toBe(false);
    });
    

    【讨论】:

      【解决方案3】:

      根据您的问题,我的理解是您正在尝试查找 DOM 中是否存在元素(但是,这并不一定意味着应该显示它)。您收到等待错误,因为您正在等待 DOM 中不存在的元素。因此,如上所示,它会引发错误。要解决它,请尝试期待元素的存在而不等待它。因为默认情况下量角器有一个预定义的等待时间来检查 DOM 中元素的存在。这是一个小sn-p -

      it('Check for presence of the element', function(){
          expect(balloon_warning.isPresent()).toBe(true);
      }, 60000); //extra timeout of 60000 so that async error doesn't show up
      

      现在,如果您想不惜一切代价使用等待,请查看下面的示例 -

      it('Check for element with wait time of 3000 ms', function(){
          var EC = protractor.ExpectedConditions;
          browser.wait(EC.presenceOf(balloon_warning), 3000).then(function(){
              expect(balloon_warning.isPresent()).toBeTruthy();
          },function(err){
              console.log('error');
          });
      }, 60000);
      

      如果未找到元素,则等待函数将抛出错误并在控制台中打印。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-21
        • 1970-01-01
        • 2018-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多