๐ญ๐ฌ ๐ฃ๐๐๐ฌ๐ช๐ฅ๐๐๐๐ง ๐ง๐๐ฃ๐ฆ ๐๐ข๐ฅ ๐๐๐ง๐ง๐๐ฅ ๐ง๐๐ฆ๐ง๐ฆ
I moved 200 tests from WDIO to Playwright. It took four days. I made mistakes early on. I wrote tests the Selenium way.
Here are 10 tips to improve your tests:
- Stop using waitForTimeout. Playwright waits for elements. Use waitForSelector for specific needs.
- Use getByRole. Stop using CSS selectors. They break when class names change. Role locators match user behavior.
- Keep Page Object Models lean. Use one class per page. Use actions as methods. Use locators as properties.
- Mock API responses. Use network interception. UI tests should not fail because of slow APIs.
- Use traces. Debugging in CI takes minutes. You get a full timeline of actions.
- Use test.describe and test.use. Run tests for different user states. Avoid duplicating code.
- Log in once. Reuse the session state. Your suite runs faster.
- Use soft assertions. Check multiple things on one page. See all failures in one run.
- Tag your tests. Use @smoke for commits. Use @regression for nightly runs.
- Use codegen to start. Use it to find locators. Refactor the code. Do not use it for final tests.
These tips took months to learn. Start using them today. Build a suite your team trusts.
Source: https://dev.to/sshhfaiz/10-playwright-tips-that-will-change-how-you-write-tests-4mbj