2017-03-14 2 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)을 반환하면 아무 것도 해결되지 않는 약속이 반환됩니다. 설명서 here을 확인하십시오.

반환

는하세요 기능이 모든 ElementFinders 호출했을 때 해결할 약속을 webdriver.promise.Promise. 약속은 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') 
}) 
를 해결해야