Protractor 如何等待element.all得到解决?

Protractor 如何等待element.all得到解决?,protractor,Protractor,我正在尝试从列表视图中查找项的索引。为了找出索引,我使用了这个函数 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(func

我正在尝试从列表视图中查找项的索引。为了找出索引,我使用了这个函数

    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;
    }

在量角器中,我如何才能等待上述承诺得到解决。现在,当我调用rest函数时,我总是在挂起状态下得到承诺

您的代码看起来不正确,尤其是返回值。您正在返回函数调用-
元素.all(locator).each(eachFunction)
,它将返回一个不会解决任何问题的承诺。检查文档

返回

!!webdriver.promise.promise承诺当 函数已在所有ElementFinder上调用。承诺会实现 解析为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')
})

您的代码看起来不正确,尤其是返回值。您正在返回函数调用-
元素.all(locator).each(eachFunction)
,它将返回一个不会解决任何问题的承诺。检查文档

返回

!!webdriver.promise.promise承诺当 函数已在所有ElementFinder上调用。承诺会实现 解析为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')
})