Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dashboard automation support #4249

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions ceph/UI/browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import os

from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from ceph.UI.ids import ElementIDs


class Browser:
def __init__(self, browser_type: str = "chrome", headless: bool = False):
"""
Initializes the Browser with basic configurations.
:param browser_type: The type of browser to use ('chrome', 'firefox'). Defaults to 'chrome'.
:param headless: Whether to run the browser in headless mode. Defaults to False.
"""
self.browser_type = browser_type.lower()
self.headless = headless
self.driver = self._initialize_browser()
self.element_ids = None # ADDED

def _initialize_browser(self):
"""
Dynamically initializes the WebDriver based on the browser type.
"""
if self.browser_type == "chrome":
return self._setup_chrome()
elif self.browser_type == "firefox":
return self._setup_firefox()
else:
raise ValueError(
f"Unsupported browser type: {self.browser_type}. Supported browsers are 'chrome' and 'firefox'."
)

def _apply_headless(self, options):
"""
Applies headless mode if enabled.
:param options: Options object for the browser (ChromeOptions or FirefoxOptions).
"""
if self.headless:
if isinstance(options, ChromeOptions):
options.add_argument("--headless=new")
else:
options.add_argument("--headless")

def _setup_chrome(self):
"""
Sets up the Chrome WebDriver with basic configurations.
"""
options = ChromeOptions()
self._apply_headless(options)
return webdriver.Chrome(options=options)

def _setup_firefox(self):
"""
Sets up the Firefox WebDriver with basic configurations.
"""
options = FirefoxOptions()
self._apply_headless(options)
return webdriver.Firefox(options=options)

def open(self, url: str):
if not isinstance(url, str):
raise ValueError("A valid URL must be provided.")
self.driver.get(url)

def quit(self):
self.driver.quit()

def load_elements(self, yaml_file_path: str):
self.element_ids = ElementIDs(yaml_file_path)

def is_element_visible(self, key: str, timeout: int = 10) -> bool:
element = self.find_element(key, timeout)
return element.is_displayed() if element else False

def find_element(self, key: str, timeout: int = 10):
if not self.element_ids:
raise RuntimeError("ElementIDs not loaded. Use 'load_elements' first.")

try:
page, element = key.split(".")
except ValueError:
raise ValueError(
"Invalid key format. Key must be in 'page.element' format."
)

locator = self.element_ids.get_element(page, element)
by_type_str = list(locator.keys())[
0
] # Get locator type string ("id", "xpath", etc.)
locator_value = locator[by_type_str] # Get the locator value
by_type = self.element_ids._get_by_type(by_type_str) # Get the By Type

try:
element = WebDriverWait(self.driver, timeout).until(
EC.presence_of_element_located((by_type, locator_value))
)
return element
except Exception as e:
raise RuntimeError(f"Element with key '{key}' not found: {e}")

def click(self, key: str, timeout: int = 10):
element = self.find_element(key, timeout)
WebDriverWait(self.driver, timeout).until(EC.element_to_be_clickable(element))
element.click()
31 changes: 31 additions & 0 deletions ceph/UI/dashboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from browser import Browser
from ids import ElementIDs


class Dashboard(Browser):
def __init__(self, yaml_file_path):
super().__init__()
self.element_ids = ElementIDs(yaml_file_path)

def login(self, username=None, email=None, password=None):
"""Logs into the Ceph dashboard using either username or email."""
if email:
login_type = "email_login"
self.enter_text(
self.element_ids.get_element(login_type, "email")["xpath"], email
)
elif username:
login_type = "username_login"
self.enter_text(
self.element_ids.get_element(login_type, "username")["xpath"], username
)
else:
raise ValueError("Either username or email must be provided!")

self.enter_text(
self.element_ids.get_element(login_type, "password")["xpath"], password
)
self.click_element(
self.element_ids.get_element(login_type, "login_button")["xpath"]
)
print("Login successful!")