Skip to content

Commit

Permalink
Add dashboard automation support for enhanced functionality
Browse files Browse the repository at this point in the history
Signed-off-by: nrao <[email protected]>
  • Loading branch information
NamrathaRao24 committed Jan 28, 2025
1 parent 4083994 commit a59971b
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 96 deletions.
85 changes: 85 additions & 0 deletions ceph/UI/browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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

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.")
return self.element_ids.get_element(key, self.driver, timeout)

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()
23 changes: 0 additions & 23 deletions ceph/UI/config/login.yaml

This file was deleted.

69 changes: 69 additions & 0 deletions ceph/UI/dashboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from ceph.UI.browser import Browser
from ceph.UI.ids import ElementIDs


class Dashboard(Browser):
def __init__(self, browser_type: str = "chrome"):
super().__init__(browser_type=browser_type)
self.element_ids = None # ElementIDs instance will be set dynamically

def login(self, username: str, password: str):
username_key, password_key, login_button_key = self.get_username_login()
username_field = self.find_element(username_key)
username_field.clear()
username_field.send_keys(username)

password_field = self.find_element(password_key)
password_field.clear()
password_field.send_keys(password)

self.click(login_button_key)
print("Login successful!")

def _get_common(self):
sign_in_button = {
"xpath": "//button[contains(text(), 'Sign In')]",
"id": "sign-in",
}

sign_up_button = {
"xpath": "//button[contains(text(), 'Sign Up')]",
"id": "sign-up",
}
return sign_in_button, sign_up_button

def get_username_login(self):
login_username = {
"id": "username",
"xpath": "//input[contains(@placeholder, 'username')]",
}

login_password = {
"id": "password",
"xpath": "//input[contains(@placeholder, 'password')]",
}

login_button = {"id": "submit", "xpath": "//button[contains(text(), 'Login')]"}
return login_username, login_password, login_button

def get_email_login(self):

login_username = {"name": "email", "xpath": "//input[@type='email']"}
login_password = {"name": "pass", "xpath": "//input[@type='password']"}
login_button = {"xpath": "//button[@type='submit']"}
sign_up_email = {"xpath": "//input[@type='email']"}
sign_up_password = {"xpath": "//input[@type='password']"}
sign_up_button = {"xpath": "//button[contains(text(), 'Register')]"}
return (
login_username,
login_password,
login_button,
sign_up_email,
sign_up_password,
sign_up_button,
)
34 changes: 0 additions & 34 deletions ceph/UI/ids.py

This file was deleted.

39 changes: 0 additions & 39 deletions ceph/UI/utilities.py

This file was deleted.

0 comments on commit a59971b

Please sign in to comment.