CP_AUTOMATION/pages/base_page.py
2025-12-12 19:54:54 +05:30

176 lines
4.5 KiB
Python

"""
Base Page Object Model class for Cognitive Prism Automation Tests
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from utils.wait_helpers import WaitHelpers
from config.config import EXPLICIT_WAIT, BASE_URL
class BasePage:
"""Base class for all page objects"""
def __init__(self, driver):
"""
Initialize base page
Args:
driver: WebDriver instance
"""
self.driver = driver
self.wait = WaitHelpers(driver)
self.base_url = BASE_URL
def navigate_to(self, url):
"""
Navigate to a URL
Args:
url: URL to navigate to
"""
self.driver.get(url)
self.wait_for_page_load()
def wait_for_page_load(self):
"""Wait for page to load completely"""
self.wait.wait_for_page_load()
self.wait.wait_for_loading_to_disappear()
def get_current_url(self):
"""Get current page URL"""
return self.driver.current_url
def get_page_title(self):
"""Get page title"""
return self.driver.title
def find_element(self, locator):
"""
Find element with explicit wait
Args:
locator: Tuple of (By, locator_string)
Returns:
WebElement: Found element
"""
return self.wait.wait_for_element_present(locator)
def find_clickable_element(self, locator):
"""
Find clickable element
Args:
locator: Tuple of (By, locator_string)
Returns:
WebElement: Clickable element
"""
return self.wait.wait_for_element_clickable(locator)
def click_element(self, locator):
"""
Click on element
Args:
locator: Tuple of (By, locator_string)
"""
element = self.find_clickable_element(locator)
element.click()
def send_keys(self, locator, text):
"""
Send keys to element
Args:
locator: Tuple of (By, locator_string)
text: Text to send
"""
element = self.find_clickable_element(locator)
element.clear()
element.send_keys(text)
def get_text(self, locator):
"""
Get text from element
Args:
locator: Tuple of (By, locator_string)
Returns:
str: Element text
"""
element = self.find_element(locator)
return element.text
def is_element_present(self, locator, timeout=5):
"""
Check if element is present
Args:
locator: Tuple of (By, locator_string)
timeout: Timeout in seconds
Returns:
bool: True if element is present
"""
try:
WebDriverWait(self.driver, timeout).until(
EC.presence_of_element_located(locator)
)
return True
except TimeoutException:
return False
def is_element_visible(self, locator, timeout=5):
"""
Check if element is visible
Args:
locator: Tuple of (By, locator_string)
timeout: Timeout in seconds
Returns:
bool: True if element is visible
"""
try:
WebDriverWait(self.driver, timeout).until(
EC.visibility_of_element_located(locator)
)
return True
except TimeoutException:
return False
def wait_for_text(self, locator, text, timeout=EXPLICIT_WAIT):
"""
Wait for specific text in element
Args:
locator: Tuple of (By, locator_string)
text: Text to wait for
timeout: Timeout in seconds
"""
WebDriverWait(self.driver, timeout).until(
EC.text_to_be_present_in_element(locator, text)
)
def take_screenshot(self, filename):
"""
Take screenshot
Args:
filename: Name of screenshot file
"""
from config.config import SCREENSHOT_DIR
screenshot_path = SCREENSHOT_DIR / filename
self.driver.save_screenshot(str(screenshot_path))
return screenshot_path