Best Practices with Playwright automation
If you are writing an automation script first time with a playwright then you must know the best practices. This will help you to have an expandable framework, fewer failures, and good readability of the code. I have been writing playwright scripts for almost one year and also we have playwright documentation for best practices. So I will put most of from combining the both.
Best Practices with Playwright Automation -
I have created a video tutorial on the same with the code demo, please go through the below video and subscribe to our YouTube channel.
- Page Object Model
- Locators - have a separate constructor to add the locators, so if you need to change it you need to change it at only one place.
- URLs - Also having a separate variable to store URLs will help, in case URL changes in the future
- Methods - Have a proper page object model that separates the methods from the script so you don't need to write the same code everywhere.
- Locators
- Avoid static Xpaths
- Avoid using class names in locators, if possible use data-testIds or the IDs or texts which won't change.
- Use locators instead of selectors - Don't write a script on the selectors-like page.click('locator') instead write like this page.locator('locator').click();
- Use double quotes in locators to find the exact element like page.getByText("Submit"). (Watch the full YouTube video for more details).
- Assertions
- Assert only if required
- Use of Soft Assertions - soft assertions are underused. It should be used more to know if something fails and also rest of the script gets executed wherever needed.
- Use of await
- Only run tests that are stable! - Include only a stable tests suite to execute in CI/CD or Daily runs that will give you more confidence.
- Tests should not be dependent on each other.
If you have any doubts or suggestions feel free to reach out on social media. Follow Us on Reddit, Facebook, and Twitter. Don't forget to subscribe to the channel -> Youtube channel. Thank You
No comments