量角器 - 功能失效时返回未决承诺

Protractor - Returning pending promise when functioned out

我正在尝试创建一个函数,该函数将梳理一组元素和 return 满足条件的元素的第一个实例。

这就是我在测试中所拥有的确实有效:

element.all(by.css('_cssSelector_')).filter(function(elms, index) {
        return elms.getAttribute('height').then(function(height) {
            return parseInt(height) > 0;
        });
    }).then(function(validElms) {
         browser.actions().mouseMove(validElms[0]).perform();
    }

...但是如果我解决这个问题,它 不起作用 :

getValidElm = function() {
    var validElm = element.all(by.css('_cssSelector_')).filter(function (elms, index) {
        return elms.getAttribute('height').then(function (height) {
            return parseInt(height) > 0;
        });
    }).then(function (validElms) {
        return validElms[0];
        });

    return validElm;
}

如果我那么运行:

var validElmFromPage = getValidElm();
console.log(validElmFromPage);

我得到:Promise::2046 {[[PromiseStatus]]:"pending"}

这表明在使用函数外部的 var 之前函数内部的某些内容未解决的问题。在(广泛地)阅读了这里的 posts 之后,甚至这篇精彩的博客 post (http://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/),我仍然无法弄清楚这是怎么回事。我知道这很简单,很可能与 controlFlow 相关?

感谢您的帮助。

让函数return一个承诺。由于 filter() return 是 ElementArrayFinder,您应该可以使用 first():

getValidElm = function() {
    return element.all(by.css('_cssSelector_')).filter(function (elms, index) {
        return elms.getAttribute('height').then(function (height) {
            return parseInt(height) > 0;
        });
    }).first();
}

first() 会 return 一个 ElementFinder 你可以传递给 mouseMove():

var elm = getValidElm();
browser.actions().mouseMove(elm).perform();