cy.get('body').then(($body) => {
if ($body.text().includes('Withdrawn')) {
cy.contains('Withdrawn')
.click({force:true})
} else if ($body.text().includes('More')) {
cy.contains('More')
.click({force:true})
cy.wait(10000)
cy.contains('Withdrawn')
.click({force:true})
})
Hello, I am new the cypress and I wish to repeat the if Statement again until the element 'Withdrawn' is found in the body by clicking on the more button. Can anyone let me know what's the best way about executing this?
Not sure if this is precisely the correct code, but the idea is to recursively call the code you have for a single iteration.
const clickUntilWithdrawn = ($parent, attempts = 0) => {
if (attempts > 10) throw 'Failed to find'
if ($parent.text().includes('Withdrawn')) {
cy.contains('Withdrawn').click()
return
}
cy.contains('More').should('be.visible').click()
cy.wait(10000) // probably can be shorter wait with visibility check above
clickUntilWithdrawn($parent, ++attempts)
}
cy.get('parent-element-of-withdrawn')
.then($el => clickUntilWithdrawn($el))