有人可以帮助找到使用 testcafe(fixture beforeEach)以不同前提条件运行相同 fixture 的最合适方法吗?
鉴于我有以下夹具:
fixture('[Test] My Page')
.meta({ env: 'aat', mobile: 'true', author: 'me' })
.beforeEach(async t => {
await t.navigateTo(myPage.url)
await waitForReact(10000)
})
test('Page should load all data successfully', async t => {
const { Query: queries } = await t.ctx.graphQLTools.mockManagementClient.getCalls()
for (const q of Object.keys(queries)) {
for (const { args, context } of Object.keys(queries[q])) {
await t.expect(queries[q][args]).typeOf('undefined')
await t.expect(queries[q][context]).typeOf('undefined')
}
}
})
在上面的代码示例中,我需要多次运行此测试:
- 我是匿名用户
- 我是管理员用户
- 我是默认用户
我试过下面的代码,但它看起来很麻烦,并且当我在需要检查的夹具中有超过 1 个测试时会产生复杂性(取自 https://testcafe-discuss.devexpress.com/t/multiple-execution-of-one-test-with-different-data/219):
const a = ['anonymous', 'logged In']
for (const user of a) {
test.meta({ wip: 'true' })(`Page should load all data successfully for ${user} user`, async t => {
if (R.equals('logged In', user)) {
await helpers.setCookie({ key: 'access_token', value: '123123123' })
await helpers.reloadPage()
}
const { Query: queries } = await t.ctx.graphQLTools.mockManagementClient.getCalls()
await t.expect(helpers.isEqlToEmptyQuery(queries.basket)).eql(true)
await t.expect(helpers.isEqlToEmptyQuery(queries.categories)).eql(true)
if (R.equals('logged In', user)) {
await t.expect(helpers.isEqlToEmptyQuery(queries.customer)).eql(true)
}
})
}
有没有一种方法可以使用不同的 beforeEach fixture hooks 运行整个 fixture 2-3 次?
最佳答案
一个简单的解决方案是在 package.json
中添加三个 npm 脚本:
"test": "testcafe chrome <all testcafe options>"
"test-anonymous": "npm test -- --user=anonymous"
"test-admin": "npm test -- --user=admin"
"test-default": "npm test -- --user=default"
然后按照我在 Testcafe - Test command line argument outside test case 中解释的那样,在 beforeEach
中阅读这个自定义命令行选项
关于testing - 是否可以在不同的先决条件下运行相同的 testcafe fixture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55140062/