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

306
Vistas
How to use forEach with Cypress to loop through and condense the current code block

so I'm new to cypress and I'm looking to create a loop with forEach but don't know how to apply it. I'm looking to make this current codeblock DRY. Does anyone have any advice on how I would do this? It needs to iterate through each day of the week and change between "first" and "last" each time. I have provided the code that I'm trying to condense. Hope this is enough information - this my first time positing on here so any advice would be greatly appreciated!

cy.get('#button').click();
  cy.get('#income-button').click();
  cy.contains('Starts on').click();
  cy.contains('First').click({force: true});
  cy.wait(50);
  cy.contains('Monday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('Last').click({force: true});
  cy.wait(50);
  cy.contains('Tuesday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('First').click({force: true});
  cy.wait(50);
  cy.contains('Wednesday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('Last').click({force: true});
  cy.wait(50);
  cy.contains('Thursday').click({force: true});
  cy.contains('Starts on').click();
  cy.contains('First').click({force: true});
  cy.wait(500);
  cy.contains('Friday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('Last').click({force: true});
  cy.wait(500);
  cy.contains('Saturday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('First').click({force: true});
  cy.wait(500);
  cy.contains('Sunday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('Last').click({force: true});
  cy.wait(500);
  cy.contains('Monday').click({force: true});
  cy.contains('Starts on').click();
  cy.contains('First').click({force: true});
  cy.wait(500);
  cy.contains('Tuesday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('Last').click({force: true});
  cy.wait(500);
  cy.contains('Wednesday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('First').click({force: true});
  cy.wait(500);
  cy.contains('Thursday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('Last').click({force: true});
  cy.wait(500);
  cy.contains('Friday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('First').click({force: true});
  cy.wait(500);
  cy.contains('Saturday').click({force: true});
  cy.contains('Starts on').click({force: true});
  cy.contains('Last').click({force: true});
  cy.wait(500);
  cy.contains('Sunday').click({force: true});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can create an Array of Strings containing all the days and then add a For Loop, something like this:

var days = [
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday',
  'Saturday',
  'Sunday',
]

for (var index in days) {
  cy.contains(days[index]).click({force: true})
  cy.contains('Starts on').click({force: true})
  cy.contains('Last').click({force: true})
  cy.wait(50)
}
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