Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress while loop [duplicate]

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?

like image 761
R.S Mohan Aravind Avatar asked Nov 15 '22 02:11

R.S Mohan Aravind


2 Answers

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()
})
like image 118
Eddie Avatar answered Dec 06 '22 18:12

Eddie


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()
})
like image 35
jjhelguero Avatar answered Dec 06 '22 19:12

jjhelguero