Sti*_*iño 5 javascript cypress
我正在尝试对两个单独页面上显示的组件运行完全相同的赛普拉斯测试。为了实现这一目标,我想我应该使用一个forEach
语句,以便为每个“状态”运行相同的代码(参见下面的代码)。
问题在于,在状态 #1 的测试完成之前before
,语句中的代码块开始针对状态 #2 运行。这会导致状态 #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() {
})
})
})
})
Run Code Online (Sandbox Code Playgroud)
找到了一个解决方案:我必须将其包装到另一个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() {
})
})
})
})
})
Run Code Online (Sandbox Code Playgroud)