|
| 1 | +import os |
| 2 | + |
| 3 | +from selenium import webdriver |
| 4 | +from selenium.webdriver.chrome.options import Options as ChromeOptions |
| 5 | +from selenium.webdriver.firefox.options import Options as FirefoxOptions |
| 6 | +from selenium.webdriver.support import expected_conditions as EC |
| 7 | +from selenium.webdriver.support.ui import WebDriverWait |
| 8 | + |
| 9 | +from ceph.UI.ids import ElementIDs |
| 10 | + |
| 11 | + |
| 12 | +class Browser: |
| 13 | + def __init__(self, browser_type: str = "chrome", headless: bool = False): |
| 14 | + """ |
| 15 | + Initializes the Browser with basic configurations. |
| 16 | + :param browser_type: The type of browser to use ('chrome', 'firefox'). Defaults to 'chrome'. |
| 17 | + :param headless: Whether to run the browser in headless mode. Defaults to False. |
| 18 | + """ |
| 19 | + self.browser_type = browser_type.lower() |
| 20 | + self.headless = headless |
| 21 | + self.driver = self._initialize_browser() |
| 22 | + self.element_ids = None # ADDED |
| 23 | + |
| 24 | + def _initialize_browser(self): |
| 25 | + """ |
| 26 | + Dynamically initializes the WebDriver based on the browser type. |
| 27 | + """ |
| 28 | + if self.browser_type == "chrome": |
| 29 | + return self._setup_chrome() |
| 30 | + elif self.browser_type == "firefox": |
| 31 | + return self._setup_firefox() |
| 32 | + else: |
| 33 | + raise ValueError( |
| 34 | + f"Unsupported browser type: {self.browser_type}. Supported browsers are 'chrome' and 'firefox'." |
| 35 | + ) |
| 36 | + |
| 37 | + def _apply_headless(self, options): |
| 38 | + """ |
| 39 | + Applies headless mode if enabled. |
| 40 | + :param options: Options object for the browser (ChromeOptions or FirefoxOptions). |
| 41 | + """ |
| 42 | + if self.headless: |
| 43 | + if isinstance(options, ChromeOptions): |
| 44 | + options.add_argument("--headless=new") |
| 45 | + else: |
| 46 | + options.add_argument("--headless") |
| 47 | + |
| 48 | + def _setup_chrome(self): |
| 49 | + """ |
| 50 | + Sets up the Chrome WebDriver with basic configurations. |
| 51 | + """ |
| 52 | + options = ChromeOptions() |
| 53 | + self._apply_headless(options) |
| 54 | + return webdriver.Chrome(options=options) |
| 55 | + |
| 56 | + def _setup_firefox(self): |
| 57 | + """ |
| 58 | + Sets up the Firefox WebDriver with basic configurations. |
| 59 | + """ |
| 60 | + options = FirefoxOptions() |
| 61 | + self._apply_headless(options) |
| 62 | + return webdriver.Firefox(options=options) |
| 63 | + |
| 64 | + def open(self, url: str): |
| 65 | + if not isinstance(url, str): |
| 66 | + raise ValueError("A valid URL must be provided.") |
| 67 | + self.driver.get(url) |
| 68 | + |
| 69 | + def quit(self): |
| 70 | + self.driver.quit() |
| 71 | + |
| 72 | + def load_elements(self, yaml_file_path: str): |
| 73 | + self.element_ids = ElementIDs(yaml_file_path) |
| 74 | + |
| 75 | + def is_element_visible(self, key: str, timeout: int = 10) -> bool: |
| 76 | + element = self.find_element(key, timeout) |
| 77 | + return element.is_displayed() if element else False |
| 78 | + |
| 79 | + def find_element(self, key: str, timeout: int = 10): |
| 80 | + if not self.element_ids: |
| 81 | + raise RuntimeError("ElementIDs not loaded. Use 'load_elements' first.") |
| 82 | + |
| 83 | + try: |
| 84 | + page, element = key.split(".") |
| 85 | + except ValueError: |
| 86 | + raise ValueError( |
| 87 | + "Invalid key format. Key must be in 'page.element' format." |
| 88 | + ) |
| 89 | + |
| 90 | + locator = self.element_ids.get_element(page, element) |
| 91 | + by_type_str = list(locator.keys())[ |
| 92 | + 0 |
| 93 | + ] # Get locator type string ("id", "xpath", etc.) |
| 94 | + locator_value = locator[by_type_str] # Get the locator value |
| 95 | + by_type = self.element_ids._get_by_type(by_type_str) # Get the By Type |
| 96 | + |
| 97 | + try: |
| 98 | + element = WebDriverWait(self.driver, timeout).until( |
| 99 | + EC.presence_of_element_located((by_type, locator_value)) |
| 100 | + ) |
| 101 | + return element |
| 102 | + except Exception as e: |
| 103 | + raise RuntimeError(f"Element with key '{key}' not found: {e}") |
| 104 | + |
| 105 | + def click(self, key: str, timeout: int = 10): |
| 106 | + element = self.find_element(key, timeout) |
| 107 | + WebDriverWait(self.driver, timeout).until(EC.element_to_be_clickable(element)) |
| 108 | + element.click() |
0 commit comments