𝗣𝗹𝗮𝘆𝘄𝗿𝗶𝗴𝗵𝘁 𝗕𝗲𝘀𝘁 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲𝘀: 𝟭𝟬 𝗥𝘂𝗹𝗲𝘀 𝗔𝗜 𝗔𝗴𝗲𝗻𝘁𝘀 𝗚𝗲𝘁 𝗪𝗿𝗼𝗻𝗴

AI agents can write 50 Playwright tests in one minute.

This speed is a trap.

Those tests often fail for no reason. AI agents copy old patterns from training data. They do not know your tests failed last night. They use stale habits.

Follow these 10 rules to keep your tests stable.

  1. Use role-based locators Use getByRole, getByLabel, or getByText. These match what a person sees. AI error: Agents use CSS or XPath. These break when you change a single class name.

  2. Use web-first assertions Use expect(locator).toBeVisible(). This auto-waits for the page. AI error: Agents add await page.waitForTimeout(3000). Hard waits cause flaky tests.

  3. Keep tests isolated Each test must start clean. Use hooks to set up state. AI error: Agents chain tests together. If test one fails, the whole file breaks.

  4. Seed data through the API Use the request fixture to send data directly to your backend. AI error: Agents click through ten UI screens to create one user. This makes tests slow and brittle.

  5. Prioritize roles over test IDs Use getByRole first. Use data-testid only as a fallback. AI error: Agents paste data-testid on everything. This misses bugs where a button has no label for screen readers.

  6. Set traces to record on retry Set trace to 'on-first-retry' in your config. AI error: Agents leave tracing off or leave it on for every run. One lacks evidence, the other slows everything down.

  7. Run in parallel Use parallel mode and sharding to split tests across machines. AI error: Agents write tests that share a single user. These tests fight each other in parallel.

  8. Remove if statements and try blocks A test should follow one path. If a button should be there, assert it. AI error: Agents wrap clicks in if statements to avoid errors. This hides real bugs.

  9. Test behavior, not internals Check the visible result on the screen. AI error: Agents assert on CSS classes or state variables. These break during every code refactor.

  10. Use projects for browser coverage Use the projects array to test Chromium, Firefox, and WebKit. AI error: Agents hard-code one browser. You miss bugs that only happen in Safari.

AI writes a fast first draft. It lacks wisdom.

A green test suite that proves nothing is dangerous. It creates false trust.

Laat de AI het concept schrijven. Gebruik een mens om deze regels toe te passen. De agent zorgt voor snelheid. De mens zorgt voor stabiliteit.

Bron: https://dev.to/aiwithanton/playwright-best-practices-10-rules-ai-agents-get-wrong-2026-5cii

Optionele leercommunity: https://t.me/GyaanSetuAi