我有以下代碼片段。打破循環/承諾,並從功能返回
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;
};
不時我的代碼達到「錯誤:: StaleElementReferenceError:陳舊的元素參考:元素沒有連接到頁面文件」線,所以我需要再次嘗試並調用我的函數「的嘗試 - 1「參數。這是預期的行爲。 但是一旦到達「已解決!」線它不斷迭代,所以我看到水木清華這樣的:
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從函數返回;(「解決了!」)一行?
您是否嘗試過使用'.done'回調? –
不,但似乎檢查我的共享延遲是否解決了我的作品。將等待更優雅的答案,如果不會提供我自己 –