Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -2801,22 +2801,37 @@ class Playwright extends Helper {
// We apply 2 strategies here: wait for text as innert text on page (wide strategy) - older
// or we use native Playwright matcher to wait for text in element (narrow strategy) - newer
// If a user waits for text on a page they are mostly expect it to be there, so wide strategy can be helpful even PW strategy is available
return Promise.race([

// Use a flag to stop retries when race resolves
let shouldStop = false
let timeoutId

const racePromise = Promise.race([
new Promise((_, reject) => {
setTimeout(() => reject(errorMessage), waitTimeout)
timeoutId = setTimeout(() => reject(errorMessage), waitTimeout)
}),
this.page.waitForFunction(text => document.body && document.body.innerText.indexOf(text) > -1, text, { timeout: timeoutGap }),
promiseRetry(
async retry => {
async (retry, number) => {
// Stop retrying if race has resolved
if (shouldStop) {
throw new Error('Operation cancelled')
}
const textPresent = await contextObject
.locator(`:has-text(${JSON.stringify(text)})`)
.first()
.isVisible()
if (!textPresent) retry(errorMessage)
},
{ retries: 1000, minTimeout: 500, maxTimeout: 500, factor: 1 },
{ retries: 10, minTimeout: 100, maxTimeout: 500, factor: 1.5 },
),
])

// Clean up when race resolves/rejects
return racePromise.finally(() => {
if (timeoutId) clearTimeout(timeoutId)
shouldStop = true
})
}

/**
Expand Down
Loading