0

I have used return false statement to break the each loop in cypress. But still the loop continue.. Please find the code snippet below:

    getRowActionByUrlOrDomain(Value) {
    var urlName;
    var flag;
    cy.get(TBL_BWLIST_ROWS).each(($li, rowindex, $lis) => {
        cy.wrap($li).find('td a', { timeout: 9000 }).each(($lidata, index, lis) => {
            urlName = $lidata.text().trim()
            rowindex = $lis.index($li)
            if (urlName.localeCompare(Value.trim()) == 0) {
                expect($lidata.text().trim()).to.be.equal(Value.trim())
                cy.get(TBL_BWLIST_ROWS + ":nth-child(index)").find('td:nth-of-type(index) a').click()
                return false;
            }
        })
        
    })

}
Amu
  • 69
  • 2
  • 9

1 Answers1

0

You have two each() loops. I have added an abort variable to allow the outer loop to be exited early as well

getRowActionByUrlOrDomain(Value) {
var urlName;
var abort = false;
cy.get(TBL_BWLIST_ROWS).each(($li, rowindex, $lis) => {
    cy.wrap($li).find('td a', { timeout: 9000 }).each(($lidata, index, lis) => {
        urlName = $lidata.text().trim()
        rowindex = $lis.index($li)
        if (urlName.localeCompare(Value.trim()) == 0) {
            expect($lidata.text().trim()).to.be.equal(Value.trim())
            cy.get(TBL_BWLIST_ROWS + ":nth-child(index)").find('td:nth-of-type(index) a').click()
            abort = true // Set this to allow the 2nd loop to be aborted as well 
            return false;
        }
    })
    if (abort) return false // This breaks out of the outer loop
})

}
Mikkel
  • 7,693
  • 3
  • 17
  • 31