在每个TestCafe之前,异步/等待测试代码均无效

时间:2019-03-04 03:07:18

标签: testing automated-tests e2e-testing testcafe

当我尝试在TestCafe中使用beforeEach时,其中包含一些测试代码的函数似乎无法正常工作。 我在所有不同的灯具和测试中都使用doLogin

不起作用

const doLogin = async (t) => {
  const login = new Login();

  await t
    .maximizeWindow()
    .typeText(login.emailInput, accounts.EMAIL_SUCCESS, { replace: true, paste: true })
    .expect(login.emailInput.value).eql(accounts.EMAIL_SUCCESS, 'check an email')
    .typeText(login.passwordInput, accounts.PASSWORD, { paste: true })
    .click(login.loginButton);
};

fixture`App > ${menuName}`
  .page`${HOST}`
  .beforeEach(async (t) => {
    // This function is called
    // but tests inside the function were not run
    doLogin(t)
  });

带夹具的工作箱

fixture`App > ${menuName}`
  .page`${HOST}`
  .beforeEach(async (t) => {
    const login = new Login();

    // But this case is working.
    await t
      .maximizeWindow()
      .typeText(login.emailInput, accounts.EMAIL_SUCCESS, { replace: true, paste: true })
      .expect(login.emailInput.value).eql(accounts.EMAIL_SUCCESS, 'check an email')
      .typeText(login.passwordInput, accounts.PASSWORD, { paste: true })
      .click(login.loginButton);
  });

通过测试致电的工作案例

test(`show all ${menuName} menu's components`, async (t) => {
  // When I added a function directly into a test function then it worked.
  doLogin(t);
  // some codes

有人可以告诉我这段代码中的问题吗?

official document中,它说At the moment test hooks run, the tested webpage is already loaded, so that you can use test actions and other test run API inside test hooks.

谢谢。

1 个答案:

答案 0 :(得分:4)

似乎您在await调用之前错过了doLogin()关键字:

fixture`App > ${menuName}`
  .page`${HOST}`
  .beforeEach(async (t) => {
    // Don't forget about await
    await doLogin(t)
  });

由于实现细节,在某些情况下可以在不使用async的情况下调用await函数,但是最好不要依赖于此,而总是将await与{{1} }函数。

如果添加了async关键字,但是它不能解决测试问题,请随时在TestCafe存储库中创建a bug report,并提供一个完整的示例来运行以重现该问题。