from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from .base_page import BasePage class FormElementsPage(BasePage): """ Page Object for the Form Elements test page. Encapsulates all locators and actions related to this page. """ # --- Locators --- _TEXT_INPUT = (By.ID, "text-input") _CHECKBOX = (By.ID, "checkbox-input") _RADIO_OPTION_ONE = (By.ID, "r1") _RADIO_OPTION_TWO = (By.ID, "r2") _RADIO_OPTION_THREE = (By.ID, "r3") _SELECT_DROPDOWN = (By.ID, "select-input") _TEXTAREA = (By.ID, "textarea-input") _SWITCH = (By.ID, "switch-input") _SUBMIT_BUTTON = (By.XPATH, "//button[@type='submit']") _CANCEL_BUTTON = (By.XPATH, "//button[text()='Cancel']") _DISABLED_BUTTON = (By.XPATH, "//button[text()='Disabled']") def __init__(self, driver): """Initializes the FormElementsPage with the WebDriver.""" super().__init__(driver) self.page_path = "/form-elements" # --- Page Actions --- def open(self, base_url: str): """Navigates to the form elements page.""" super().open(base_url, self.page_path) def enter_text_in_input(self, text: str): """Enters text into the main text input field.""" self.send_keys(self._TEXT_INPUT, text) def get_text_from_input(self) -> str: """Gets the current value from the text input field.""" return self.find_element(self._TEXT_INPUT).get_attribute("value") def select_checkbox(self): """Clicks the checkbox to select it.""" self.click(self._CHECKBOX) def is_checkbox_selected(self) -> bool: """Checks if the checkbox is selected.""" checkbox = self.find_element(self._CHECKBOX) return checkbox.get_attribute("data-state") == "checked" def choose_radio_option(self, option: int): """ Selects a radio button option. :param option: 1 for Option One, 2 for Option Two, 3 for Option Three. """ if option == 1: self.click(self._RADIO_OPTION_ONE) elif option == 2: self.click(self._RADIO_OPTION_TWO) elif option == 3: self.click(self._RADIO_OPTION_THREE) else: raise ValueError("Invalid option number. Must be 1, 2, or 3.") def is_radio_option_selected(self, option: int) -> bool: """Checks if a specific radio button option is selected.""" locator = None if option == 1: locator = self._RADIO_OPTION_ONE elif option == 2: locator = self._RADIO_OPTION_TWO elif option == 3: locator = self._RADIO_OPTION_THREE else: return False radio_button = self.find_element(locator) return radio_button.get_attribute("data-state") == "checked" def select_fruit_by_visible_text(self, text: str): """ Selects an option from the custom shadcn dropdown by its visible text. :param text: The visible text of the option to select (e.g., "Apple"). """ # 1. Click the dropdown trigger to open the options self.click(self._SELECT_DROPDOWN) # 2. Define the locator for the desired option based on its text # The options are typically in a popover, so we wait for them to be visible. option_locator = (By.XPATH, f"//div[@role='option' and .//span[text()='{text}']]") # 3. Click the option self.click(option_locator) def get_selected_fruit(self) -> str: """Gets the currently selected value from the dropdown trigger.""" return self.get_text(self._SELECT_DROPDOWN) def enter_message_in_textarea(self, message: str): """Enters text into the textarea field.""" self.send_keys(self._TEXTAREA, message) def get_message_from_textarea(self) -> str: """Gets the current value from the textarea field.""" return self.find_element(self._TEXTAREA).get_attribute("value") def toggle_switch(self): """Clicks the switch to toggle its state.""" self.click(self._SWITCH) def is_switch_on(self) -> bool: """ Checks if the switch is in the 'on' state. This depends on how state is represented (e.g., aria-checked, class). For shadcn, it's often a data attribute `data-state`. """ switch_element = self.find_element(self._SWITCH) return switch_element.get_attribute("data-state") == "checked" def click_submit_button(self): """Clicks the submit button.""" self.click(self._SUBMIT_BUTTON) def is_disabled_button_enabled(self) -> bool: """Checks if the 'Disabled' button is enabled.""" return self.is_element_enabled(self._DISABLED_BUTTON)