Async / await t测试代码在TestCafe的beforeEach中不起作用

问题描述 投票:1回答:1

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

Not working

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)
  });

Working Case with a fixture

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);
  });

Working Case with calling from a test

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.

提前致谢。

testing automated-tests e2e-testing testcafe
1个回答
4
投票

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

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

由于实现细节,在某些情况下可以调用没有asyncawait函数,但最好不要依赖于此并始终使用awaitasync函数。

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

© www.soinside.com 2019 - 2024. All rights reserved.