我正在尝试在量角器中编写一个我认为相当简单的测试,但似乎当您尝试同步执行任何操作时,量角器就会让您的生活变得困难!通常,处理定位器函数(返回 Promise)不是问题,因为任何 Expect 语句都会在测试断言之前自动解析传递给它的任何 Promise 语句。但是,我想要做的涉及在 Expect 语句之前解决这些定位器承诺,以便我可以有条件地执行一些测试逻辑。考虑(伪代码):
// 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”调用返回一个承诺,因此实际上不能以这种方式调用。将其称为承诺(即使用 then)意味着我的 forEach 块在我记录该元素是否存在于页面上之前就退出了。
我对如何解决这个问题一片空白,有人遇到过类似的情况吗?
我用过bluebird https://github.com/petkaantonov/bluebird/blob/master/API.md执行以下操作;
it('element should be present', function(done)
Promise.cast(elementImLookingFor.isPresent)
.then(function(present){
expect(present).toBeTruthy();
})
.nodeify(done);
});
如果您想检查一些元素isPresent
您应该能够执行以下操作;
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);
});
希望这可以帮助
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)