Last active
December 22, 2020 17:00
-
-
Save jasdeepkhalsa/c11dcaf0341e4f3e0007b6208cefb326 to your computer and use it in GitHub Desktop.
Cypress waitForWindow recursive promise function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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