需要帮助使用量角器中环和gettext的() [英] need help using for loop and getText() in protractor
本文介绍了需要帮助使用量角器中环和gettext的()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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();
}
});
}
在这种情况下,'我'函数内部的值总是返回3。
我得到的文本,然后检查文字是我想要的东西,并点击一个元素。
In this case the value of 'i' inside the function is always returning 3. I have get text and then check if the text is what I want and click on an element.
我中的值'如果'的语句总是返回3.事做承诺,但我不知道。
改性code任何帮助是非常AP preciated。
The value of 'i' in the 'if' statement is always returned as 3. Something to do with promises, but I am not sure. Any help with modified code is much appreciated.
谢谢!
推荐答案
不要叫 by.repeater()
多次;相反,使用 地图()
和产业链的承诺是这样的:
Don't call by.repeater()
multiple times; instead, use map()
and "chain" the promises like this:
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:
All of the credits go to @Andres for the solution provided here:
- Passing Protractor ElementFinder to deferred.fulfill() results in a promise containing a null value
另请参见:
- Protractor find element inside a repeater
这篇关于需要帮助使用量角器中环和gettext的()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文