簡體   English   中英

如何在量角器中返回嵌套承諾的元素

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM