Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a test multiple times in Cypress.io

I have a test case to fix a bug that appears 1 in X times.

I'd like to run the same test multiple times but I can't find any documentation that explains how to restart the test automatically and stop when a threshold is hit.

Any insight is appreciated

like image 729
Webreality Avatar asked Sep 10 '18 20:09

Webreality


3 Answers

You can add the testing block inside a loop.

Cypress is bundled with a few libraries, lodash can do this using: Cypress._.times method:

Cypress._.times(10, () => {
  describe('Description', () => {
    it('runs 10 times', () => {
      //...
    });
  });
});
like image 136
Getulio Gasparotto Dadald Avatar answered Nov 13 '22 09:11

Getulio Gasparotto Dadald


Instead of putting the loop inside the test, you could put it in the outer space as following

var i = 0;
for (i = 0; i < 3 ; i++) {     
  describe('Verify "Login" is visible. Test: '+i, function() {
    it('finds the Login link in the header', function() {

      //Place code inside the loop that you want to repeat

    })
  })
}

The Result will be the following:

  • Verify "Login" is visible. Test: 0
  • finds the Login link in the header
  • Verify "Login" is visible. Test: 1
  • finds the Login link in the header
  • Verify "Login" is visible. Test: 2
  • finds the Login link in the header
like image 19
Sam Avatar answered Nov 13 '22 09:11

Sam


I completely spaced and forgot that these are normal JS files, so I wrapped the test in a for loop. This seems to work as I expected.

describe('Verify "Login" is visible', function() {
  it('finds the Login link in the header', function() {
    var i = 0;
    for (i = 0; i < 5 ; i++) { 
      //Place code inside the loop that you want to repeat
      cy.visit('https://www.example.com/page1')
      cy.get('.navbar').contains('Login').should('be.visible')
      cy.visit('https://www.example.com/page2')
      cy.get('.navbar').contains('Login').should('be.visible')
    }      
  })
})
like image 16
Webreality Avatar answered Nov 13 '22 09:11

Webreality