简体   繁体   English

写入后无法从夹具文件中读取数据

[英]Unable to read data from fixture file after writing it

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.我正在从 API 中提取的 .json 文件中编写带有 ID 的 URL,但问题是 cy.log() 在将数据写入文件之前打印数据。 Because the AppelData.json has the written data, but cy.log() prints nothing.因为 AppelData.json 有写入的数据,但 cy.log() 什么也不打印。 After the second run, the cy.log() prints the previous data from AppelData.json.第二次运行后,cy.log() 打印 AppelData.json 中的先前数据。

So how do I make, that cy.log() to print the data from AppelData.json only after it is been written?那么我该如何让 cy.log() 仅在写入后才从 AppelData.json 打印数据?

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!!谢谢!!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM