如何根据 Cypress.io 中 fixture 返回的对象的索引 运行 进行循环测试?
How to run a test in loop based on the index of the objects returned by fixture in Cypress.io?
我需要根据 Cypress.io 中的夹具返回的对象数量 运行 测试 'n' 次。我正在使用赛普拉斯公开的 forEach 方法进行循环。我在 'it' 命令的外部 space 中使用 forEach 循环,但在内部描述。我在夹具对象上收到未定义的错误。
请看下面的代码:
describe("Search by people returns expected result", () => {
before(() => {
cy.fixture('people').then(function(peopleJson) {
this.peopleJson = peopleJson;
});
cy.fixture('planets').then(function(planetsJson) {
this.planetsJson = planetsJson;
});
})
beforeEach(() => {
cy.visit('/')
});
this.peopleJson.forEach(data => {
it('Verify people by'+ data.name, function() {
cy.searchBy('people', data.name)
cy.verifyPerson(data)
})
})
afterEach(() => {
cy.log("inside aftereach!")
clearForm();
})});
Error message that I am getting in Cypress compiler is
The following error originated from your test code, not from Cypress.
Cannot read properties of undefined (reading 'peopleJson')
当 Cypress 检测到源自您的测试代码的未捕获错误时
它会自动使当前测试失败。
赛普拉斯无法将此错误与任何特定测试相关联。
我们动态生成了一个新测试来显示此失败。
this.peopleJson
现在未定义,但如果我在 'if' 块内使用 forEach 循环,则代码可以正常工作。但我想动态生成测试用例,并且如果任何数据对象的任何测试用例失败,那么测试将继续对其他人进行测试。
我尝试了另一种方法,将 fixture 命令放在 forEach 的正上方,但这也没有用。我参考了 Cypress.io 的原始文档,发现 fixture 调用必须是 It 块的一部分,所以我不能在 forEach 循环之上使用 fixture。
处理这种情况的最佳方法是什么?
People.json
[
{
"name": "R2-D2",
"gender": " n/a ",
"birthYear": " 33BBY ",
"eyeColor": " red ",
"skinColor": " white, blue ",
"description": "name with numbers"
},
{
"name": "Obi-Wan Kenobi",
"gender": " male ",
"birthYear": " 57BBY ",
"eyeColor": " blue-gray ",
"skinColor": " fair ",
"description": "name with dash and space"
},
{
"name": "Boba Fett",
"gender": " male ",
"birthYear": " 31.5BBY ",
"eyeColor": " brown ",
"skinColor": " fair ",
"description": "name consists of two words"
},
{
"name": "Dormé",
"gender": " female ",
"birthYear": " unknown ",
"eyeColor": " brown ",
"skinColor": " light ",
"description": "name with special character"
},
{
"name": "Ki-Adi-Mundi",
"gender": " male ",
"birthYear": " 92BBY ",
"eyeColor": " yellow ",
"skinColor": " pale ",
"description": "name with two dashes"
}
]
因为您想在 it
块之外使用 json 文件,所以您不能使用 beforeEach()
。相反,您必须在测试中导入 json 文件并应用 forEach
import peopleJson from '../fixtures/people.json' //Assuming your test is inside integration folder
describe('Search by people returns expected result', () => {
beforeEach(() => {
cy.visit('/')
})
peopleJson.forEach((data) => {
it('Verify people by' + data.name, function () {
cy.searchBy('people', data.name)
cy.verifyPerson(data)
})
})
afterEach(() => {
cy.log('inside aftereach!')
clearForm()
})
})
要使用 this
关键字,您必须使用常规函数 function () {}
而不是粗箭头 () => {}
describe("Search by people returns expected result", () => {
before( function () { // changed to regular function
cy.fixture('people').then(function(peopleJson) {
this.peopleJson = peopleJson;
});
cy.fixture('planets').then(function(planetsJson) {
this.planetsJson = planetsJson;
});
})
beforeEach(() => {
cy.visit('/')
});
this.peopleJson.forEach(data => {
it('Verify people by'+ data.name, function() {
cy.searchBy('people', data.name)
cy.verifyPerson(data)
})
})
afterEach(() => {
cy.log("inside aftereach!")
clearForm();
})});
我需要根据 Cypress.io 中的夹具返回的对象数量 运行 测试 'n' 次。我正在使用赛普拉斯公开的 forEach 方法进行循环。我在 'it' 命令的外部 space 中使用 forEach 循环,但在内部描述。我在夹具对象上收到未定义的错误。 请看下面的代码:
describe("Search by people returns expected result", () => {
before(() => {
cy.fixture('people').then(function(peopleJson) {
this.peopleJson = peopleJson;
});
cy.fixture('planets').then(function(planetsJson) {
this.planetsJson = planetsJson;
});
})
beforeEach(() => {
cy.visit('/')
});
this.peopleJson.forEach(data => {
it('Verify people by'+ data.name, function() {
cy.searchBy('people', data.name)
cy.verifyPerson(data)
})
})
afterEach(() => {
cy.log("inside aftereach!")
clearForm();
})});
Error message that I am getting in Cypress compiler is
The following error originated from your test code, not from Cypress.
Cannot read properties of undefined (reading 'peopleJson')
当 Cypress 检测到源自您的测试代码的未捕获错误时 它会自动使当前测试失败。
赛普拉斯无法将此错误与任何特定测试相关联。
我们动态生成了一个新测试来显示此失败。
this.peopleJson
现在未定义,但如果我在 'if' 块内使用 forEach 循环,则代码可以正常工作。但我想动态生成测试用例,并且如果任何数据对象的任何测试用例失败,那么测试将继续对其他人进行测试。
我尝试了另一种方法,将 fixture 命令放在 forEach 的正上方,但这也没有用。我参考了 Cypress.io 的原始文档,发现 fixture 调用必须是 It 块的一部分,所以我不能在 forEach 循环之上使用 fixture。
处理这种情况的最佳方法是什么?
People.json
[
{
"name": "R2-D2",
"gender": " n/a ",
"birthYear": " 33BBY ",
"eyeColor": " red ",
"skinColor": " white, blue ",
"description": "name with numbers"
},
{
"name": "Obi-Wan Kenobi",
"gender": " male ",
"birthYear": " 57BBY ",
"eyeColor": " blue-gray ",
"skinColor": " fair ",
"description": "name with dash and space"
},
{
"name": "Boba Fett",
"gender": " male ",
"birthYear": " 31.5BBY ",
"eyeColor": " brown ",
"skinColor": " fair ",
"description": "name consists of two words"
},
{
"name": "Dormé",
"gender": " female ",
"birthYear": " unknown ",
"eyeColor": " brown ",
"skinColor": " light ",
"description": "name with special character"
},
{
"name": "Ki-Adi-Mundi",
"gender": " male ",
"birthYear": " 92BBY ",
"eyeColor": " yellow ",
"skinColor": " pale ",
"description": "name with two dashes"
}
]
因为您想在 it
块之外使用 json 文件,所以您不能使用 beforeEach()
。相反,您必须在测试中导入 json 文件并应用 forEach
import peopleJson from '../fixtures/people.json' //Assuming your test is inside integration folder
describe('Search by people returns expected result', () => {
beforeEach(() => {
cy.visit('/')
})
peopleJson.forEach((data) => {
it('Verify people by' + data.name, function () {
cy.searchBy('people', data.name)
cy.verifyPerson(data)
})
})
afterEach(() => {
cy.log('inside aftereach!')
clearForm()
})
})
要使用 this
关键字,您必须使用常规函数 function () {}
而不是粗箭头 () => {}
describe("Search by people returns expected result", () => {
before( function () { // changed to regular function
cy.fixture('people').then(function(peopleJson) {
this.peopleJson = peopleJson;
});
cy.fixture('planets').then(function(planetsJson) {
this.planetsJson = planetsJson;
});
})
beforeEach(() => {
cy.visit('/')
});
this.peopleJson.forEach(data => {
it('Verify people by'+ data.name, function() {
cy.searchBy('people', data.name)
cy.verifyPerson(data)
})
})
afterEach(() => {
cy.log("inside aftereach!")
clearForm();
})});