On web application, particular element is visible only after page reload and that is also available after some time, so currently I have implemented it as below:
it('element-check', () => {
cy.visit('url')
// performing certain actions
cy.wait(150000)
cy.reload()
cy.contains('text').click()
})
instead of fixed wait cy.wait(150000), I need to use the polling mechanism in such a way that after every 30 seconds the page is getting reloaded and check for the required element until the element is visible.
You can use a recursive function to achieve this.
it('element-check', () => {
cy.visit('url')
let retry = 0
function isElementVisible() {
if (retry < 5 && Cypress.$('selector').length == 0) {
//Increment retry
retry++
//wait 30 seconds
cy.wait(30000)
//Reload Page
cy.reload()
//Element is not yet visible, Call the recursive function again
cy.then(isElementVisible)
} else if (retry < 5 && Cypress.$('selector').length == 1) {
cy.get('selector').click()
return
} else {
//It excedded required no. of execution
return
}
}
//Trigger the recursive function
cy.then(isElementVisible)
})