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 operations for automation #4250

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
50 changes: 50 additions & 0 deletions ceph/UI/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


def capture_screen(driver, filename: str) -> str:
"""
Captures a screenshot and saves it to the specified filename.
:param driver: WebDriver instance.
:param filename: The filename to save the screenshot as.
:return: The path of the saved screenshot.
"""
try:
if driver.save_screenshot(filename):
return filename
else:
raise RuntimeError(f"Screenshot capture failed for file: {filename}")
except Exception as e:
raise RuntimeError(f"Failed to capture screenshot: {e}")


def wait_for_element(driver, locator: str, locator_type: str = "id", timeout: int = 10):
"""
Waits for a specific web element to become visible.
:param driver: WebDriver instance.
:param locator: The locator for the element.
:param locator_type: The type of locator ('id', 'name', 'class', 'xpath', 'css').
:param timeout: Maximum time to wait for the element to become visible (default: 10 seconds).
:return: The WebElement if found within the timeout period.
"""
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:
element = WebDriverWait(driver, timeout).until(
EC.visibility_of_element_located((by_types[locator_type], locator))
)
return element
except Exception as e:
raise RuntimeError(
f"Element with locator '{locator}' and type '{locator_type}' not visible within {timeout} seconds: {e}"
)
33 changes: 33 additions & 0 deletions ceph/UI/operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from ceph.UI.dashboard import Dashboard


class Operations:
def __init__(self, browser_type: str):
self.dashboard = Dashboard(browser_type)

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

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

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

def perform_click(self, element_key: str):
self.dashboard.click(element_key)

def perform_input(self, element_key: str, text: str):
element = self.dashboard.find_element(element_key)
element.clear()
element.send_keys(text)