cy.fixture()

To load a fixture, use the cy.fixture() command.

cy.server()
cy
  .fixture('example.json').as('comment')

  .route(/comments/, [email protected]').as('getComment')
  .get('.fixture-btn').click()
  .wait([email protected]').its('responseBody')
    .should('have.property', 'name')
      .and('include', 'Using fixtures to represent data')

// you can also just write the fixture in the route
cy
  .route(/comments/, 'fixture:example.json').as('getComment')
  .get('.fixture-btn').click()
  .wait([email protected]').its('responseBody')
    .should('have.property', 'name')
      .and('include', 'Using fixtures to represent data')

// or write fx to represent fixture
// by default it assumes it's .json
cy
  .route(/comments/, 'fx:example').as('getComment')
  .get('.fixture-btn').click()
  .wait([email protected]').its('responseBody')
    .should('have.property', 'name')
      .and('include', 'Using fixtures to represent data')

cy.readFile()

To read a file's content, use the cy.readFile() command.

cy
  .readFile('cypress.json').then(function(json) {
    expect(json).to.be.an('object')
  })

cy.writeFile()

To write to a file with the specified contents, use the cy.writeFile() command.

cy
  .request('https://jsonplaceholder.typicode.com/users').then(function(response){
    cy.writeFile('cypress/fixtures/users.json', response.body)
  })
  .fixture('users').then(function(users){
    expect(users[0].name).to.exist
  })