Home > database >  Cypress: How to create loop for fixture file with multiple arr
Cypress: How to create loop for fixture file with multiple arr

Time:11-26

Here is image from page

Hi all, I'm new here. How to create loop for fixture file with multiple arr in Cypress to check all the list?

[
  {
    "firstName": "John",
    "email": "[email protected]",
    "due": "50$",
    "webSite": "www"
  },
  {
    "firstName": "Frank",
    "email": "[email protected]",
    "due": "51$",
    "webSite": "www"
  },
  {
    "firstName": "Jason",
    "email": "[email protected]",
    "due": "100$",
    "webSite": "www"
  }
]
cy.fixture('folderName/fileName.json').then(function (testdata) {
            this.testdata = testdata
    })

    it('DDT fixture file', function () {
        cy.get('name_selector').should('have.text', this.testdata.firstName);
        cy.get('email_selector').should('have.text', this.testdata.email);
        cy.get('due_selector').should('have.text', this.testdata.due);
        cy.get('website_selector').should('have.text', this.testdata.website);
    })

CodePudding user response:

Cypress comes bundled with lodash you can use _.forEach()

const testData = require('cypress/fixture/folder/path')

Cypress._.forEach(testData, (data) => {
  it(`${data} test`, () => {
    cy.get('name_selector').should('have.text', data.firstName)
        cy.get('email_selector').should('have.text', data.email)
        cy.get('due_selector').should('have.text', data.due)
        cy.get('website_selector').should('have.text', data.website)
  }
})

CodePudding user response:

Simple approach would be Array.forEach()

cy.fixture('folderName/fileName.json').as('testdata')  // alias sets "this.testdata"

describe('All testdata' function() {

  this.testdata.forEach(item => {

    it('DDT fixture file for '   item.firstName, () => {
      cy.get('name_selector').should('have.text', item.firstName);
      cy.get('email_selector').should('have.text', item.email);
      cy.get('due_selector').should('have.text', item.due);
      cy.get('website_selector').should('have.text', item.website);
    })
  })
})
  • Related