agentleFS
bartstcbartstc/vite-ts-react-template/.agents/skills/playwright-page-objects/SKILL.md

playwright-page-objects

Skill122 starsChanged 37 days agoMITView on GitHub

Use when creating page objects or refactoring Playwright E2E tests for better maintainability with Page Object Model patterns.

The file

Files
SKILL.md

Playwright Page Objects

Modern POM architecture using Playwright's fixtures pattern, composition over inheritance, and role-based locators.

When to Apply

  • Setting up E2E test architecture from scratch
  • Creating new page objects or component helpers
  • Implementing test fixtures for dependency injection
  • Refactoring existing page objects for maintainability
  • Writing reusable component-level test abstractions

Core Principles

  1. Fixtures over PageManager — On-demand instantiation, automatic setup/teardown, native TypeScript inference
  2. Composition over inheritance — Compose from focused components, avoid deep class hierarchies
  3. Role-based selectors first — getByRole() > getByLabel() > getByText() > getByTestId()
  4. Tests own assertions — Page objects expose state via getters, tests make assertions
  5. Separate pages by workflow — ProductListPage ≠ ProductDetailsPage (different user interactions)
  6. Readonly locators — All page object locator properties should be readonly
  7. Fluent interfaces — Methods return this for chaining; navigation methods return target page object
  8. Private internals — Mark implementation details (spinners, retry logic, helpers) as private
  9. Wait for stability — Use waitForPageLoad(), waitForResponse() before assertions
  10. High-level actions — Create AppActions class for complex multi-page user flows

Quick Reference

PatternWhen to UseReference
FixturesAlways for page object instantiationfixtures-pattern.md
Basic POMAll page objectscore-patterns.md
CompositionShared UI elements (header, cards, modals)core-patterns.md
Waiting strategiesLoading states, API responsescore-patterns.md
High-level actionsMulti-page flows (checkout, onboarding)core-patterns.md
Form handlingMulti-step forms, validationform-handling.md

Locator Priority

1. page.getByRole('button', { name: 'Add to Cart' })  // Preferred
2. page.getByLabel('Email')                           // Form inputs
3. page.getByText('Welcome back')                     // Non-interactive
4. page.getByPlaceholder('Search...')                 // When no label
5. page.getByTestId('cart-item-count')                // Escape hatch

Anti-Patterns Summary

Anti-PatternProblemSolution
Assertions in POMsHidden test logicExpose state, assert in tests
Fat page objects50+ locators unmaintainableSplit into components
Deep inheritanceRigid, hard to modifyUse composition
CSS selectorsBreak on styling changesUse role-based selectors
Wrapping PlaywrightUnnecessary abstractionUse Playwright directly
All members publicExposes internalsMark internal members private

References

  • Fixtures Pattern — Setup and dependency injection
  • Core Patterns — Basic POM, composition, locators, waiting, high-level actions
  • Form Handling — Multi-step flows, validation components
  • Anti-Patterns — What to avoid

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.