如何使用Protractor在JS中创建isVisible函数?

时间:2017-02-08 19:09:50

标签: protractor

我想做类似的事情,但它不起作用,因为isPresent返回一个promise而不是一个布尔值......

this.isVisible = function(){
  return browser.isElementPresent(_this.username) &&
         browser.isElementPresent(_this.password) &&
         browser.isElementPresent(_this.submit)
}

我也试过

this.isVisible = function(){
  return _this.username.isPresent() &&
         _this.password.isPresent() &&
         _this.submit.isPresent()
}

有办法解决这个问题吗?也许使用all然后将它与一个布尔承诺或其他东西结合起来?

1 个答案:

答案 0 :(得分:1)

您可以使用protractor.promise.all()

this.isVisible = function() {
    return protractor.promise.all([
        browser.isElementPresent(_this.username),
        browser.isElementPresent(_this.password),
        browser.isElementPresent(_this.submit)
    ]).then(function (isPresent) {
        return isPresent[0] && isPresent[1] && isPresent[2];
    });
}

并且,如果您要添加helper spread() function

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

这会使事情变得更加明确:

this.isVisible = function() {
    return protractor.promise.all([
        browser.isElementPresent(_this.username),
        browser.isElementPresent(_this.password),
        browser.isElementPresent(_this.submit)
    ]).then(spread(function (isUsernamePresent, isPasswordPresent, isSubmitPresent) {
        return isUsernamePresent && isPasswordPresent && isSubmitPresent;
    }));
}
相关问题