I'm working with Playwright and TypeScript and have noticed that Page Object Models (POMs) always require a page parameter for initialization.
Since the page object is only available from the beforeEach block onwards, I'm forced to use this pattern:
test.describe('describe name', () => {
let potentialSnippetPage: PotentialSnippetPage
test.beforeEach(async ({ page }) => {
potentialSnippetPage = new PotentialSnippetPage(page);
});
test('test title', async () => {
await potentialSnippetPage.someMethod();
});
});
This approach feels cumbersome because I have to declare the POM variable with let first, then initialize it properly in the beforeEach block with the actual page object.
Is there a way to handle POM initialization in Playwright without beforeEach and let?
I'm looking for patterns that would reduce the repetitive setup while maintaining type safety.