-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
102 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,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}" | ||
) |
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,52 @@ | ||
from dashboard import Dashboard | ||
|
||
|
||
class Operations: | ||
def __init__(self, browser_type: str): | ||
""" | ||
Initialize Operations with a browser type. | ||
:param browser_type: The type of browser to initialize ('chrome', 'firefox', etc.). | ||
""" | ||
self.dashboard = Dashboard(browser_type) | ||
|
||
def login( | ||
self, | ||
username: str, | ||
password: str, | ||
username_locator: str, | ||
password_locator: str, | ||
login_button_locator: str, | ||
username_locator_type: str = "id", | ||
password_locator_type: str = "id", | ||
login_button_locator_type: str = "id", | ||
): | ||
""" | ||
Performs a login operation using the provided credentials. | ||
:param username: The username for login. | ||
:param password: The password for login. | ||
:param username_locator: The locator for the username field. | ||
:param password_locator: The locator for the password field. | ||
:param login_button_locator: The locator for the login button. | ||
:param username_locator_type: The locator type for the username field ('id', 'name', 'class', 'xpath', 'css'). | ||
:param password_locator_type: The locator type for the password field ('id', 'name', 'class', 'xpath', 'css'). | ||
:param login_button_locator_type: The locator type for the login button ('id', 'name', 'class', 'xpath', 'css'). | ||
""" | ||
try: | ||
|
||
username_field = self.dashboard.find_element( | ||
username_locator, username_locator_type | ||
) | ||
username_field.clear() | ||
username_field.send_keys(username) | ||
|
||
password_field = self.dashboard.find_element( | ||
password_locator, password_locator_type | ||
) | ||
password_field.clear() | ||
password_field.send_keys(password) | ||
|
||
self.dashboard.click( | ||
login_button_locator, locator_type=login_button_locator_type | ||
) | ||
except Exception as e: | ||
raise RuntimeError(f"Login failed: {e}") |