Search code examples
cypressfixtures

How to use fixture for cy.visit()


I have multiple links to test having the same test cases. I wanted to use fixture but it did not work. Can someone please help? I want to reiterate my tests for all links in the json file.

spec.js:

 cy.readFile('cypress/fixtures/trial.json').then((details) => {
                details.forEach((totest)=>{
                    cy.visit(totest.url)
                })
            })

fixture (.json):

"totest":[
    { 
      "url": "https:www.link1.com"
    },
    { 
      "url": "https:www.link2.com"}
      ,
    { 
      "url": "https:www.link3.com"
    }
     
    ]

Error


Solution

  • Your array is inside the totest property, so you need to add that

    cy.fixture('trial.json').then((details) => {
      details.totest.forEach((totest) => {
        cy.visit(totest.url)
      })
    })
    

    One test per URL

    import details from './cypress/fixtures/trial.json'
    //OR
    const details = require('./cypress/fixtures/trial.json')
    
    describe('tests URLs', () => {
    
      details.totest.forEach(totest => {
    
        it(`testing ${totest.url}`, () => {
          cy.visit(totest.url)
        })
      })
    })