'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
Solution 1:[1]
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
Solution 2:[2]
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')
}
})
})
Solution 3:[3]
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', () => {
//...
});
});
});
Solution 4:[4]
Here you go https://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
}
}
Solution 5:[5]
You can also put the loop outside the it or describe calls. This way the Cypress Test Runner UI will show you a pass or fail for each distinct iteration of the loop. In my experience this is the better way to do it since a failure will continue next iteration.
describe('Visit multiple pages', function() {
//array of values to try
['example.com/1','example.com/2','example.com/3']
// unique test run for each url
.forEach( (url)=>{
// can use parameters in test names
it(`we can visit ${url}`, function() {
cy.visit(url)
})
})
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sam |
Solution 2 | Webreality |
Solution 3 | |
Solution 4 | Idriss Mahjoubi |
Solution 5 | Eddie |