testing - 是否可以在不同的先决条件下运行相同的 testcafe fixture

标签 testing automated-tests e2e-testing web-testing testcafe

有人可以帮助找到使用 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. 我是匿名用户
  2. 我是管理员用户
  3. 我是默认用户

我试过下面的代码,但它看起来很麻烦,并且当我在需要检查的夹具中有超过 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/

相关文章:

ruby - rspec:第一次失败后如何继续测试

xcode - XCUITest 检测 UIView

cypress - 如何让 cypress 等待依赖于另一个响应的响应?

node.js - testcafe 正在打开浏览器并默认为/browser/connect/.... 并且无法执行测试

selenium-webdriver - 有没有办法在 Protractor 中检索当前的相对 url?

javascript - Paper.js 上的触发工具事件

grails - 为 grails 插件编写集成测试用例

ruby - 在 Ruby 测试中避免 protected 级别的访问器和相等方法

c# - WatiN: GetAttributeValue ("value") 不返回值

reactjs - Testcafe 忽略覆盖元素