如何在赛普拉斯的 json 中附加一个新的键值对
How to append a new key,value pair in json in cypress
我有一个字段想读取并追加到现有 json。
我知道 cy.write({a+}) 将数据附加到 json 文件,但它会创建一对新的花括号。
我想用下面的格式写一个现有的 json
{
"Name":"X",
"Age:,"Y",
"New_data":"Z"
}
现在的格式是这样的
{
"Name":"X",
"Age:,"Y",
}
{
"New_data":"Z"
}
这是一种在现有 json 中写入数据的优雅方式。
在JSON之前:
{
"Name":"X",
"Age":"Y"
}
假设您在 fixtures 文件夹中有一个文件 example.json
。您的代码应如下所示:
cy.readFile('cypress/fixtures/example.json').then((data) => {
data.newKey = 'newValue'
cy.writeFile('cypress/fixtures/example.json', JSON.stringify(data))
})
JSON之后:
{
"Name":"X",
"Age":"Y",
"newKey":"newValue"
}
如果要在 JSON 文件中附加数据,您需要先使用 cy.readfile
在写入 JSON 文件之前:
cy.readFile('cypress/fixtures/example.json').then((data) => {
data.newKey = 'newValue'
cy.writeFile('cypress/fixtures/example.json', data)
})
已经给出的答案是正确的,并且是解决问题的常规和有据可查的方法。
您还可以使用 Proxy set() handler 在每次添加 属性 时自动保留对象。
function persist(fileName, initial = {}) {
Cypress._myObject = { ...initial }
const handler = {
set(obj, prop, value) {
Reflect.set(...arguments)
cy.writeFile(`./cypress/fixtures/${fileName}`, Cypress._myObject, { log: false });
return true;
}
};
return new Proxy(Cypress._myObject, handler);
}
// Wrap the object
const myObject = persist('myObject.json', { name: 'fred', age: 30 })
it('test1', () => {
// Modify it, fixture file is also updated
myObject.address1 = "somewhere"
myObject.address2 = "somecity" // could be set in a 2nd test
// Check the fixture file
cy.fixture('myObject.json').then(console.log)
.should(fixture => {
expect(fixture.address1).to.eq('somewhere')
expect(fixture.address2).to.eq('somecity')
})
})
或者,自定义命令(需要嵌套级别)
/cypress/support/index.js
Cypress.Commands.add('persist', (fileName, initial = {}) => {
Cypress._myObject = { ...initial }
const handler = {
set(obj, prop, value) {
Reflect.set(...arguments)
cy.writeFile(`./cypress/fixtures/${fileName}`, Cypress._myObject, { log: false });
return true;
}
};
return new Proxy(Cypress._myObject, handler)
})
测试
it('test2', () => {
cy.persist('myObject.json', { name: 'fred', age: 30 })
.then(myObject => {
myObject.address1 = "somewhere"
myObject.address2 = "somecity"
cy.fixture('myObject.json')
.should(fixture => {
expect(fixture.address1).to.eq('somewhere')
expect(fixture.address2).to.eq('somecity')
})
})
})
我有一个字段想读取并追加到现有 json。 我知道 cy.write({a+}) 将数据附加到 json 文件,但它会创建一对新的花括号。 我想用下面的格式写一个现有的 json
{
"Name":"X",
"Age:,"Y",
"New_data":"Z"
}
现在的格式是这样的
{
"Name":"X",
"Age:,"Y",
}
{
"New_data":"Z"
}
这是一种在现有 json 中写入数据的优雅方式。
在JSON之前:
{
"Name":"X",
"Age":"Y"
}
假设您在 fixtures 文件夹中有一个文件 example.json
。您的代码应如下所示:
cy.readFile('cypress/fixtures/example.json').then((data) => {
data.newKey = 'newValue'
cy.writeFile('cypress/fixtures/example.json', JSON.stringify(data))
})
JSON之后:
{
"Name":"X",
"Age":"Y",
"newKey":"newValue"
}
如果要在 JSON 文件中附加数据,您需要先使用 cy.readfile
在写入 JSON 文件之前:
cy.readFile('cypress/fixtures/example.json').then((data) => {
data.newKey = 'newValue'
cy.writeFile('cypress/fixtures/example.json', data)
})
已经给出的答案是正确的,并且是解决问题的常规和有据可查的方法。
您还可以使用 Proxy set() handler 在每次添加 属性 时自动保留对象。
function persist(fileName, initial = {}) {
Cypress._myObject = { ...initial }
const handler = {
set(obj, prop, value) {
Reflect.set(...arguments)
cy.writeFile(`./cypress/fixtures/${fileName}`, Cypress._myObject, { log: false });
return true;
}
};
return new Proxy(Cypress._myObject, handler);
}
// Wrap the object
const myObject = persist('myObject.json', { name: 'fred', age: 30 })
it('test1', () => {
// Modify it, fixture file is also updated
myObject.address1 = "somewhere"
myObject.address2 = "somecity" // could be set in a 2nd test
// Check the fixture file
cy.fixture('myObject.json').then(console.log)
.should(fixture => {
expect(fixture.address1).to.eq('somewhere')
expect(fixture.address2).to.eq('somecity')
})
})
或者,自定义命令(需要嵌套级别)
/cypress/support/index.js
Cypress.Commands.add('persist', (fileName, initial = {}) => {
Cypress._myObject = { ...initial }
const handler = {
set(obj, prop, value) {
Reflect.set(...arguments)
cy.writeFile(`./cypress/fixtures/${fileName}`, Cypress._myObject, { log: false });
return true;
}
};
return new Proxy(Cypress._myObject, handler)
})
测试
it('test2', () => {
cy.persist('myObject.json', { name: 'fred', age: 30 })
.then(myObject => {
myObject.address1 = "somewhere"
myObject.address2 = "somecity"
cy.fixture('myObject.json')
.should(fixture => {
expect(fixture.address1).to.eq('somewhere')
expect(fixture.address2).to.eq('somecity')
})
})
})