我使用Protractor elementfinder作为参数调用deferred.fulfill()。当在履行中放置断点时,我可以看到元素找到者" solutionElement"不是空的。承诺得到解决,我的"然后"回调被执行。然而," myElement"的价值在回调中为空。
如果我没有将元素查找器传递给履行,而是使用其他一些值(即" cnt" var)" myElement"变量解析为" cnt"。
的实际值我想知道这个问题是否与我的履行电话在当时的回调中有关,但我不确定。
任何帮助/建议将不胜感激。谢谢
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;
};
答案 0 :(得分:4)
我通常使用map,filter,action / assert模式。它看起来像这样:
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
之前完全解析所有承诺。