playwright best practices

🎭 Playwright Best Practices: Or How Not to Rage-Click Your Way Through End-to-End Tests

Ah, Playwright! The magical tool that lets us test our apps like a robot ninja with superhuman clicking speed. But with great power comes… well, some really flaky tests if you’re not careful. So buckle up, because we’re diving into some best practices for using Playwright without losing your sanity or your CI pipeline.

🎯 1. Use await Like Your Life Depends on It

Playwright is async. Like, really async. Forgetting await is like sending your friend to get coffee and starting a Zoom meeting without them.

Bad:

jsCopyEditpage.click('#login-button') // Uh-oh...

Good:

jsCopyEditawait page.click('#login-button') // Much better

Your tests will thank you. And so will your teammates. Probably with cookies.


🕰️ 2. Don’t Sleep on sleep

Seriously. Don’t use sleep.

jsCopyEditawait page.waitForTimeout(5000) // This is the testing equivalent of a nap in traffic.

Instead, be smart:

jsCopyEditawait page.waitForSelector('#success-toast', { state: 'visible' })

Use Playwright’s waiting mechanisms. They’re like fancy butlers who know exactly when to open the door.


🔍 3. Test What Matters, Not What’s Cool

It’s easy to get carried away and test everything. Every. Single. Button.

But please — test user flows, not pixels.

✅ Login
✅ Add item to cart
✅ Checkout
❌ Button color is cornflower blue

Visual regression is cool, but don’t confuse “cool” with “critical.” You’re not testing a fashion show.


📐 4. Keep Selectors Simple and Stable

If your selector looks like this:

jsCopyEditpage.locator('div > :nth-child(3) > span.some-class[data-test-id="maybe-this"]')

…you might be in trouble.

Use test IDs like a civilized developer:

jsCopyEditpage.getByTestId('login-button')

And if you don’t have test IDs? Add them. Future you will high-five present you.


🧹 5. Clean Up After Yourself

Every test should be like a guest at a party: leave things how they found them.

That means:

  • Clear cookies
  • Reset database (if needed)
  • Use isolated test accounts

Don’t let Test #27 fail because Test #26 forgot to do the dishes.


🚀 6. Parallelize Like a Pro

Playwright runs tests in parallel out of the box. Embrace it! Use .only and .skip wisely during debugging, but don’t forget to remove them, or you’ll spend hours wondering why only one test is running.

Also: use isolated contexts if you’re testing the same app in multiple tabs or users.

jsCopyEditconst context = await browser.newContext()

Boom. Instant sandbox.


🤖 7. Embrace CI (and Pray to the Pipeline Gods)

CI is your bestie. Set up your GitHub Actions, GitLab Pipelines, Jenkins, or whatever your team uses.

Pro tip: Run Playwright in headless mode, but also record videos/screenshots of failures. Debugging without visuals is like playing charades with your terminal.


🎉 Final Thoughts

Playwright is powerful, fast, and surprisingly fun — if you follow best practices. Treat your tests like code (because they are code), keep them clean, and remember: flaky tests aren’t funny… unless you enjoy 2AM Slack messages from QA.

Happy testing! 🎭✨

Article Writter By

Long Phạm

"Thành công nuôi dưỡng sự hoàn hảo. Sự hoàn hảo lại nuôi lớn thất bại. Chỉ có trí tưởng tượng mới tồn tại."