urlcypresse2e-testing

Increase URL cypress test


one question I have a URL xx/0/xy and in my test I have to check the duration of an element (this part work) the issue ist is from the URL xx/0/xy to xx/2000/xy how can I automate this, without having to write them separately.? at the moment i write this way but consume so much time, exist a better way to write this test?

it("0", () =>{

        cy.visit('/xx/0/xy')

        cy.checkelement()

    })

it("1", () =>{

        cy.visit('/xx/1/xy')

        cy.checkelement()

    })

it("2", () =>{

        cy.visit('/xx/2/xy')

        cy.checkelement()

    })

.... Repeat cy.visit('/xx/1000/xy') ....

Thanks a lot this work but it is a lot of work and i hope exist a better way to do this

it("0", () =>{

        cy.visit('/xx/0/xy')

        cy.checkelement()

    })

it("1", () =>{

        cy.visit('/xx/1/xy')

        cy.checkelement()

    })

it("2", () =>{

        cy.visit('/xx/2/xy')

        cy.checkelement()

    })

Solution

  • Probably this will provide the result you want, using string templates to insert the various values into the test code.

    const urlValues = ['0','1','2']
    
    urlValues.forEach(url => {
    
      it(`testing ${url}`, () => {
        cy.visit(`/xx/${url}/xy`)
        ...
    

    Or with strictly numeric urlValues using lodash to generate the array

    Cypress._.times(2000, (url => {
    
      it(`testing ${url}`, () => {
        cy.visit(`/xx/${url}/xy`)
        ...