Boris Grunwald
Boris Grunwald

Reputation: 2712

How to repeat a test in Cypress multiple times?

I have the following section of code in one of my Cypress tests:

cy.window()
  .its('store')
  .invoke('getState')
  .then((state) => {
    expect(state.app.gameStarted).to.equal(true)
    expect(state.app.noteButtonValues).to.have.lengthOf(4);
    expect(state.app.noteButtonValues).to.include(state.app.correctAnswer)
    cy.get("button").contains(state.app.correctAnswer).click()

    cy.window()
    .its('store')
    .invoke('getState')
    .then((state) => {  
        expect(state.app.correctAnswered).to.equal(1)
        expect(state.app.totalAnswered).to.equal(1)
    })   
  })
})

I'm testing that when a user clicks on a button, the redux state is updated as well. Now suppose I want to repeat this section for 100 clicks or so. How would that be possible?

It doesn't work wrapping it in a for loop like:

for (let i = 0; i < 100; i = i++) {
    cy.window()
    .its("store")
    .invoke("getState")
    .then((state) => {
        expect(state.app.gameStarted).to.equal(true);
        expect(state.app.noteButtonValues).to.have.lengthOf(4);
        expect(state.app.noteButtonValues).to.include(state.app.correctAnswer);
        cy.get("button").contains(state.app.correctAnswer).click();

        cy.window()
        .its("store")
        .invoke("getState")
        .then((state) => {
            expect(state.app.correctAnswered).to.equal(1);
            expect(state.app.totalAnswered).to.equal(1);
        });
    });
}

Upvotes: 3

Views: 2151

Answers (1)

Fiodorov Andrei
Fiodorov Andrei

Reputation: 2018

Use lodash method Cypress._.times()

Example:

Cypress._.times(100, (k) => {
  it(`typing hello ${k + 1} / 100`, () => {
    cy.log(k)
  })
})

Upvotes: 4

Related Questions