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() {
})
})
})
})
解决方案
找到了一个解决方案:我不得不将它包装到另一个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() {
})
})
})
})
})
推荐阅读
- php - 如何在PHP中的日期数组中从最近的日期到最远的日期对日期进行排序
- git - 如何将一个存储库的分支移动到不同存储库的另一个分支
- php - 是否可以通过php中的锚标记发送用户输入的关键字
- python - 亚马逊销售合作伙伴 API 提要使用 Python 加密文件
- python - 禁止消息 discord.py 上的命令
- reactjs - axios 调用数据返回 useState undefined
- javascript - 使用 Azure 函数从 Azure Blob 存储下载文件会返回不同的文件大小
- ssl - Gradle 未能构建应用程序 - 无法找到请求目标的有效认证路径
- c# - 从未排序的数组中获取有序数组的范围
- python - AWS TimeStream Python SDK:无法创建正确的客户端对象