Javascript 检索Cypress fixture JSON数据的所有类似键值
我很难找到一种干净的方法从Cypress fixture JSON文件中的同一个key-in对象中检索所有值的值 例如,在下面名为myPeople.JSON的JSON文件中,我只需要people下所有对象的firstName值 如果我想检索people下的所有对象,我可以执行cy.fixturemyPeople.itspeople,它将漂亮地返回所有对象 事实证明,收回所有firstName值是很困难的。我尝试了以下方法来检索这些值,但它们没有达到我预期的效果Javascript 检索Cypress fixture JSON数据的所有类似键值,javascript,typescript,cypress,Javascript,Typescript,Cypress,我很难找到一种干净的方法从Cypress fixture JSON文件中的同一个key-in对象中检索所有值的值 例如,在下面名为myPeople.JSON的JSON文件中,我只需要people下所有对象的firstName值 如果我想检索people下的所有对象,我可以执行cy.fixturemyPeople.itspeople,它将漂亮地返回所有对象 事实证明,收回所有firstName值是很困难的。我尝试了以下方法来检索这些值,但它们没有达到我预期的效果 cy.fixture("
cy.fixture("myPeople").its("people").its("firstName") //This doesn't work likely because it expects a specific object under the people node to look up its firstName key's value
但是,如果传入显式索引,它将返回该索引的firstName值:
cy.fixture("myPeople").its("people").its(0).its("firstName") //This returns the value "Bob".
我在这里遗漏了什么?不考虑Cypress本身,您似乎想要使用JavaScript映射函数 例如:
let people = {
"people": [
{
"firstName": "Bob",
"lastName": "Dawson"
},
{
"firstName": "Tom",
"lastName": "Wild"
},
{
"firstName": "Sally",
"lastName": "Rose"
}
]
}
let firstNames = people.people.map(person => person.firstName)
console.log(firstNames) // --> [ 'Bob', 'Tom', 'Sally' ]
更多。不考虑Cypress本身,您似乎想要使用JavaScript映射函数 例如:
let people = {
"people": [
{
"firstName": "Bob",
"lastName": "Dawson"
},
{
"firstName": "Tom",
"lastName": "Wild"
},
{
"firstName": "Sally",
"lastName": "Rose"
}
]
}
let firstNames = people.people.map(person => person.firstName)
console.log(firstNames) // --> [ 'Bob', 'Tom', 'Sally' ]
更多。一个选项是使用简单的for循环,并从fixtures文件访问所有数组元素
describe('Get first Name from fixtures', () => {
beforeEach(() => {
//Load Fixture File
cy.fixture('myPeople.json').as('myPeople')
})
it('Test', () => {
cy.get('@myPeople').then((myPeople) => {
for (var i = 0; i < myPeople.people.length; i++) {
//Print the first Names
cy.log(myPeople.people[i].firstName)
}
})
})
})
一个选项是使用简单的for循环并从fixtures文件访问所有数组元素
describe('Get first Name from fixtures', () => {
beforeEach(() => {
//Load Fixture File
cy.fixture('myPeople.json').as('myPeople')
})
it('Test', () => {
cy.get('@myPeople').then((myPeople) => {
for (var i = 0; i < myPeople.people.length; i++) {
//Print the first Names
cy.log(myPeople.people[i].firstName)
}
})
})
})
在Cypress中,您可以像这样应用map.cy.fixturemyPeople.itspeople.thenpeople=>people.mapp=>p.firstName我知道这是一件小事,我完全想过头了。这就成功了,谢谢你@Ackroydd还专门为Cypress提供了一个简洁的解决方案,效果很好。在Cypress中,您可以像这样应用map cy.fixturemyPeople.itspeople.thenpeople=>people.mapp=>p.FirstName我知道这是一件小事,我完全想过头了。这就成功了,谢谢你@Ackroydd还专门为Cypress提供了一个简洁的解决方案,效果很好。