2017-02-08 20 views
1

我願做這樣的事情,但它不工作,因爲isPresent返回一個承諾,而不是一個布爾...如何在JS中使用量角器創建isVisible函數?

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

有沒有一種辦法處理這個(事情?也許全部使用,然後將其與單個布爾諾言或什麼?

回答

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

酷那隻會返回一個帶有單個布爾權的承諾 – Jackie

+1

@Ja ckie yup,我還添加了一些更可讀的版本,請檢查一下。謝謝。 – alecxe

相關問題