I want to break my outermost each loop from inside a then loop. I tried return false but it is not working. Following is my code snippet:
cy.get('abc').each(($elm) => {
if ($elm.text() == 'xyz') {
cy.get('pqr').click().then(() => {
if (cy.get('dgf').text() == "sdasd") {
expect(value1).to.be.equal(value2)
return false // I want to stop the outermost for each loop
//But even with return false it does not breaks the forEach
}
})
}
})
CodePudding user response:
Instead of using if (cy.get('dgf').text() == "sdasd")
use if ($('selector').text() == 'sdasd')
cy.get('abc').each(($elm) => {
if ($elm.text() == 'xyz') {
$('pqr').click()
if ($('dgf').text() == 'sdasd') {
expect(value1).to.be.equal(value2)
}
return false
}
})
CodePudding user response:
Try using a flag, I saw somewhere that breacking out of .each()
needs an unconditional return
value (i.e not inside the if()
)
cy.get('abc').each(($elm) => {
let continue = true;
if ($elm.text() == 'xyz') {
cy.get('pqr').click()
cy.get('dgf') // NO cy.get() in if()
.invoke('text')
.then(dgfText => {
if (dgfText === "sdasd") {
expect(value1).to.be.equal(value2)
continue = false
}
})
})
}
return continue; // true will keep going, false will break out
})
I suspect that's not going to do it tho. Maybe a restructure to eliminate the .each()
cy.get('abc')
.filter((index, el) => el.innerText === 'sdasd') // instead of each() and if()
.then(() => {
cy.get('pqr').click()
cy.get('dgf') // NO cy.get() in if()
.invoke('text')
.then(dgfText => {
if (dgfText === "sdasd") {
expect(value1).to.be.equal(value2)
}
})
})
})