[英]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.