将 Protractor ElementFinder 传递给 deferred.fulfill() 会导致一个包含空值的承诺 [英] Passing Protractor ElementFinder to deferred.fulfill() results in a promise containing a null value
问题描述
我使用 Protractor elementfinder 作为参数调用 deferred.fulfill().在完成上放置断点时,我可以看到元素查找器solutionElement"不为空.承诺得到解决,我的then"回调被执行.但是回调中myElement"的值为null.
I am calling deferred.fulfill() with a Protractor elementfinder as a parameter. When placing a breakpoint on the fulfill I can see that the elementfinder "solutionElement" is not null. The promise is resolved and my "then" callback is executed. However the value of "myElement" in the callback is null.
如果我不将元素查找器传递给完成,而是使用其他值(即cnt"var),myElement"变量将解析为cnt"的实际值.
If I don't pass the elementfinder to the fulfill and instead use some other value(i.e. the "cnt" var) the "myElement" variable resolves to the actual value of "cnt".
我想知道这个问题是否与我的完成调用在 then 回调中这一事实有关,但我不确定.
I'm wondering if the issue is related to the fact that my fulfill call is within a then callback, but I don't understand for sure.
任何帮助/建议将不胜感激.谢谢你
Any help/advice would be appreciated. Thank You
it('Should select when clicked',function() {
sb.getSolutionElementIndexByName("test").then(function(myElement) {
myElement.click();
});
});
SbPageObject.prototype.getSolutionElementIndexByName = function(name){
var deferred = protractor.promise.defer();
var cnt = 0;
var notFulFilled = true;
var allSolutions = this.allSolutions;
allSolutions.count().then(function(solCnt){
allSolutions.each(function (solutionElement) {
solutionElement.element(by.className("sb-solution-name")).getText().
then(function (solutionText) {
if (solutionText === name) {
console.log("*****Fullfiled");
deferred.fulfill(solutionElement);
notFulFilled = false;
}
//if this is the last element and it's still not a match, reject promise
else if (cnt + 1 == solCnt){
deferred.reject(new Error ("Solution " + name + " was not found."));
}
cnt++;
});
});
});
return deferred.promise;
};
推荐答案
我通常使用map、filter、action/assert模式.它看起来像这样:
I usually use the map, filter, action/assert pattern. It looks something like this:
element.all(locator).map(function(elm, index) {
// Get the value you are interested in finding and return the element too.
return {
elm: elm,
text: elm.getText(),
index: index
};
}).then(function(list) {
// Find your text here. Otherwise fail.
for(var i = 0; i<list.length; i++) {
if(list[i].text === name) {
return list[i].elm;
}
}
throw new Error('Solution not found');
}).then(function(elm) {
// Perform an action on the element you found.
elm.click();
});
Map 将在结果传递给链中的下一个 then
之前完全解析所有的 Promise.
Map will fully resolve all the promises before the results are passed to the next then
in the chain.
这篇关于将 Protractor ElementFinder 传递给 deferred.fulfill() 会导致一个包含空值的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!