简体   繁体   English

如何在赛普拉斯更新夹具文件

[英]How to update a fixture file in Cypress

I have a fixture file in cypress which has json data within it我在 cypress 中有一个夹具文件,其中包含 json 数据

I want to be able to update the fields in this fixture file when I run the test script我希望能够在运行测试脚本时更新此夹具文件中的字段

For example the fixture file would read例如,夹具文件将读取

 {
  table: [
    {
      name: 'Joe',
      number: 1,
    },
    {
      name: 'Bob',
      number: 2,
    },
  ],
};

And I want to update the number fields to 3 and 4我想将数字字段更新为 3 和 4

I have tried我努力了

cy.fixture('dataFile.json')
  .as('data')
  .then((data) => {
    data.table[0].number = 3;
    data.table[1].number = 4;
  });

but it is not working when I run the test i am still seeing everything behave as if the number fields are still 1 and 2. If I print the fields to the console i can see they are actually updated but cypress is still running with the original data但是当我运行测试时它不起作用我仍然看到一切都表现得好像数字字段仍然是 1 和 2。如果我将字段打印到控制台我可以看到它们实际上已更新但赛普拉斯仍在运行原始数据

I am still new to both cypress and javascript.我对 cypress 和 javascript 仍然是新手。 How can I get around this?我怎样才能解决这个问题?

You have to use both cy.readFile() and cy.writeFile() to achieve this.您必须同时使用cy.readFile()cy.writeFile()来实现这一点。 You can write something like:您可以编写如下内容:

cy.readFile("cypress/fixtures/dataFile.json", (err, data) => {
    if (err) {
        return console.error(err);
    };
}).then((data) => {
    data.table[0].number = 3
    data.table[1].number = 4
    cy.writeFile("cypress/fixtures/dataFile.json", JSON.stringify(data))
})

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

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