1

I'm writing a URL with an ID in a .json file that I'm extracting from the API, but the problem is that the cy.log() is printing the data before it is wrote in the file. Because the AppelData.json has the written data, but cy.log() prints nothing. After the second run, the cy.log() prints the previous data from AppelData.json.

So how do I make, that cy.log() to print the data from AppelData.json only after it is been written?

describe('Creer un appel modal', () => {

    beforeEach(() => {

        cy.fixture('AppelData').then(function(data) {
            this.AppelData = data
        })

        cy.visit('/')
        cy.loginAsAdmin()
       

    })

    it('Create an intervention request using existing site', function() {

        navigateTo.plusAppelButton()
        onAppelModal.SelectSite(this.AppelData.Site)
        onAppelModal.SelectMetier(this.AppelData.Metier)
        onAppelModal.FillMotif(this.AppelData.Motif)

        cy.intercept('POST','/documents/datatable/intervention_request/**').as('response')
            cy.contains('Valider').click()
            cy.wait('@response').get('@response').then(xhr => {
                console.log(xhr)
                cy.readFile('cypress/fixtures/AppelData.json').then(AppelData => {
                AppelData.AppelID = xhr.request.url.replace(/\D/g,'').replace(/3/, '')
                cy.writeFile('cypress/fixtures/AppelData.json', AppelData) 
                cy.log(this.AppelData.AppelID) // logs no value on the first run, and prints old value from the 2nd run
                })
 
            })
    })
})

Thank you!!

1 Answers1

1

Assuming that that the value is updated into the fixtures file, you can do this:

it('Create an intervention request using existing site', function () {
  navigateTo.plusAppelButton()
  onAppelModal.SelectSite(this.AppelData.Site)
  onAppelModal.SelectMetier(this.AppelData.Metier)
  onAppelModal.FillMotif(this.AppelData.Motif)
  cy.intercept('POST', '/documents/datatable/intervention_request/**').as(
    'response'
  )
  cy.contains('Valider').click()
  cy.wait('@response')
    .get('@response')
    .then((xhr) => {
      console.log(xhr)
      cy.readFile('cypress/fixtures/AppelData.json').then((AppelData) => {
        AppelData.AppelID = xhr.request.url.replace(/\D/g, '').replace(/3/, '')
        cy.writeFile('cypress/fixtures/AppelData.json', AppelData)
      })
    })

  cy.readFile('cypress/fixtures/AppelData.json').then((AppelData) => {
    cy.log(AppelData.AppelID)
  })
})
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • Thanks! It's working! But why didn't it work in my code? cy.log() was executed after the write file, so logically it should've been provided the new data from the file no? Or because I have a fixture load before the test, the log got the value from the file and didn't update after the new record? – Andrei Nico Jul 22 '22 at 14:34
  • 1
    Because after updating the value, you need to read the file again to get the updated value. – Alapan Das Jul 22 '22 at 14:35