2017-03-14 33 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') 
})