Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
Cypress foreach loop stop condition

I need some help with the following loop:

 localStorage.removeItem('DDFound');

 cy.get(sel).each(($el, index, $list) => {
    
    if(localStorage.getItem('DDFound')!='1')
    {      
    cy.log(localStorage.getItem('DDFound'));
    cy.wrap($el).click().then(()=> {
      cy.get(table).then(elx => {
            if(elx.find(tableitem).length > 0) {
                cy.get(tableitem).click();
                cy.get(lable).should('contain.text',"Item")
                localStorage.setItem('DDFound','1');                  
            }
            
        })
    });
    }
});

I would like to break just after finding the right item(tableitem) and for that, I'm setting a localstorage (didn't find any other way) but it looks like cypress runs all items in each loop in parallel and not getting to the if(localStorage.getItem('DDFound')!='1') after each element.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you have complex code inside the .each() you should wrap it in a promise

let found = false;

cy.get(sel).each(($el, index, $list) => {
    
  if (found) return false;   // early exit

  return new Cypress.Promise(resolve => {

    cy.wrap($el).click().then(() => {
      cy.get(table).then(elx => {
        if (elx.find(tableitem).length > 0) {
          cy.get(tableitem).click();
          cy.get(label).should('contain.text',"Item")
          found = true;
        }
        resolve()      // wait for above to complete
      })
    })

  })
})

See .each() - Promises

Promises are awaited

If your callback function returns a Promise, it will be awaited before iterating over the next element in the collection.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can stop the .each() loop early by returning false in the callback function. In your case, we can just return the value if DDItem does not equal one.

 localStorage.removeItem('DDFound');

 cy.get(sel).each(($el, index, $list) => {
    
    if(localStorage.getItem('DDFound')!='1')
    {      
    cy.log(localStorage.getItem('DDFound'));
    cy.wrap($el).click().then(()=> {
      cy.get(table).then(elx => {
            if(elx.find(tableitem).length > 0) {
                cy.get(tableitem).click();
                cy.get(lable).should('contain.text',"Item")
                localStorage.setItem('DDFound','1');                  
            }
            
        })
    });
    }

    return localStorage.getItem('DDFound') !== 1
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda