Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Last active December 22, 2020 17:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/c11dcaf0341e4f3e0007b6208cefb326 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/c11dcaf0341e4f3e0007b6208cefb326 to your computer and use it in GitHub Desktop.
Cypress waitForWindow recursive promise function
export default function waitForWindow(property, timeout = 10000, interval = 500, attempts = 0) {
if (!property) {
return Promise.reject(new Error('No window property added to the function'))
}
return cy.window()
.then(
(win) => {
if (win[property] !== undefined) {
return true
}
return false
})
.then((result) => {
if (result) {
return Promise.resolve(true)
}
cy.wait(interval)
if (attempts <= timeout / interval) {
attempts++
return waitForWindow(property, timeout, interval, attempts)
} else {
return Promise.reject(new Error(`window.${property} did not load within ${timeout/1000} seconds`))
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment