Engineering Playwright Test Generation

Playwright Test Prompt

getByRole over CSS chains, auto-wait over sleep, web-first assertions — Playwright tests written the way Playwright wants.

Overview

Playwright tests fail two ways: flaky (sleeps and fragile selectors) or unmaintainable (CSS chains that break on every restyle). This setup generates the Playwright contract for an E2E journey with the framework's own discipline: locate by user-facing attributes (getByRole, getByLabel) so restyles don't break tests, never sleep — auto-waiting and web-first assertions do the waiting, every test starts from a clean context, and page objects only where flows are actually reused.

How to use this resource

  1. Generate with the journey preset

    E2E strategy + Playwright mode: journey philosophy plus framework discipline in one contract.

  2. Audit the locators

    Every getByRole the model produces is a test that survives a restyle; every CSS chain is a future failure.

  3. Reject any sleep

    The contract bans fixed waits — if generated code sleeps, regenerate with the contract quoted back.

Why This Works

  • User-facing locators align tests with what users do, not how the DOM is shaped today
  • Auto-wait discipline removes the #1 source of E2E flake at the contract level
  • Clean-context rules keep tests independent — parallelizable and debuggable

Best for

  • Teams adopting Playwright from Selenium or Cypress
  • E2E suites where flake is the main maintenance cost
  • Modern web apps with accessible, role-labeled UI

Not for

  • Selenium projects — switch the framework mode; the discipline differs (explicit waits, stale elements)
  • API testing — Playwright can do it, but the API strategy with generic mode contracts it better

Use cases

  • Generating Playwright journeys that survive UI restyles
  • Converting sleep-ridden specs into auto-waiting ones
  • Onboarding a team to Playwright's locator philosophy

FAQ

What flow do I paste into the Playwright test prompt?

Paste the flow under test into the CODE CONTEXT block — the page or component source for one realistic journey, like the onboarding sequence in the TESTING OBJECTIVE. Include the markup or handlers that expose roles and labels so the model can pick getByRole and getByLabel locators. Anything unspecified surfaces under the ASSUMPTIONS and GAPS headings rather than being invented.

How does the getByRole and auto-wait discipline in this prompt work?

The FRAMEWORK GUIDANCE section forces two rules on whatever assistant you run it in. Locators come from user-facing attributes — getByRole, getByLabel, getByText — so a restyle can't break them, and the contract bans fixed sleeps: web-first assertions like await expect(locator).toBeVisible() do the waiting. Each test also starts from a clean context, keeping runs independent and parallelizable.

Does a passing generated Playwright test mean the feature is correct?

Passing tests prove what they assert — the COVERAGE AREAS and FAILURE SCENARIOS the file actually checks — not that the feature is correct. The NON-GOALS bar the model from weakening assertions or touching the implementation, so it tests the code as-is. Take the file the assistant returns, fix what's wrong, and confirm it goes green in CI yourself.

More resources from Test Case Prompt Generator

Resources that pair well

Related tools

Tip: Save time by exploring related resources and tools that integrate with this resource.