在量角器中传播承诺
q
库 有这个巧妙的功能来解析和传播多个承诺成单独的参数:
q
library has this neat feature to resolve and spread multiple promises into separate arguments:
如果你有一个数组的承诺,你可以使用 spread 作为替换然后.扩展函数将值扩展"到履行处理程序的参数.
If you have a promise for an array, you can use spread as a replacement for then. The spread function "spreads" the values over the arguments of the fulfillment handler.
return getUsername()
.then(function (username) {
return [username, getUser(username)];
})
.spread(function (username, user) {
});
在量角器中,我们尝试使用内置的protractor.promise
来自 WebDriverJS
.
In protractor, we are trying to use the built-in protractor.promise
coming from WebDriverJS
.
问题:
是否可以使用 protractor.promise
实现传播"功能?
Is it possible to have the "spread" functionality with protractor.promise
?
用例示例:
我们已经实现了一个自定义的 jasmine 匹配器来检查一个元素是否被聚焦.在这里,我们需要在进行相等比较之前解决两个 promise.目前,我们正在使用 protractor.promise.all()
和 then()
:
We've implemented a custom jasmine matcher to check if an element is focused. Here we need to resolve two promises before making an equality comparison. Currently, we are using protractor.promise.all()
and then()
:
protractor.promise.all([
elm.getId(),
browser.driver.switchTo().activeElement().getId()
]).then(function (values) {
jasmine.matchersUtil.equals(values[0], values[1]);
});
理想情况下,我们希望它处于更易读的状态:
which ideally we'd like to have in a more readable state:
protractor.promise.all([
elm.getId(),
browser.driver.switchTo().activeElement().getId()
]).spread(function (currentElementID, activeElementID) {
return jasmine.matchersUtil.equals(currentElementID, activeElementID);
})
使用起来可能有点难看,但是你可以定义一个独立的辅助函数,可以传递给 then()
作为参数并有一个回调,通常传递给 then()
传递给它.然后这个函数会将数组值转换为函数参数:
It may come a bit ugly to use, but you can define an independent helper function, which can be passed to then()
as a parameter and have a callback, which is usually passed to then()
to be passed to it. This function will then convert array value to function arguments:
protractor.promise.all([
elm.getId(),
browser.driver.switchTo().activeElement().getId()
]).then(spread(function (currentElementID, activeElementID) {
// ---^^^----- use helper function to spread args
jasmine.matchersUtil.equals(currentElementID, activeElementID);
}));
// helper function gets a callback
function spread(callback) {
// and returns a new function which will be used by `then()`
return function (array) {
// with a result of calling callback via apply to spread array values
return callback.apply(null, array);
};
}
您仍然可以将其与另一个 then()
链接并提供拒绝回调;它保持量角器承诺的所有行为相同,但只是将值数组转换为参数.
You can still chain it with another then()
and provide rejection callbacks; it keeps all the behavior of Protractor promises the same, but just converts array of values to arguments.
缺点是它在您的示例中看起来并不完美(不是 .all().spread()
而是 .all().then(spread())
) 并且您可能必须为此助手创建一个模块或全局定义它,以便能够在多个测试文件中轻松使用它.
Drawbacks are that it is does not have a perfect look like in your example (not .all().spread()
but .all().then(spread())
) and you'll probably have to create a module for this helper or define it globally to be able to use it easily in multiple test files.
更新:
在 ES2015 中,可以使用 解构赋值 与 then()
一起:
With ES2015 it is possible to use destructuring assignment along with then()
:
protractor.promise.all([
elm.getId(),
browser.driver.switchTo().activeElement().getId()
]).then(function (values) {
// Destructure values to separate variables
const [currentElementID, activeElementID] = values;
jasmine.matchersUtil.equals(currentElementID, activeElementID);
}));