e-nms_qa_automation/components_derived/modal_edit_user.py

294 lines
12 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.

"""Модуль modal_edit_user содержит класс для работы с окном редактирования пользователя.
Класс EditUserModalWindow наследует базовый функционал ModalWindowComponent
и реализует методы для редактирования данных пользователя.
"""
import re
from playwright.sync_api import Page
from tools.logger import get_logger
from locators.modal_window_locators import ModalWindowLocators
from elements.text_input_element import TextInput
from elements.text_element import Text
from elements.checkbox_element import Checkbox
from components.modal_window_component import ModalWindowComponent
from components.dropdown_list_component import DropdownList
from components.confirm_component import ConfirmComponent
logger = get_logger("EDIT_USER_MODAL_WINDOW")
class EditUserModalWindow(ModalWindowComponent):
"""Модальное окно редактирования пользователя.
Наследует ModalWindowComponent и добавляет:
- Поля редактирования данных
- Чекбоксы настроек
- Выпадающий список ролей
- Кнопки действий (Сохранить, Удалить и др.)
"""
def __init__(self, page: Page, user_name: str):
"""Инициализирует элементы формы редактирования пользователя."""
super().__init__(page)
# Локаторы элементов формы
input_form_locator = ModalWindowLocators.INPUT_FORM_USER_DATA
# Настройка заголовка и кнопки закрытия
self.window_title = user_name
locator_button_toolbar_close = (
self.page.get_by_role("navigation")
.filter(has_text=re.compile(self.window_title))
.get_by_role("button")
)
self.add_toolbar_title(self.window_title)
self.add_toolbar_button(locator_button_toolbar_close, "close")
# Добавление полей формы
elements_locators = self.get_input_fields_locators(
self.page.locator(ModalWindowLocators.INPUT_FORM_USER_DATA))
# Поле Имя
loc = elements_locators.get("Имя").locator(ModalWindowLocators.INPUT_FORM_USER_DATA_FIELD_NAME)
name_input = TextInput(page, loc, "name_input")
self.add_content_item("name_input", name_input)
# Поле Роль
role_loc = elements_locators.get("Роль").get_by_role("combobox").first
role_input = TextInput(page, role_loc, "role_input")
self.add_content_item("role_input", role_input)
self.add_content_item("roles_list", DropdownList(page))
# Поле Комментарий
loc = elements_locators.get("Комментарий").locator(ModalWindowLocators.INPUT_FORM_USER_DATA_FIELD_COMMENT)
commentary_input = TextInput(page, loc, "commentary_input")
self.add_content_item("commentary_input", commentary_input)
# Поле E-mail
loc = elements_locators.get("E-mail").locator(ModalWindowLocators.INPUT_FORM_USER_DATA_FIELD_EMAIL)
email_input = TextInput(page, loc, "email_input")
self.add_content_item("email_input", email_input)
# Поле Номер для СМС
loc = elements_locators.get("Номер для СМС").locator(ModalWindowLocators.INPUT_FORM_USER_DATA_FIELD_SMS)
phone_input = TextInput(page, loc, "phone_input")
self.add_content_item("phone_input", phone_input)
# Добавление чекбоксов и их меток
# Метка "Блокировка"
label_blocking_locator = self.page.locator(input_form_locator). \
locator("//label").get_by_text("Блокировка")
label_blocking = Text(
page,
label_blocking_locator,
"blocking_checkbox_label"
)
self.add_content_item("blocking_checkbox_label", label_blocking)
# Чекбокс "Блокировка"
checkbox_blocking = Checkbox(
page,
page.locator(input_form_locator).locator(ModalWindowLocators.INPUT_FORM_USER_DATA_CHECKBOX_BLOCKED),
"blocking"
)
self.add_content_item("blocking_checkbox", checkbox_blocking)
# Метка "Подписка на Push-уведомления"
label_push_locator = self.page.locator(input_form_locator). \
locator("//label").get_by_text("Подписка на Push-уведомления")
label_push = Text(
page,
label_push_locator,
"push_notification_checkbox_label"
)
self.add_content_item("push_notification_checkbox_label", label_push)
# Чекбокс "Подписка на Push-уведомления"
checkbox_push = Checkbox(
page,
page.locator(input_form_locator).locator(ModalWindowLocators.INPUT_FORM_USER_DATA_CHECKBOX_PUSH_ACTIVE),
"push_notification"
)
self.add_content_item("push_notification_checkbox", checkbox_push)
# Добавление кнопок действий
locator_button_save = self.page.get_by_role("button", name="Сохранить")
self.add_button(locator_button_save, "save")
locator_button_delete = self.page.get_by_role("button", name="Удалить")
self.add_button(locator_button_delete, "delete")
locator_button_reset = self.page.get_by_role("button", name="Сбросить пароль")
self.add_button(locator_button_reset, "reset_password")
locator_button_close = self.page.get_by_role("button", name="Закрыть")
self.add_button(locator_button_close, "close")
# Инициализация компонентов подтверждения
self.save_user_confirm = ConfirmComponent(page, " Отмена ", " Сохранить ")
self.delete_user_confirm = ConfirmComponent(page, " Отмена ", " Удалить ")
# Действия:
def check_blocking_checkbox(self):
"""Включает чек-бокс Блокировка."""
self.get_content_item("blocking_checkbox").check(force=True)
def uncheck_blocking_checkbox(self):
"""Выключает чек-бокс Блокировка."""
self.get_content_item("blocking_checkbox").uncheck(force=True)
def check_push_notification_checkbox(self):
"""Включает чек-бокс Push-уведомления."""
self.get_content_item("push_notification_checkbox").check(force=True)
def uncheck_push_notification_checkbox(self):
"""Выключает чек-бокс Push-уведомления."""
self.get_content_item("push_notification_checkbox").uncheck(force=True)
def close_window(self):
"""Закрывает окно через кнопку 'Закрыть'."""
close_button = self.get_button_by_name("close")
close_button.click()
def close_window_by_toolbar_button(self):
"""Закрывает окно через кнопку в тулбаре."""
self.click_toolbar_close_button()
def delete_user(self):
"""Удаляет пользователя с подтверждением."""
delete_button = self.get_button_by_name("delete")
delete_button.click()
title = "Удаление"
self.delete_user_confirm.check_title(
title,
f"Confirmation dialog window with title '{title}' is missing"
)
self.delete_user_confirm.click_allow_button()
def edit_user(self, user_data):
"""Редактирует данные пользователя.
Args:
user_data (dict): Данные для обновления (имя, роль и др.)
"""
fields = user_data.keys()
if "name" in fields:
input_field = self.get_content_item("name_input")
input_field.input_value(user_data["name"])
if "role" in fields:
role_field = self.get_content_item("role_input")
role_field.click()
roles_list = self.get_content_item("roles_list")
roles_list.check_item_with_text(user_data["role"])
roles_list.click_item_with_text(user_data["role"])
if "commentary" in fields:
input_field = self.get_content_item("commentary_input")
input_field.input_value(user_data["commentary"])
if "email" in fields:
input_field = self.get_content_item("email_input")
input_field.input_value(user_data["email"])
if "phone_number" in fields:
input_field = self.get_content_item("phone_input")
input_field.input_value(user_data["phone_number"])
if "blocking_checked" in fields:
checkbox = self.get_content_item("blocking_checkbox")
if user_data["blocking_checked"]:
checkbox.check()
else:
checkbox.uncheck()
if "push_notification_checked" in fields:
checkbox = self.get_content_item("push_notification_checkbox")
if user_data["push_notification_checked"]:
checkbox.check()
else:
checkbox.uncheck()
save_button = self.get_button_by_name("save")
save_button.click()
title = "Сохранение"
self.save_user_confirm.check_title(
title,
f"Confirmation dialog window with title '{title}' is missing"
)
self.save_user_confirm.click_allow_button()
def reset_password(self):
"""Инициирует сброс пароля пользователя."""
reset_password_button = self.get_button_by_name("reset_password")
reset_password_button.click()
# Проверки:
def check_content(self, user_name, role):
"""Проверяет наличие и корректность элементов окна.
Args:
user_name (str): Ожидаемое имя пользователя
role (str): Ожидаемая роль пользователя
"""
menu_locator = self.page.locator(ModalWindowLocators.MENU_ACTIVE_INPUT_FORM)
self.check_by_window_title()
self.check_toolbar_button_visibility("close")
self.check_toolbar_button_tooltip("close", "Закрыть")
for name in self.content_items:
item = self.get_content_item(name)
if name == "push_notification_checkbox_label":
item.check_have_text(
"Подписка на Push-уведомления",
"Label 'Подписка на Push-уведомления' is missing"
)
elif name == "blocking_checkbox_label":
item.check_have_text(
"Блокировка",
"Label 'Блокировка' is missing"
)
elif name == "name_input":
name_field = self.get_content_item("name_input")
text_value = name_field.get_input_value()
assert text_value == user_name, (
f"Expected user name '{user_name}' is not equal "
f"real user name '{text_value}'"
)
elif name == "role_input":
item.click()
roles_list = self.get_content_item("roles_list")
roles_list.check_visibility(menu_locator, "Roles list is missing")
roles_list.check_item_with_text(role)
elif name == "roles_list":
continue
else:
item.check_visibility(
f"Modal window content item with name '{name}' is missing"
)
self.check_button_visibility("save")
self.check_button_visibility("delete")
self.check_button_visibility("reset_password")
self.check_button_visibility("close")