量角器测试中的同步处理 [英] Dealing synchronously in Protractor tests

查看:74
本文介绍了量角器测试中的同步处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写我认为是量角器中相当简单的测试,但似乎在您尝试同步执行任何操作的那一刻,量角器使您的生活变得艰难!通常,处理定位器函数(返回promise)不是问题,因为任何expect语句都会在测试断言之前自动解析传递给它的promise语句.但是,我想做的事情涉及在Expect语句之前解决这些定位器诺言,以便我可以有条件地执行一些测试逻辑.考虑(伪代码):

I'm trying to write what I think is a fairly simple test in protractor, but it would seem that the minute you try to do anything synchronously, Protractor makes life hard for you! Normally, dealing with locator functions (that return a promise) are not an issue, since any expect statement will automatically resolve any promise statement passed to it before testing the assertion. However, what I'm trying to do involves resolving these locator promises before the expect statement so that I can conditionally execute some test logic. Consider (pseudocode):

// Imagine I have a number of possible elements on the page
// and I wish to know which are on the page before continuing with a test.

forEach(elementImLookingFor){
  if (elementImLookingFor.isPresent) {
    // record the fact that the element is (or isnt) present
  }
}

// Now do something for the elements that were not found

但是,在我上面的示例中,"isPresent"调用返回了一个Promise,因此实际上不能以这种方式被调用.将其称为promise(即带有then)意味着如果页面上是否存在该元素,我的forEach块将在记录之前退出.

However, in my above example, the 'isPresent' call returns a promise, so can't actually be called in that way. Calling it as a promise (i.e. with a then) means that my forEach block exits before I've recorded if the element is present on the page or not.

我正在为此做一个空白,有人遇到过类似的事情吗?

I'm drawing a blank about how to go about this, has anyone encountered something similar?

推荐答案

我使用了

I've used bluebird to do the following;

it('element should be present', function(done)
  Promise.cast(elementImLookingFor.isPresent)
    .then(function(present){
      expect(present).toBeTruthy();
    })
    .nodeify(done);
});

如果要检查isPresent的元素很多,则应该可以执行以下操作;

If you have a few elements that you want to check the isPresent on you should be able to do the following;

it('check all elements are present', function(done){
  var promises = [element1, element2].map(function(elm){
    return elm.isPresent();
  });

  // wait until all promises resolve
  Promise.all(promises)
    .then(function(presentValues){
      // check that all resolved values is true
      expect(presentValues.every(function(present){
        return present;
      })).toBeTruthy(); 

    })
    .nodeify(done);
});

希望这会有所帮助

这篇关于量角器测试中的同步处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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