e-nms_qa_automation/components/alert_component.py

89 lines
3.6 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, expect
from components.base_component import BaseComponent
from elements.text_element import Text
from tools.logger import get_logger
logger = get_logger("ALERT")
class AlertComponent(BaseComponent):
"""Компонент для работы с alert-окнами.
Поддерживает различные типы alert-окон: error, success, info, warning.
Атрибуты:
page: экземпляр страницы Playwright
alert_type: тип alert-окна (error/success/info/warning)
text: текстовый элемент сообщения alert-окна
"""
def __init__(self, page: Page, alert_type: str):
"""Инициализация компонента alert-окна.
Args:
page: экземпляр страницы Playwright
alert_type: тип alert-окна (error/success/info/warning)
Raises:
ValueError: если передан неподдерживаемый тип alert-окна
"""
super().__init__(page)
alert_types = ["error", "success", "info", "warning"]
if alert_type not in alert_types:
raise ValueError("Unsupported type of alert window")
self.alert_type = alert_type
self.text = Text(page, f"//div[@class='v-alert {self.alert_type}']/div", "Alert message")
# Действия:
def get_text(self) -> str:
"""Получение текста сообщения из alert-окна.
Returns:
str: текст сообщения alert-окна
"""
return self.text.get_text(0)
# Проверки:
def check_presence(self, text: str):
"""Проверка наличия alert-окна с заданным текстом.
Args:
text: текст для проверки (если пустая строка - проверяется только наличие окна)
Raises:
AssertionError: если alert-окно не найдено
"""
msg = f"No {self.alert_type} alert window on page"
if text == "":
expect(self.page.get_by_role("alert")).to_be_visible(), msg
else:
expect(self.page.get_by_role("alert").filter(has_text=text)).to_be_visible(), msg
def check_absence(self, text: str, timeout: int = 30000):
"""Проверка отсутствия alert-окна с заданным текстом.
Args:
text: текст для проверки
timeout: время ожидания исчезновения (в миллисекундах)
Raises:
AssertionError: если alert-окно не исчезает в течение заданного времени
"""
seconds = int(timeout/1000)
msg = f"Alert {self.alert_type} window should disappear after {seconds} seconds"
expect(self.page.get_by_role("alert").filter(has_text=text)).to_be_hidden(timeout=timeout), msg
def check_text(self, alert_text: str):
"""Проверка точного соответствия текста в alert-окне.
Args:
alert_text: ожидаемый текст сообщения
Raises:
AssertionError: если текст не соответствует ожидаемому
"""
self.text.check_have_text(alert_text, f"Unexpected message in alert {self.alert_type} window")