【发布时间】:2016-07-21 09:11:16
【问题描述】:
我有以下代码 sn-p。
this.clickButtonText = function (buttonText, attempts, defer) {
var me = this;
if (attempts == null) {
attempts = 3;
}
if (defer == null) {
defer = protractor.promise.defer();
}
browser.driver.findElements(by.tagName('button')).then(function (buttons) {
buttons.forEach(function (button) {
button.getText().then(
function (text) {
console.log('button_loop:' + text);
if (text == buttonText) {
defer.fulfill(button.click());
console.log('RESOLVED!');
return defer.promise;
}
},
function (err) {
console.log("ERROR::" + err);
if (attempts > 0) {
return me.clickButtonText(buttonText, attempts - 1, defer);
} else {
throw err;
}
}
);
});
});
return defer.promise;
};
我的代码有时会到达 'ERROR::StaleElementReferenceError: stale element reference: element is not attach to the page document' 行,所以我需要再次尝试并使用 “尝试 - 1” 参数。这是预期的行为。 但是一旦它到达 "RESOLVED!" 行,它就会不断迭代,所以我看到这样的东西:
button_loop:wrong_label_1
button_loop:CORRECT_LABEL
RESOLVED!
button_loop:wrong_label_2
button_loop:wrong_label_3
button_loop:wrong_label_4
问题是:如何在 console.log('RESOLVED!'); 行之后打破循环/承诺并从函数返回?
【问题讨论】:
-
你试过使用
.done回调吗? -
否,但似乎检查我的共享延迟是否已解决对我有用。将等待更优雅的答案,如果没有,将提供我自己的
标签: javascript promise protractor