Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

85
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});
7 months 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)
}
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs