0

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": "jsmith@gmail.com",
    "due": "50$",
    "webSite": "www"
  },
  {
    "firstName": "Frank",
    "email": "fbach@yahoo.com",
    "due": "51$",
    "webSite": "www"
  },
  {
    "firstName": "Jason",
    "email": "jdoe@hotmail.com",
    "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);
    })

3 Answers3

1

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);
    })
  })
})
0

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)
  }
})
jjhelguero
  • 2,281
  • 5
  • 13
0

Many thanks for answer, but how create loop like:

  1. Find "firstName": "John" on web (from fixture file)
  2. Once find it continue assertion in this current row, rest of columns like email, due and website where is mentioned firstName?
  3. Once all columns was checked in this row, start loop next data set.

Those approaches make search on whole page not in one row.

Really many thanks with each help.