Skip to content

Commit

Permalink
Add UI locators and utility functions for login automation
Browse files Browse the repository at this point in the history
Signed-off-by: nrao <[email protected]>
  • Loading branch information
NamrathaRao24 committed Dec 10, 2024
1 parent 898bae6 commit 4083994
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ceph/UI/config/login.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
login_username:
id: "username"
name: "username"
xpath: "//input[@name='username']"
css: "#username"

login_password:
id: "password"
name: "password"
xpath: "//input[@name='password']"
css: "#password"

login_button:
id: "login-btn"
name: "login"
xpath: "//button[@type='submit']"
css: ".btn-login"

search_box:
id: "search"
name: "q"
xpath: "//input[@name='q']"
css: ".search-box"
34 changes: 34 additions & 0 deletions ceph/UI/ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import yaml


class ElementIDs:
def __init__(self, yaml_file_path: str):
"""
Initialize ElementIDs with the path to the YAML file containing element identifiers.
:param yaml_file_path: Path to the YAML file.
"""
self.yaml_file_path = yaml_file_path
self.elements = self._load_ids()

def _load_ids(self) -> dict:
"""
Load element IDs from a YAML file.
:return: A dictionary of element identifiers.
"""
try:
with open(self.yaml_file_path, "r") as file:
return yaml.safe_load(file)
except FileNotFoundError:
raise RuntimeError(f"YAML file not found: {self.yaml_file_path}")
except yaml.YAMLError as e:
raise RuntimeError(f"Error parsing YAML file: {e}")

def get_element(self, key: str) -> dict:
"""
Get the locator types for a given key.
:param key: The key for the desired element.
:return: A dictionary with locator types (id, name, xpath, etc.) for the element.
"""
if key not in self.elements:
raise KeyError(f"Element key '{key}' not found in YAML.")
return self.elements[key]
39 changes: 39 additions & 0 deletions ceph/UI/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from selenium.webdriver.common.by import By


def read_table(driver, table_ref: str, locator_type: str = "id") -> list:
"""
Reads the data from a table on a webpage.
:param driver: WebDriver instance.
:param table_ref: The reference to locate the table (e.g., ID, class, XPath, etc.).
:param locator_type: The type of locator ('id', 'name', 'class', 'xpath', 'css'). Defaults to 'id'.
:return: A list of lists, where each sublist represents a row in the table.
"""
by_types = {
"id": By.ID,
"name": By.NAME,
"class": By.CLASS_NAME,
"xpath": By.XPATH,
"css": By.CSS_SELECTOR,
}

if locator_type not in by_types:
raise ValueError(f"Unsupported locator type: {locator_type}")

try:

table_element = driver.find_element(by_types[locator_type], table_ref)

rows = table_element.find_elements(By.TAG_NAME, "tr")

table_data = []
for row in rows:
cells = row.find_elements(By.TAG_NAME, "td")
if not cells:
cells = row.find_elements(By.TAG_NAME, "th")
row_data = [cell.text.strip() for cell in cells]
table_data.append(row_data)

return table_data
except Exception as e:
raise RuntimeError(f"Failed to read table with reference '{table_ref}': {e}")

0 comments on commit 4083994

Please sign in to comment.