diff --git a/components/alert_component.py b/components/alert_component.py index 966c286..98547ba 100644 --- a/components/alert_component.py +++ b/components/alert_component.py @@ -6,9 +6,9 @@ alert-окон (error, success, info, warning) и проверки их сост from playwright.sync_api import Page, expect from tools.logger import get_logger +from locators.alert_locators import AlertLocators from elements.text_element import Text from components.base_component import BaseComponent -from locators.alert_locators import AlertLocators logger = get_logger("ALERT") @@ -32,6 +32,38 @@ class AlertComponent(BaseComponent): self.text = Text(page, AlertLocators.ALERT_MESSAGE, "Alert message") # Действия: + def close_alert_by_text(self, text: str) -> None: + """Закрывает alert-окно с заданным текстом с помощью кнопки закрытия. + + Args: + text: Текст alert-окна, которое нужно закрыть. + + Raises: + AssertionError: Если не удалось найти или закрыть alert-окно. + """ + # Находим alert с нужным текстом + alert_locator = self.page.get_by_role( + AlertLocators.ALERT_ROLE + ).filter(has_text=text) + + # Проверяем, что alert видим + expect(alert_locator).to_be_visible() + + # Находим кнопку закрытия внутри alert + close_button = alert_locator.locator(AlertLocators.ALERT_DISMISS_BUTTON) + + # Проверяем, что кнопка закрытия доступна и кликаем + expect(close_button).to_be_visible() + expect(close_button).to_be_enabled() + + # Кликаем по кнопке закрытия + close_button.click() + + # Проверяем, что alert исчез после закрытия + expect(alert_locator).to_be_hidden() + + logger.info(f"Alert with text '{text}' closed successfully") + def get_alert_type(self) -> str: """Возвращает тип alert-окна. @@ -42,7 +74,9 @@ class AlertComponent(BaseComponent): ValueError: Если получен неподдерживаемый тип alert-окна. """ - class_attr = self.page.get_by_role(AlertLocators.ALERT_ROLE).locator('>div').get_attribute('class') + class_attr = self.page.get_by_role(AlertLocators.ALERT_ROLE).locator( + '>div' + ).get_attribute('class') alert_type = None if 'v-alert' in class_attr: @@ -63,56 +97,7 @@ class AlertComponent(BaseComponent): return self.text.get_text(0) - def close_alert_by_text(self, text: str) -> None: - """Закрывает alert-окно с заданным текстом с помощью кнопки закрытия. - - Args: - text: Текст alert-окна, которое нужно закрыть. - - Raises: - AssertionError: Если не удалось найти или закрыть alert-окно. - """ - # Находим alert с нужным текстом - alert_locator = self.page.get_by_role(AlertLocators.ALERT_ROLE).filter(has_text=text) - - # Проверяем, что alert видим - expect(alert_locator).to_be_visible() - - # Находим кнопку закрытия внутри alert - close_button = alert_locator.locator(AlertLocators.ALERT_DISMISS_BUTTON) - - # Проверяем, что кнопка закрытия доступна и кликаем - expect(close_button).to_be_visible() - expect(close_button).to_be_enabled() - - # Кликаем по кнопке закрытия - close_button.click() - - # Проверяем, что alert исчез после закрытия - expect(alert_locator).to_be_hidden() - - logger.info(f"Alert with text '{text}' closed successfully") - # Проверки: - def check_alert_presence(self, text: str) -> None: - """Проверяет наличие alert-окна с заданным текстом. - - Args: - text: Текст для проверки. Если пустая строка - проверяет только - наличие окна. - - Raises: - AssertionError: Если alert-окно не найдено. - """ - - msg = "Alert window is missing" - if text == "": - expect(self.page.get_by_role(AlertLocators.ALERT_ROLE)).to_be_visible(), msg - logger.info("Alert window successfully displayed") - else: - expect(self.page.get_by_role(AlertLocators.ALERT_ROLE).filter(has_text=text)).to_be_visible(), msg - logger.info(f"Alert window with text '{text}' successfully displayed") - def check_alert_absence(self, text: str, timeout: int = 30000) -> None: """Проверяет отсутствие alert-окна с заданным текстом. @@ -128,12 +113,37 @@ class AlertComponent(BaseComponent): msg = f"Alert window should disappear after {seconds} seconds" if text == "": - expect(self.page.get_by_role(AlertLocators.ALERT_ROLE)).to_be_hidden(timeout=timeout), msg - logger.info("Alert window successfully disappeared") + expect(self.page.get_by_role( + AlertLocators.ALERT_ROLE + )).to_be_hidden(timeout=timeout), msg + logger.info(f"Alert window successfully disappeared") else: - expect(self.page.get_by_role(AlertLocators.ALERT_ROLE).filter(has_text=text)).to_be_hidden(timeout=timeout), msg + expect(self.page.get_by_role( + AlertLocators.ALERT_ROLE + ).filter(has_text=text)).to_be_hidden(timeout=timeout), msg logger.info(f"Alert window with text '{text}' successfully disappeared") + def check_alert_presence(self, text: str) -> None: + """Проверяет наличие alert-окна с заданным текстом. + + Args: + text: Текст для проверки. Если пустая строка - проверяет только + наличие окна. + + Raises: + AssertionError: Если alert-окно не найдено. + """ + + msg = "Alert window is missing" + if text == "": + expect(self.page.get_by_role(AlertLocators.ALERT_ROLE)).to_be_visible(), msg + logger.info(f"Alert window successfully displayed") + else: + expect(self.page.get_by_role( + AlertLocators.ALERT_ROLE + ).filter(has_text=text)).to_be_visible(), msg + logger.info(f"Alert window with text '{text}' successfully displayed") + def check_text(self, alert_text: str) -> None: """Проверяет точное соответствие текста в alert-окне.