【问题标题】:Async/await t test codes are not working in in beforeEach of TestCafeAsync/await t 测试代码在 TestCafe 的 beforeEach 中不起作用
【发布时间】:2019-03-04 03:07:18
【问题描述】:

当我尝试在 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.

提前致谢。

【问题讨论】:

  • Alex Skorkin,感谢您修正语法。 :)

标签: testing automation automated-tests e2e-testing testcafe


【解决方案1】:

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

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

由于实现细节,在某些情况下,可以在没有await 的情况下调用async 函数,但最好不要依赖此函数,而是始终将awaitasync 函数一起使用。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 2022-01-22
    • 1970-01-01
    • 2014-12-10
    • 2019-12-17
    • 2020-12-07
    相关资源
    最近更新 更多