gpt4 book ai didi

javascript - 如何在 Cypress 中使用不同的夹具运行相同的测试?

转载 作者:行者123 更新时间:2023-12-04 12:25:09 26 4
gpt4 key购买 nike

我正在尝试对显示在两个单独页面上的组件运行完全相同的 Cypress 测试。为了实现这一点,我想我会使用 forEach陈述
以便为每个“状态”运行相同的代码(参见下面的代码)。
问题是 before 中的代码块语句开始运行状态#2,之前 状态 #1 的测试已经完成。这导致状态#1 的测试失败(因为它具有状态#2 固定装置)。
如何制作 before状态#2 的部分等待状态#1 中的所有测试完成?

const states = [
{
"startPath": "/path1",
"fixture": "fixture1"
},
{
"startPath": "/path2",
"fixture": "fixture2"
}
]

describe('Start test', function() {

// Loop through both test
states.forEach((state) => {

// In this before statement the fixtures are setup
before(function () {
cy.flushDB()
cy.fixture(state.fixture)
.then(fixtureData => {
new Cypress.Promise((resolve, reject) =>
cy.createCollections(fixtureData, resolve, reject)
)
})
.then(() => cy.visit(state.startPath))
})

context('Within this context', function() {
it(`Can run some test for fixture ${state.fixture}`, function() {

})
})
})
})

最佳答案

找到了解决方案:我不得不将它包装到另一个 describe代码块,然后它工作:

const states = [
{
"startPath": "/path1",
"fixture": "fixture1",
"context": "1"
},
{
"startPath": "/path2",
"fixture": "fixture2",
"context": "2"
}
]

describe('Start test', function() {

// Loop through both test
states.forEach((state) => {

// SOLUTION HERE
describe(state.context, () => {

// In this before statement the fixtures are setup
before(function () {
cy.flushDB()
cy.fixture(state.fixture)
.then(fixtureData => {
new Cypress.Promise((resolve, reject) =>
cy.createCollections(fixtureData, resolve, reject)
)
})
.then(() => cy.visit(state.startPath))
})

context('Within this context', function() {
it(`Can run some test for fixture ${state.fixture}`, function() {

})
})
})
})
})

关于javascript - 如何在 Cypress 中使用不同的夹具运行相同的测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58017174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com