Locators in Page Objects | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Page Objects
Instructor: Artem Bondar
Lesson Summary
This lesson discusses how to organize locators within page objects in Playwright. Two approaches are presented: the recommended method by Playwright and a personal preference. Playwright's Recommended Approach Playwright suggests separating locators from functional methods to enhance reusability and maintainability. Here’s how to implement this: Create fields for all locators, such as FormLayoutsMenuItem , and define them as read-only properties. Assign locator values within the constructor. Use these fields in functional methods instead of defining locators directly within them. This method adheres to the DRY principle (Don't Repeat Yourself), allowing locators to be reused across different methods. Personal Preference The instructor prefers keeping locators within functional methods, arguing that: It maintains simplicity, especially in smaller page objects. It reduces confusion when dealing with numerous locators, preventing duplication and dead locators. Debugging is easier as failures point directly to the line of code where the locator is used. The instructor emphasizes the KISS principle (Keep It Simple, Stupid), advocating for a straightforward approach that enhances code maintainability. In conclusion, while Playwright's method has its merits, the instructor finds that keeping locators within functional methods works better for their projects. The choice ultimately depends on personal or team preferences.