Search code examples
testingautomated-testscypresse2e-testing

Cypress : Run same test on 2 different url (to avoid copy paste same code)


I would like to know if there is a way to rerun the same tests twice (second time with different url) to avoid copy paste of the exact same code should look like something like this :

describe('...', () => {
   before(() => cy.visit('/))

   describe('Series of tests ...', () => {
      // Tests goes here
   })

   after(() => {
     cy.visit('other url')
     cy.rerun() // Re run the describe right above with the new url is there a way to do something like this ?
   })
})

Solution

  • You could use Cypress Lodash to iterate a certain number of times.

    const urls = ['url1', 'url2'];
    
    describe('...', () => {
      Cypress._.times(urls.length, ((index) => {
        it('Runs the test', () => {
          cy.visit(url[index]);
          // rest of test
        });
      });
    });