Is it possible to break out of nested .each loops from an if-else statement?
I'd like to:
- Get the text from each element
- Check if it is included in an existing string
- As soon as a substring that isn't included in the original string is found, click on the element with said substring
- Exit the loop
The return false doesn't trigger -- right now the test fails because after the item is clicked on, the loop is still running and cannot find the next elements of each, since it's now on a new page. Would it be possible to fix this, or would it be best to use a different approach?
it("Test", () => {
cy.get('[data-testid="item"]').each(($el, index) => {
cy.wrap($el).children().eq(1).each(($el) => {
itemText = $el.text()
cy.log(itemText)
itemList = "list text here"
if (itemList.includes(itemText)) {
cy.log("Skip this item")
}
else {
cy.log("Proceed with this item")
cy.get('[data-testid="item"]').eq(index).click()
itemFound = true;
return false;
}
})
if (itemFound) return false;
});
});