from playwright.sync_api import Page, Locator, expect, TimeoutError from tools.logger import get_logger logger = get_logger("BASE_ELEMENT") class BaseElement: """Базовый класс для работы с элементами страницы.""" def __init__(self, page: Page, locator: str | Locator, name: str) -> None: self.page = page self.name = name self.locator: Locator if isinstance(locator, Locator): self.locator = locator elif isinstance(locator, str): self.locator = self.page.locator(locator) else: raise TypeError("locator value should be string type or Locator type") @property def type_of(self) -> str: return "base element" # Действия: def click(self) -> None: logger.info(f"Clicking {self.type_of} '{self.name}'") self.locator.click() def get_text(self, index: int) -> str: logger.info(f"Get text for {self.type_of} '{self.name}'") return self.locator.nth(index).text_content() def wait_for_element(self, timeout: int = 12000) -> None: logger.info(f"Wait for {self.type_of} '{self.name}'") self.locator.wait_for(timeout=timeout) # Проверки: def check_have_text(self, text: str, msg: str) -> None: logger.info(f"Check that {self.type_of} '{self.name}' has text '{text}'") expect(self.locator).to_have_text(text), msg def check_presence(self, msg: str) -> None: logger.info(f"Check that {self.type_of} '{self.name}' is present") print(self.locator) expect(self.locator).to_be_visible(visible=True, timeout=12000), msg def is_present(self, timeout: int = 5000) -> bool: logger.info(f"Check that {self.type_of} '{self.name}' is present") try: self.locator.wait_for(timeout=timeout) except TimeoutError: return False return True def is_not_present(self, timeout: int = 5000) -> bool: logger.info(f"Check that {self.type_of} '{self.name}' is missing") try: self.locator.wait_for(timeout=timeout) except TimeoutError: return True return False