DEV Community

TestDino
TestDino

Posted on

Mistake 7/14: You are still using beforeEach for everything

If you are still copy-pasting beforeEachblocks across fifty spec files to initialize your Page Objects, you are building a maintenance trap.

The inconsistency isn't laziness. It is what happens when your setup has no enforced contract across the codebase.

BEFORE - Brittle boilerplate

// Every spec file repeats this logic
let settingsPage;

test.beforeEach(async ({ page }) => {
  settingsPage = new SettingsPage(page);
});
Enter fullscreen mode Exit fullscreen mode

AFTER - Scalable dependency injection

// fixtures.ts
export const test = base.extend({
  settingsPage: async ({ page }, use) => {
    await use(new SettingsPage(page));
  },
});
Enter fullscreen mode Exit fullscreen mode

Playwright built Fixtures to eliminate the "which pattern did we agree on?" conversation.

-Encapsulation: Setup and teardown live together in the fixture.

-Lazy Loading: If a test doesn't ask for the fixture, it never runs.

-Consistency: One source of truth for every Page Object in your framework.

Stop managing state manually. Let the runner do the heavy lifting. ⚡

Do you enforce a fixture strategy on your team, or is it a free-for-all? Drop it in the comments.

Top comments (0)