简体   繁体   English

量角器 - 在运行时返回未决的承诺

[英]Protractor - Returning pending promise when functioned out

I am trying to create a function that will comb through an array of elements and return the first instance of one that meets the criteria.我正在尝试创建一个函数,该函数将梳理元素数组并返回满足条件的元素的第一个实例。

This is what I have inside my test that does work :这就是我在测试中所做的工作

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

...but if I function this out, it does NOT work : ...但如果我把它解决了,它就行不通了

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

If I then run:如果我然后运行:

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

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

Which points to an issue of something inside the function not resolving before the var outside the function is being used.这表明函数内部的某个问题在使用函数外部的 var 之前没有解决。 After reading (extensively) through posts here, and even this wonderful blog post ( http://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/ ), I still can't figure out what the deal is.在阅读(广泛)通过这里的帖子,甚至这篇精彩的博客文章( http://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/ )后,我仍然无法弄清楚是什么交易是。 I know it's something simple, most likely controlFlow related?我知道这很简单,很可能与 controlFlow 相关?

Thanks for your help.谢谢你的帮助。

Let the function return a promise.让函数返回一个承诺。 Since filter() returns an ElementArrayFinder , you should be able to use first() :由于filter()返回一个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() would return an ElementFinder which you can pass to mouseMove() : first()将返回一个ElementFinder ,您可以将其传递给mouseMove()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM