【问题标题】:Protractor - Returning pending promise when functioned out量角器 - 功能结束时返回待处理的承诺
【发布时间】:2015-09-19 03:05:59
【问题描述】:

我正在尝试创建一个函数,该函数将梳理一组元素并返回符合条件的元素的第一个实例。

这是我在测试中发现的确实有效

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 被使用之前,函数内部的某些问题没有解决。在(广泛地)阅读了这里的帖子,甚至这篇精彩的博客文章 (http://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/) 之后,我仍然无法弄清楚这笔交易是什么。我知道这很简单,很可能与 controlFlow 相关?

感谢您的帮助。

【问题讨论】:

    标签: javascript angularjs selenium-webdriver promise protractor


    【解决方案1】:

    让函数返回一个承诺。由于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() 将返回一个ElementFinder,您可以将其传递给mouseMove()

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-18
      • 2021-07-22
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      相关资源
      最近更新 更多