Skip to content

Commit dab66e9

Browse files
Deal with stalereferences caused by timing issues
looking up objects in the DOM
1 parent 49671b4 commit dab66e9

File tree

1 file changed

+55
-28
lines changed

1 file changed

+55
-28
lines changed

selenium/test/pageobjects/BasePage.js

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -185,42 +185,69 @@ module.exports = class BasePage {
185185
}
186186

187187
async waitForLocated (locator) {
188-
try {
189-
return this.driver.wait(until.elementLocated(locator), this.timeout,
190-
'Timed out after [timeout=' + this.timeout + ';polling=' + this.polling + '] seconds locating ' + locator,
191-
this.polling)
192-
}catch(error) {
193-
if (!error.name.includes("NoSuchSessionError")) {
194-
console.error("Failed waitForLocated " + locator + " due to " + error)
195-
}
196-
throw error
197-
}
188+
let attempts = 3
189+
let retry = false
190+
let rethrowError = null
191+
do {
192+
try {
193+
return this.driver.wait(until.elementLocated(locator), this.timeout,
194+
'Timed out after [timeout=' + this.timeout + ';polling=' + this.polling + '] seconds locating ' + locator,
195+
this.polling)
196+
}catch(error) {
197+
if (error.name.includes("StaleElementReferenceError")) {
198+
retry = true
199+
}else if (!error.name.includes("NoSuchSessionError")) {
200+
console.error("Failed waitForLocated " + locator + " due to " + error)
201+
retry = false
202+
}
203+
rethrowError = error
204+
}
205+
} while (retry && --attempts > 0)
206+
throw rethrowError
198207
}
199208

200209
async waitForVisible (element) {
201-
try {
202-
return this.driver.wait(until.elementIsVisible(element), this.timeout,
203-
'Timed out after [timeout=' + this.timeout + ';polling=' + this.polling + '] awaiting till visible ' + element,
204-
this.polling)
205-
}catch(error) {
206-
if (!error.name.includes("NoSuchSessionError")) {
207-
console.error("Failed to find visible element " + element + " due to " + error)
210+
let attempts = 3
211+
let retry = false
212+
let rethrowError = null
213+
do {
214+
try {
215+
return this.driver.wait(until.elementIsVisible(element), this.timeout,
216+
'Timed out after [timeout=' + this.timeout + ';polling=' + this.polling + '] awaiting till visible ' + element,
217+
this.polling)
218+
}catch(error) {
219+
if (error.name.includes("StaleElementReferenceError")) {
220+
retry = true
221+
}else if (!error.name.includes("NoSuchSessionError")) {
222+
console.error("Failed to find visible element " + element + " due to " + error)
223+
retry = false
224+
}
225+
rethrowError = error
208226
}
209-
throw error
210-
}
227+
} while (retry && --attempts > 0)
228+
throw rethrowError
211229
}
212230

213231

214232
async waitForDisplayed (locator) {
215-
if (this.interactionDelay && this.interactionDelay > 0) await this.driver.sleep(this.interactionDelay)
216-
try {
217-
return this.waitForVisible(await this.waitForLocated(locator))
218-
}catch(error) {
219-
if (!error.name.includes("NoSuchSessionError")) {
220-
console.error("Failed to waitForDisplayed " + locator + " due to " + error)
221-
}
222-
throw error
223-
}
233+
let attempts = 3
234+
let retry = false
235+
let rethrowError = null
236+
do {
237+
if (this.interactionDelay && this.interactionDelay > 0) await this.driver.sleep(this.interactionDelay)
238+
try {
239+
return this.waitForVisible(await this.waitForLocated(locator))
240+
}catch(error) {
241+
if (error.name.includes("StaleElementReferenceError")) {
242+
retry = true
243+
}else if (!error.name.includes("NoSuchSessionError")) {
244+
retry = false
245+
console.error("Failed to waitForDisplayed " + locator + " due to " + error)
246+
}
247+
rethrowError = error
248+
}
249+
} while (retry && --attempts > 0 )
250+
throw rethrowError
224251
}
225252

226253
async getText (locator) {

0 commit comments

Comments
 (0)