admin管理员组文章数量:1192605
I have a fixture file in cypress which has json data within it
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
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
I am still new to both cypress and javascript. How can I get around this?
I have a fixture file in cypress which has json data within it
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
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
I am still new to both cypress and javascript. How can I get around this?
Share Improve this question edited Nov 26, 2020 at 6:41 Joshua 3,1663 gold badges26 silver badges40 bronze badges asked Nov 25, 2020 at 20:37 user60982user60982 1231 gold badge1 silver badge2 bronze badges4 Answers
Reset to default 10The fixture can be read at the top of the spec file and modified for all tests in the spec.
This way you avoid unexpected problems when using the fixture in another spec
const data = require('cypress/fixtures/dataFile.json')
data.table[0].number = 3
data.table[1].number = 4
it('tests with altered fixture', () => {
...
})
Modify fixture data before using it.
cy.fixture('user').then((user) => {
user.firstName = 'Jane'
cy.intercept('GET', '/users/1', user).as('getUser')
})
cy.visit('/users')
cy.wait('@getUser').then(({ request }) => {
expect(request.body.firstName).to.eq('Jane')
})
From: https://docs.cypress.io/api/commands/fixture#Modifying-fixture-data-before-using-it
You have to use both cy.readFile() and cy.writeFile() to achieve this. 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))
})
Below code is working for me while updating existing .JSON value.
cy.readFile("cypress/fixtures/credentials.json").then((data) => {
data.Username = '[email protected]'
data.Password = 'test@123'
cy.writeFile("cypress/fixtures/credentials.json", JSON.stringify(data))
})
本文标签: javascriptHow to update a fixture file in CypressStack Overflow
版权声明:本文标题:javascript - How to update a fixture file in Cypress - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738466957a2088362.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论