Asad Khan
Asad Khan

Reputation: 41

Reading updated fixture file in Cypress

I am trying to run a test case in which a fixture file is updated. I am then using this file and uploading it somewhere. The problem is that during the test run, Cypress caches the fixture files so even if the file is updated, cypress-file-upload uses the cached version of the file. Is there a way to solve this issue?

Upvotes: 0

Views: 1561

Answers (4)

Neel Alex
Neel Alex

Reputation: 733

use cy.readFile() instead of cy.fixture()

the syntax needs to be adapted as follows:
cy.fixture('generated/${fileName}')
=> cy.readFile('./cypress/fixtures/generated/${fileName}')

Upvotes: 0

BalajiK
BalajiK

Reputation: 1

import example from '../fixtures/example.json'

it('accessing fixture file array', () => {
    cy.fixture('example').then((userdata) => {
        userdata.forEach(function(item){
            cy.log('UserName: ' + item.username);
            cy.log('Password: ' + item.password);
            cy.log('Body: ' + item.body);
          });
      })
});

example.json:

[ {
  "username": "admin",
  "password": "admin",
  "body": "Fixtures data 1"
},
{
  "username": "test",
  "password": "test",
  "body": "Fixtures data 2"
}]

Upvotes: 0

user14783414
user14783414

Reputation:

I think you can adapt the example shown under Working with raw file contents, and use cy.readFile() to bypass the caching problem with cy.fixture().

cy.readFile('./cypress/fixtures/my-file.json')
  .then(fileContent => {
    cy.get('[data-cy="file-input"]').attachFile({
      fileContent,
      filePath: './cypress/fixtures/my-file.json',
      encoding: 'utf-8',
      lastModified: new Date().getTime()
    });
  });

Upvotes: 1

Rosen Mihaylov
Rosen Mihaylov

Reputation: 1427

I have had problems with clearing the cypress-file-upload cache as well and the solution I found is reloading the page after each upload. For example:

cy.get('[data-qa="inputFile_image"]')
   .attachFile(image1.url)
cy.wait(4000)
cy.get('[data-qa="saveButton"]')
    .click()
cy.get('[data-qa="successNotification"]')
   .should('be.visible')
cy.reload()
cy.get('[data-qa="image1"]')
   .should('be.visible')
cy.get('[data-qa="inputFile_image"]')
    .attachFile(image2.url)
cy.wait(4000)
cy.get('[data-qa="saveButton"]')
    .click()
cy.get('[data-qa="successNotification"]')
    .should('be.visible')
cy.reload()

Upvotes: 0

Related Questions