e-nms_qa_automation/elements/base_element.py

65 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from playwright.sync_api import Page, Locator, expect, TimeoutError
from typing import Optional
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