在 Protractor 测试中同步处理
Dealing synchronously in Protractor tests
我正在尝试用 Protractor 编写我认为相当简单的测试,但似乎在您尝试同步执行任何操作的那一刻,Protractor 让您的生活变得艰难!通常,处理定位器函数(return 一个承诺)不是问题,因为任何 expect 语句都会在测试断言之前自动解析传递给它的任何承诺语句。但是,我要做的是在 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' 调用 return 是一个承诺,因此实际上不能以这种方式调用。将其称为承诺(即使用 then)意味着我的 forEach 块在我记录元素是否存在于页面上之前退出。
我一头雾水,请问有没有人遇到过类似的情况?
我已使用 bluebird 执行以下操作;
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);
});
希望对您有所帮助
所以 elementImLookingFor
是 element.all
返回的承诺,我想?或者,如 Protractor 文档中所述,ElementArrayFinder。您可以在其上调用方法 .each()
并向其传递 expect
的函数。
我正在尝试用 Protractor 编写我认为相当简单的测试,但似乎在您尝试同步执行任何操作的那一刻,Protractor 让您的生活变得艰难!通常,处理定位器函数(return 一个承诺)不是问题,因为任何 expect 语句都会在测试断言之前自动解析传递给它的任何承诺语句。但是,我要做的是在 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' 调用 return 是一个承诺,因此实际上不能以这种方式调用。将其称为承诺(即使用 then)意味着我的 forEach 块在我记录元素是否存在于页面上之前退出。
我一头雾水,请问有没有人遇到过类似的情况?
我已使用 bluebird 执行以下操作;
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);
});
希望对您有所帮助
所以 elementImLookingFor
是 element.all
返回的承诺,我想?或者,如 Protractor 文档中所述,ElementArrayFinder。您可以在其上调用方法 .each()
并向其传递 expect
的函数。