Playwright Anti-patterns and Code Smells

πŸ“‹ Table of Contents β–Ύ
  1. Common Playwright Anti-patterns
  2. Common Mistakes

Over time, certain bad habits and shortcuts can creep into a Playwright suite. Recognising anti-patterns early helps you keep tests fast, reliable and maintainable.

Common Playwright Anti-patterns

Examples include overusing hard waits, relying on brittle selectors, mixing concerns across layers and writing overly long tests.

// Examples of anti-patterns
// 1. Hard-coded waits
await page.waitForTimeout(5000); // ❌ avoid when possible

// 2. Brittle CSS selectors
await page.locator('div:nth-child(3) > span.button'); // ❌ fragile

// 3. Giant tests covering many flows
// ❌ difficult to debug and maintain
Note: Replacing these patterns with locators, auto-waiting assertions, fixtures and page models usually improves both readability and stability.
Tip: Review the suite periodically for smells such as repeated waitForTimeout calls or selectors that depend on visual structure.
Warning: Ignoring early warning signs leads to a gradual decline in test quality and confidence.

Agreeing on a short list of β€œbanned patterns” can help teams avoid reintroducing them.

Common Mistakes

Mistake 1 β€” Accepting flaky tests as normal

This lowers standards.

❌ Wrong: Marking tests as flaky without investigation.

βœ… Correct: Use flakiness as a signal to improve design or infrastructure.

Mistake 2 β€” Ignoring maintainability when adding tests

This creates long-term pain.

❌ Wrong: Optimising only for speed of writing, not readability.

βœ… Correct: Invest in patterns that keep the suite healthy over time.

🧠 Test Yourself

Which practice is a clear Playwright anti-pattern?