-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UI locators and utility functions for login automation
Signed-off-by: nrao <[email protected]>
- Loading branch information
1 parent
898bae6
commit 4083994
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |