I am looking for an easy way to return an array of values from protractor's all.(by.repeater)
我正在寻找一种从量角器的全部返回值数组的简单方法。(by.repeater)
Basically, I just want an easy way to make an array of usernames
given a repeater like user in users
.
基本上,我只想要一个简单的方法来为用户提供像用户这样的转发器的用户名数组。
Right now I'm building it like this:
现在我正在构建它:
allUsers = element.all(by.repeater('user in users').column('user.username')).then(function(array){
var results = []
var elemLength = array.length
for(var n = 0; n < elemLength; n++){
array[n].getText().then(function(username){
results.push(username)
})
}
return results
});
expect(allUsers).toContain(newUser)
Is there a more concise, reusable way to do this built into protractor/jasmine that I just can't find?
有没有更简洁,可重复使用的方法来构建量角器/茉莉花,我找不到?
2 个解决方案
#1
11
AS alecxe said, use map to do this. This will return a deferred that will resolve with the values in an array, so if you have this:
AS alecxe说,使用map来做到这一点。这将返回一个将使用数组中的值解析的延迟,所以如果你有这个:
var mappedVals = element.all(by.repeater('user in users').column('user.username')).map(function (elm) {
return elm.getText();
});
It will resolve like this:
它会像这样解决:
mappedVals.then(function (textArr) {
// textArr will be an actual JS array of the text from each node in your repeater
});
#2
3
I've successfully used map()
for this before:
我以前成功地使用了map():
element.all(by.repeater('user in users').column('user.username')).map(function (elm) {
return elm.getText();
});
When I researched the topic I took the solution from:
当我研究这个主题时,我采用了以下解决方案:
- protractor - how to get the result of an array of promises into another array
量角器 - 如何将promises数组的结果转换为另一个数组
#1
11
AS alecxe said, use map to do this. This will return a deferred that will resolve with the values in an array, so if you have this:
AS alecxe说,使用map来做到这一点。这将返回一个将使用数组中的值解析的延迟,所以如果你有这个:
var mappedVals = element.all(by.repeater('user in users').column('user.username')).map(function (elm) {
return elm.getText();
});
It will resolve like this:
它会像这样解决:
mappedVals.then(function (textArr) {
// textArr will be an actual JS array of the text from each node in your repeater
});
#2
3
I've successfully used map()
for this before:
我以前成功地使用了map():
element.all(by.repeater('user in users').column('user.username')).map(function (elm) {
return elm.getText();
});
When I researched the topic I took the solution from:
当我研究这个主题时,我采用了以下解决方案:
- protractor - how to get the result of an array of promises into another array
量角器 - 如何将promises数组的结果转换为另一个数组