Junior — Middle
What design patterns are used for organizing the storage of locators in automation UI?
sobes.tech AI
Answer from AI
In UI automation for organizing locator storage, the following design patterns are often used:
-
Page Object — each screen or page of the application is represented by a separate class, where locators and interaction methods with elements are encapsulated. This improves readability and test maintenance.
-
Singleton — a singleton can be used to store and access locators or configurations to ensure a single source of data.
-
Factory — sometimes used to dynamically create page or element objects, especially if there are different page variants.
Example of a simple Page Object in Python with Selenium:
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username_locator = "#username"
self.password_locator = "#password"
self.login_button_locator = "#login"
def enter_username(self, username):
self.driver.find_element_by_css_selector(self.username_locator).send_keys(username)
def enter_password(self, password):
self.driver.find_element_by_css_selector(self.password_locator).send_keys(password)
def click_login(self):
self.driver.find_element_by_css_selector(self.login_button_locator).click()
This approach separates test logic from locator details and simplifies maintenance.