在量角器US preading承诺 [英] Spreading promises in Protractor

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

问题描述

有这个实用的功能

库来解决和s $ P $垫多个承诺为独立的参数:


  

如果你有一个数组的承诺,你可以使用s $ P $垫作为
  更换即可。在S $ P $垫功能S $ P $垫的价值观在
  履行处理程序的参数。


 返回getUsername()
    。然后(功能(用户名){
        返回[用户名,的getUser(用户名)];
    })
    .S $ P $垫(功能(用户名,用户){    });

在量角器,我们正在尝试使用内置的<一个href=\"http://selenium.google$c$c.com/git/docs/api/javascript/namespace_webdriver_promise.html\"><$c$c>protractor.promise WebDriverJS 来了。

问题:

是否有可能与 protractor.promise

的s $ P $垫功能

示例使用情况:

我们已经实现了自定义茉莉匹配到检查元素是否集中 。在这里,我们需要做的是等于比较之前分辨两个承诺。目前,我们正在使用 protractor.promise.all()则()

  protractor.promise.all([
    elm.getId(),
    browser.driver.switchTo()。activeElement()的getId()
])。然后(功能(值){
    jasmine.matchersUtil.equals(值[0],值[1]);
});

我们希望有一个更可读的状态,这理想的:

  protractor.promise.all([
    elm.getId(),
    browser.driver.switchTo()。activeElement()的getId()
]):S $ P $垫(功能(currentElementID,activeElementID){
    返回jasmine.matchersUtil.equals(currentElementID,activeElementID);
})


解决方案

这可能会有点丑使用,但您可以定义一个独立的辅助功能,它可以传递给则()作为参数,并有一个回调,这通常是传递给则()要传递给它。然后,此功能将转换成数组值函数参数:

  protractor.promise.all([
    elm.getId(),
    browser.driver.switchTo()。activeElement()的getId()
])。然后(S $ P $垫(功能(currentElementID,activeElementID){
    // --- ^^^ -----使用辅助函数为s $ P $垫ARGS
    jasmine.matchersUtil.equals(currentElementID,activeElementID);
}));
//辅助函数得到一个回调
函数s $ P $垫(回调){
    //并返回将由`使用那么新功能()`
    复位功能(阵列){
        //调用与通过回调申请为s $ P $垫数组值的结果
        返回callback.apply(NULL,数组);
    };
}

您仍可以链与另一个则()并提供拒绝回调;它使量角器的所有行为的承诺一样,只是转换值的数组参数。

缺点是,它是没有一个完美的外观就像在你的榜样(未。所有():S $ P $垫(),但。所有()。然后(S $ p $垫())),你可能会需要创建这个助手的模块或定义它在全球范围内能够在轻松地使用它多个测试文件。

q library has this neat feature to resolve and spread multiple promises into separate arguments:

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) {

    });

In protractor, we are trying to use the built-in protractor.promise coming from WebDriverJS.

The Question:

Is it possible to have the "spread" functionality with protractor.promise?

Example use case:

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);
})

解决方案

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);
    };
}

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.

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.

这篇关于在量角器US preading承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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