[英]How return element of nested promise in protractor
我在量角器中创建了以下函数:
function getChildElementByText(parentElement, tagName, textToSearch)
{
return parentElement.all(by.tagName(tagName))
.then((items) => {
items.map( item => {
item.getText().then(text => {
if (text === textToSearch){
return item;
}
});
});
});
}
获取<select>
元素的<option>
元素的示例:
let myitem = selectorHelpers.getChildElementByText(clientIdSelect, 'option', 'ExampleText');
myitem.click();
这给了我以下错误:
Failed: myitem.click is not a function
我需要该函数返回与文本条件匹配的项目,然后单击该元素,如上例所示。
使用map
获取单个元素可能不是最好的方法。 map
根据每个项目的返回值返回一个新数组。
考虑使用for...or
甚至经典的for
循环来遍历每个项目,并使用async / await
等待每个item.getText()
解析,然后再继续下一个项目。 然后如果等待的结果等于textToSearch
返回item
对象。 否则在循环后返回null
。
function getChildElementByText(parentElement, tagName, textToSearch) {
return parentElement.all(by.tagName(tagName))
.then(async items => {
for (const item of items) {
const text = await item.getText();
if (text === textToSearch) {
return item;
}
}
return null;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.