简体   繁体   中英

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

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', () => {
      //...
    });
  });
});

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')
    }      
  })
})

You can also put the loop above the describe/it pair. This way the Cypress Test Runner UI will show you a pass or fail for each distinct instance of the loop. In my experience this is the better way to do it.

Here you gohttps://docs.cypress.io/guides/guides/test-retries#How-It-Works .

You can configure this in your configuration file (cypress.json by default) by passing the retries option an object with the following options:

runMode allows you to define the number of test retries when running cypress run openMode allows you to define the number of test retries when running cypress open

{
  "retries": {
    // Configure retry attempts for `cypress run`
    // Default is 0
    "runMode": 2,
    // Configure retry attempts for `cypress open`
    // Default is 0
    "openMode": 0
  }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM