我正在尝试创建一个函数,该函数将梳理元素数组,并返回符合条件的元素的第一个实例。
这就是我在测试中所做的确实有效:
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);
我得到:承诺::2046{[[PromiseStatus]]:"待定"}
这表明函数内部的某个问题在使用函数外部的var之前没有得到解决。在(广泛)阅读了这里的帖子,甚至这篇精彩的博客文章之后(http://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/),我仍然不知道交易是什么。我知道这是一件简单的事情,很可能与controlFlow有关?
谢谢你的帮助。
让函数返回一个promise。由于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();