4

Could somebody help with finding the most adequate way to run the same fixture with different preconditions using testcafe (fixture beforeEach)?

Given I have following 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')
    }
  }
})

In the code example above I need to run this test for several times:

  1. I'm an anonymous user
  2. I'm the admin user
  3. I'm a default user

I've tried the following code, but it looks cumbersome and will create complexity when I have more than 1 test in the fixture that needs to be checked(taken from 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)
    }
  })
}

Is there a way I can run the whole fixture for 2-3 times with different beforeEach fixture hooks?

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
Andrii
  • 73
  • 4

1 Answers1

5

One simple solution could be to add three npm scripts in package.json:

"test": "testcafe chrome <all testcafe options>"
"test-anonymous": "npm test -- --user=anonymous"
"test-admin": "npm test -- --user=admin"
"test-default": "npm test -- --user=default"

Then read this custom command-line option in the beforeEach as I explained in Testcafe - Test command line argument outside test case

hdorgeval
  • 3,000
  • 8
  • 17
  • 1
    Thanks. That indeed works. I think that can be even more simplified if we could read meta info from fixture (from inside the fixture) and apply necessary amendments in beforeEach hook. – Andrii Mar 14 '19 at 16:24