Cypress IO- Writing a For Loop

I have 15 buttons on a page. I need to test each button.

I tried a simple for loop, like

for (var i = 1; i < 15; i++) {

   cy.get("[=buttonid=" + i + "]").click()

But Cypress didn’t like this. How would I write for loops in Cypress?

To force an arbitrary loop, I create an array with the indices I want, and then call cy.wrap

var genArr = Array.from({length:250},(v,k)=>k+1)
cy.wrap(genArr).each((index) => {
    cy.get("#button-" + index).click()

Lodash is bundled with Cypress and methods are used with Cypress._ prefix.

For this instance, you’ll be using the _.times. So your code will look something like this:

Cypress._.times(15, (k) => {
   cy.get("[=buttonid=" + k + "]").click()

You can achieve something similar to a “for loop” by using recursion.

I just posted a solution here: How to use a while loop in cypress? The control of is NOT entering the loop when running this spec file? The way I am polling the task is correct?

Add this to your custom commands:

Cypress.Commands.add('recursionLoop', {times: 'optional'}, function (fn, times) {
  if (typeof times === 'undefined') {
    times = 0;

  cy.then(() => {
    const result = fn(++times);
    if (result !== false) {
      cy.recursionLoop(fn, times);

Then you can use it by creating a function that returns false when you want to stop iterating.

cy.recursionLoop(times => {
  console.log(`Iteration: ${times}`);
  console.log('Here goes your code.');
  return times < 5;

While cy.wrap().each() will work (one of the answers given for this question), I wanted to give an alternate way that worked for me. cy.wrap().each() will work, but regular while/for loops will not work with cypress because of the async nature of cypress. Cypress doesn’t wait for everything to complete in the loop before starting the loop again. You can however do recursive functions instead and that waits for everything to complete before it hits the method/function again.

Read More:   codemirror autocomplete after any keyup?

Here is a simple example to explain this. You could check to see if a button is visible, if it is visible you click it, then check again to see if it is still visible, and if it is visible you click it again, but if it isn’t visible it won’t click it. This will repeat, the button will continue to be clicked until the button is no longer visible. Basically the method/function is called over and over until the conditional is no longer met, which accomplishes the same thing as a for/while loop, but actually works with cypress.

clickVisibleButton = () => {
        cy.get( 'body' ).then( $mainContainer => {
            const isVisible = $mainContainer.find( '#idOfElement' ).is( ':visible' );
            if ( isVisible ) {
                cy.get( '#idOfElement' ).click();
        } );

Then obviously call the this.clickVisibleButton() in your test. I’m using typescript and this method is setup in a class, but you could do this as a regular function as well.

// waits 2 seconds for each attempt

refreshQuote(attempts) {
    let arry = []
    for (let i = 0; i < attempts; i++) { arry.push(i) }
    cy.wrap(arry).each(() => {
        cy.get('.quote-wrapper').then(function($quoteBlock) {
            if($quoteBlock.text().includes('Here is your quote')) {
            else {

Try template literals using backticks:

for(let i = 0; i < 3; i++){
    cy.get(`ul li:nth-child(`${i}`)).click();

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts