When I tried to use beforeEach
in TestCafe, a function with some test codes inside of it seems not working properly.
I am using doLogin
in all different fixtures and tests.
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
Could anyone tell me the problem in this code?
In the official document, it said 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.
Thanks in advance.
It seems you missed the await
keyword before the doLogin()
call:
fixture`App > ${menuName}`
.page`${HOST}`
.beforeEach(async (t) => {
// Don't forget about await
await doLogin(t)
});
Due to the implementation details it's possible to call an async
function without await
in some cases, but it's better to not rely on this and always use await
with async
functions.
If you add the async
keyword and it don't fix the test, feel free to create a bug report in the TestCafe repository and provide a complete example that can be run to reproduce the problem.