Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda