2017-03-14 34 views
1

我正在嘗試從列表視圖中查找項目的索引。要找出我正在使用此功能的指數如何等待element.all得到解決?

function restFunction(appName) { 
    var indexForItem = 0; 
    var a = element.all(by.repeater("app in itemList").column("app.itemName")).each(function (element, index) { 
      element.getText().then(function (name) { 
       console.log("Name is " + name); 
       if (name == "sam") { 
        indexForItem = index + 1; 
        console.log("Ïndex is " + indexForItem); 
        a = index; 
       } 
      }); 
     }); 

     return a; 
    } 

在量角器中我該如何等待上述承諾才能得到解決。現在,當我打電話給我restFunction我總是得到承諾在待定狀態

回答

0

你的代碼看起來不正確,尤其是返回值。你正在返回函數調用 - element.all(locator).each(eachFunction)這將返回一個Promise,它不會解析任何東西。檢查文檔here

返回

!webdriver.promise.Promise當 功能已呼籲所有的ElementFinders將解決的承諾。承諾將 解析爲null。

雖然你重新分配後,調用內部的值,因爲模式是異步,它不會等到重新分配。

你必須改變它返回索引直接

function restFunction(appName) { 
    return element.all(by.repeater("app in itemList").column("app.itemName")).each(function (element, index) { 
     return element.getText().then(function (name) { 
      if (name === "sam") { 
       return (index+1); 
      } 
     }); 
    }); 
} 

當你調用該函數 - 你必須解決的承諾

restFunction('').then(function _resolvePromiseOfCall(value){ 
    console.log('value') 
})