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

312
Views
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 answers
Answer question

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 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!