Locators Reusability | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Locators and Assertions
Instructor: Artem Bondar
Lesson Summary
This lesson focuses on the importance of reusing locators in automation testing to enhance code efficiency and maintainability. The presenter demonstrates how to assign locators to constants instead of copying and pasting them throughout the code. Key Steps in the Automation Process: Automate a basic form that includes fields for email and password , a checkbox , and a submit button . Initially, locators are defined and used multiple times, leading to inefficient code. To improve this, create a constant for the basic form locator: const basicForm = locator('Basic Form'); Replace repeated locator references with the new constant. Locator Usage and Assertions: Locators do not require await when defined as constants, as they are not promises. Demonstrated the creation of additional constants for specific fields, such as: const emailInputField = basicForm.locator('Email'); Introduced assertions using the expect function to validate input field values. In summary, by saving locators into constants, you can streamline your test code, reduce redundancy, and maintain clarity. This approach allows for easy updates and enhances the overall structure of your automation scripts.