Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
Cypress break outer foreach from an inside then

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
            }
        })
    }
})
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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
  }
})
about 4 years ago · Juan Pablo Isaza Report

0

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)
        }
      })
    })
  })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!