Locators in Page Objects | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Page Objects
Instructor: Artem Bondar
Lesson Summary
This video lesson discusses the management of locators within page objects in Playwright. The instructor emphasizes that there is no single correct way to structure page objects and shares both Playwright's recommendations and their personal preferences. Key Concepts Page Object Structure: The Playwright documentation suggests creating read-only fields for locators in the constructor to avoid duplication across methods. Refactoring Example: The instructor demonstrates how to refactor a navigation page by creating fields for locators and using them in methods. Concerns with Playwright's Approach Readability Issues: As the number of locators grows, the constructor can become cluttered, making it hard to track existing locators. Duplication Risk: Different engineers may inadvertently create duplicate locators under different names due to a lack of clarity. Stale Locators: Unused locators may accumulate over time, leading to unnecessary clutter in the code. Dynamic Locators: Parameterized locators cannot be stored as fields, resulting in a mix of locator types within the class. Instructor's Preference The instructor prefers to define locators directly within methods, arguing that it simplifies maintenance and enhances clarity. They believe this approach minimizes boilerplate code and reduces confusion. Ultimately, the choice of approach is left to the viewer, with the instructor advocating for simplicity and clarity in code structure.