browser.findElements(protractor.By.repeater('cat in cats')).then(function(rows) {
for (i = 0; i < rows.length; i++) { //which is always 3
var tmp = element(by.repeater('cat in cats').row(i)).element(by.binding('cat.name')).getText();
tmp.then(function(text) {
console.log('text is : ' + text);
console.log('iteration number is: ' + i);
if (text == 'someText') {
element(by.repeater('cat in cats').row(i)).element(by.binding('cat.action')).click();
}
});
}
在这种情况下,函数内“i”的值总是返回 3。 我得到了文本,然后检查文本是否是我想要的,然后单击一个元素。
“if”语句中“i”的值总是返回 3。与 promises 有关,但我不确定。 非常感谢任何有关修改代码的帮助。
谢谢!
最佳答案
不要多次调用by.repeater()
;相反,使用 map()
并像这样“链接” promise :
element.all(By.repeater('cat in cats')).map(function(elm) {
return {
text: elm.element(by.binding('cat.name')).getText(),
action: elm.element(by.binding('cat.action'))
};
}).then(function(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i].text == 'someText') {
return arr[i].action;
}
}
throw new Error('Text not found');
}).then(function(elm) {
elm.click();
});
此处提供的解决方案的所有功劳归于@Andres:
另见:
关于javascript - 需要帮助在 Protractor 中使用 for 循环和 getText(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27516683/