After upgrading Playwright to latest version, I've encountered the following issue. In our fixture, we've set up these blocks, but with the latest Playwright versions, it seems that using them is no longer allowed. Can someone please guide?
The error message I'm getting is: "Error: Playwright Test did not expect test.beforeAll() to be called here. The most common reasons for this error include:
Calling test.beforeAll() in a configuration file. Calling test.beforeAll() in a file that is imported by the configuration file. Having two different versions of @playwright/test. This usually happens when one of the dependencies in your package.json depends on @playwright/test."
Fixture file:
import { testSetup } from './test-setup';
import {
loginPage...etc} from '/page-object.ts';
let testPage: Page = null;
export const test = base.extend<{
loginPage: LoginPage;
}>({
page: async ({ browser }, use) => {
if (testPage === null) {
testPage = await browser.newPage();
}
await use(testPage);
},
loginPage: async ({ page }, use) => {
await use(new LoginPage(page));
},
testSetup(test);
export const expect = test.expect;
Test-setup.ts
export const testSetup = (test) => {
test.beforeAll(async () => {
// login & store sessions
});
test.beforeEach(async () => {
// other steps to capture test information for an internal portal
});
};
This thread is trying to answer question "How can I resolve the 'Error: Playwright Test did not expect test.beforeAll() to be called here' after upgrading Playwright to the latest version?"
base.
come from at your fixture?page: async...
at your fixture. At your test you can access page directly through your fixture.test('my_test', asyn({loginPage, page, etc}))
you have access to everything and you dont need to run .beforeAll/.beforeEach
...async({loginPage....
loaded your data directly when you step into your test.fixture.ts
import { test as base } from '@playwright/test';
import { LoginPage} from 'page-object';
export const test = base.extend<{loginPage: LoginPage}>({
loginPage: async ({ page }, use) => {
await use(new LoginPage(page));
},
custom_page: async ({ page }, use) => {
//your custom page and before each test stuff
await use(custom_page)
},
});
export { expect } from '@playwright/test';
testfile.spec.ts
import {test, expect} from 'fixture'
test('my test 1', async ({ custom_page, loginPage}) => {
//assertion
});
test('my test 2', async ({loginPage}) => {
//assertion
});
};
If it confuses you let me know. here is a link to fixtures https://playwright.dev/docs/test-fixtures#using-a-fixture
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].