from pages.base_page import BasePage from Locators.base_page import BasePageLocators from Locators.configuration_page import ConfigurationPageLocators from Locators.users_tab import UsersTabLocators from data.assertions import Assertions from playwright.sync_api import Page import re class UsersTab(BasePage): def __init__(self, page: Page) -> None: super().__init__(page) self.assertion = Assertions(page) self.role_dict = {"administrator": "Администратор", "manager":"Контактное лицо", "operator":"Оператор", "inform_secur_user" : "Специалист информационной безопасности"} def add_new_user(self, user_data): fields = user_data.keys() self.assertion.check_presence(UsersTabLocators.USER_DATA_INPUT_FORM, \ "Input form for add new user is not present") ## input user name if "name" in fields: loc = "xpath=div[2]/div[2]/div/div/div/div/input" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).fill(user_data["name"]) ## input user role if "role" in fields: loc = "xpath=div[3]/div[2]/div/div/div/div/div[1]" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).click() self.assertion.check_presence(UsersTabLocators.USER_DATA_INPUT_FORM_ROLES_MENU, \ "Roles drop-down menu is not present") role = user_data["role"] self.assertion.check_menu_item_with_text(role, f"No menu item with text: {role}") self.page.get_by_role("listitem").filter(has_text=role).click() ## input password if "password" in fields: loc = "xpath=div[4]/div[2]/div/div/div/div/input" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).fill(user_data["password"]) ## input commentary if "commentary" in fields: loc = "xpath=div[5]/div[2]/div/div/div/div/input" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).fill(user_data["commentary"]) ## input e-mail if "email" in fields: loc = "xpath=div[6]/div[2]/div/div/div/div/input" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).fill(user_data["email"]) ## input phone number for sms if "phone_number" in fields: loc = "xpath=div[7]/div[2]/div/div/div/div/input" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).fill(user_data["phone_number"]) ##to be done: checkbox ## click add user button add_button_text = "Добавить" self.assertion.check_button_presence_with_text(add_button_text, f"Add user input form button with text {add_button_text} is not present") self.page.get_by_role("button", name=add_button_text).click() ##check add user confirmation dialog confirm_add_button_text = " Добавить " self.assertion.check_confirmation_dialog_with_title(UsersTabLocators.USER_ACTION_CONFIRMATION_DIALOG, "Добавить нового пользователя") self.page.get_by_role("button", name=confirm_add_button_text).first.click() ## check message about successfull user addition self.assertion.check_alert_window_with_text("success", ' Новый пользователь \n успешно добавлен! ') def close_user_window_by_toolbar_button(self, window_title): BUTTON_CLOSE_ON_TOOLBAR = f"xpath=(.//*[normalize-space(text()) and normalize-space(.)='{window_title}'])[1]/following::*[name()='svg'][1]" self.assertion.check_presence(BUTTON_CLOSE_ON_TOOLBAR, \ "Close button is not presenet on toolbar") self.click(BUTTON_CLOSE_ON_TOOLBAR) self.assertion.check_absence(UsersTabLocators.USER_DATA_WORK_AREA_TITLE, \ f"{window_title} window should be closed") def close_user_window(self, window_title): close_button_text = "Закрыть" self.assertion.check_button_presence_with_text(close_button_text, f"Edit user input form button with text {close_button_text} is not present") self.page.get_by_role("button", name=close_button_text).click() self.assertion.check_absence(UsersTabLocators.USER_DATA_WORK_AREA_TITLE, \ f"{window_title} window should be closed") def delete_user(self): remove_button_text = "Удалить" self.assertion.check_button_presence_with_text(remove_button_text, f"Edit user input form button with text {remove_button_text} is not present") self.page.get_by_role("button", name=remove_button_text).click() ##check add user confirmation dialog confirm_remove_button_text = " Удалить " self.assertion.check_confirmation_dialog_with_title(UsersTabLocators.USER_ACTION_CONFIRMATION_DIALOG, "Удаление") self.page.get_by_role("button", name=confirm_remove_button_text).first.click() ## check message about successfull user deletion self.assertion.check_alert_window_with_text("success", '\nПользователь удалён\n') def edit_user(self, user_data): fields = user_data.keys() self.assertion.check_presence(UsersTabLocators.USER_DATA_INPUT_FORM, \ "Input form for edit user is not present") ## input user name if "name" in fields: loc = "xpath=div[2]/div[2]/div/div/div/div/input" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).fill(user_data["name"]) ## input user role if "role" in fields: loc = "xpath=div[3]/div[2]/div/div/div/div/div[1]" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).click() self.assertion.check_presence(UsersTabLocators.USER_DATA_INPUT_FORM_ROLES_MENU, \ "Roles drop-down menu is not present") role = user_data["role"] self.assertion.check_menu_item_with_text(role, f"No menu item with text: {role}") self.page.get_by_role("listitem").filter(has_text=role).click() ## input commentary if "commentary" in fields: loc = "xpath=div[5]/div[2]/div/div/div/div/input" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).fill(user_data["commentary"]) ## input e-mail if "email" in fields: loc = "xpath=div[5]/div[2]/div/div/div/div/input" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).fill(user_data["email"]) ## input phone number for sms if "phone_number" in fields: loc = "xpath=div[6]/div[2]/div/div/div/div/input" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).fill(user_data["phone_number"]) ##to be done: checkbox ## click add user button add_button_text = "Сохранить" self.assertion.check_button_presence_with_text(add_button_text, f"Add user input form button with text {add_button_text} is not present") self.page.get_by_role("button", name=add_button_text).click() ##check add user confirmation dialog confirm_add_button_text = " Сохранить " self.assertion.check_confirmation_dialog_with_title(UsersTabLocators.USER_ACTION_CONFIRMATION_DIALOG, "Сохранение") self.page.get_by_role("button", name=confirm_add_button_text).first.click() ## check message about successfull user addition. Temporarily without translation self.assertion.check_alert_window_with_text("success", '\nupdate success\n') def open_add_user_page(self): self.assertion.check_presence(UsersTabLocators.TOOLBAR_EDIT_BUTTON, \ "Edit button is not presenet on toolbar") self.click(UsersTabLocators.TOOLBAR_EDIT_BUTTON) self.assertion.check_presence(UsersTabLocators.TOOLBAR_ADD_USER_BUTTON, \ "Add User button is not presenet on toolbar") self.click(UsersTabLocators.TOOLBAR_ADD_USER_BUTTON) # check that new work area with title has been opened self.assertion.have_title(UsersTabLocators.USER_DATA_WORK_AREA_TITLE, "Добавить нового пользователя", \ "Expected work area page title is not equal real title") def open_edit_user_page_by_index(self, row_index): ## temporarily tmp_dict = {"admin":"Администратор", "manager":"Контактное лицо", "operator":"Оператор"} users_table = self.read_table_data(ConfigurationPageLocators.WORK_AREA_TABLE) self.assertion.check_not_equals(len(users_table) - 1, 0, "Users table is empty") # remove header del users_table[0] # check row_index if row_index > len(users_table): assert False, "Row_index is out of range" # get user name and role user_name = users_table[row_index][0] for key, val in tmp_dict.items(): if user_name == val: user_name = key role = users_table[row_index][2] # click to found table row self.page.locator(ConfigurationPageLocators.WORK_AREA_TABLE).locator(BasePageLocators.TABLE_BODY).nth(row_index).click() # check that edit user work area with user name title has been opened self.assertion.have_title(UsersTabLocators.USER_DATA_WORK_AREA_TITLE, user_name, \ "Expected edit user work area page title is not equal real title") return user_name, role def reset_password(self): new_password = "" reset_password_button_text = "Сбросить пароль" self.assertion.check_button_presence_with_text(reset_password_button_text, f"Edit user input form button with text {reset_password_button_text} is not present") self.page.get_by_role("button", name=reset_password_button_text).click() alert_message = self.get_text(BasePageLocators.ALERT_WINDOW_TEXT_SUCCESS, 0) if len(alert_message) > 0: new_password = re.findall(r'[\d]+', alert_message)[0] return new_password def open_edit_user_page_by_name(self, name, role): row_index = self.find_user_in_table(name, role) if row_index == -1: assert False, f"User with name {name} and role {role} has not been found" # click to found table row self.page.locator(ConfigurationPageLocators.WORK_AREA_TABLE).locator(BasePageLocators.TABLE_BODY).nth(row_index).click() # check that edit user work area with user name title has been opened self.assertion.have_title(UsersTabLocators.USER_DATA_WORK_AREA_TITLE, name, \ "Expected edit user work area page title is not equal real title") def should_be_users_work_area(self): self.assertion.have_title(ConfigurationPageLocators.WORK_AREA_TITLE, "Пользователи", \ "Expected work area page title is not equal real title") self.assertion.check_presence(UsersTabLocators.TOOLBAR_EDIT_BUTTON, \ "Edit button is not presenet on toolbar") self.should_be_users_table() def should_be_users_page_toolbar_buttons(self): self.assertion.check_presence(UsersTabLocators.TOOLBAR_EDIT_BUTTON, \ "Edit button is not presenet on toolbar") self.assertion.check_tooltip_with_text(UsersTabLocators.TOOLBAR_EDIT_BUTTON, "Редактировать") self.click(UsersTabLocators.TOOLBAR_EDIT_BUTTON) self.assertion.check_presence(UsersTabLocators.TOOLBAR_ADD_USER_BUTTON, \ "Add User button is not presenet on toolbar") self.assertion.check_presence(UsersTabLocators.TOOLBAR_CLOSE_BUTTON, \ "Close button is not presenet on toolbar") self.assertion.check_tooltip_with_text(UsersTabLocators.TOOLBAR_ADD_USER_BUTTON, "Добавить") self.assertion.check_tooltip_with_text(UsersTabLocators.TOOLBAR_CLOSE_BUTTON, "Закрыть") self.click(UsersTabLocators.TOOLBAR_CLOSE_BUTTON) self.assertion.check_presence(UsersTabLocators.TOOLBAR_EDIT_BUTTON, \ "Edit button is not presenet on toolbar") def should_be_add_user_work_area(self): self.assertion.have_title(UsersTabLocators.USER_DATA_WORK_AREA_TITLE, "Добавить нового пользователя", \ "Expected add user window title is not equal real title") self.assertion.check_presence(UsersTabLocators.ADD_USER_WORK_AREA_CLOSE_BUTTON, \ "Close button is not presenet on toolbar") self.assertion.check_tooltip_with_text(UsersTabLocators.ADD_USER_WORK_AREA_CLOSE_BUTTON, "Закрыть") # check input form self.should_be_add_user_input_form() # check input form buttons add_button_text = "Добавить" self.assertion.check_button_presence_with_text(add_button_text, f"Add user input form button with text {add_button_text} is not present") close_button_text = "Закрыть" self.assertion.check_button_presence_with_text(close_button_text, f"Add user input form button with text {close_button_text} is not present") def should_be_edit_user_work_area(self, user_name, role): EDIT_USER_WORK_AREA_CLOSE_BUTTON = f"xpath=(.//*[normalize-space(text()) and normalize-space(.)='{user_name}'])[1]/following::*[name()='svg'][1]" self.assertion.have_title(UsersTabLocators.USER_DATA_WORK_AREA_TITLE, f"{user_name}", \ "Expected edit user window title is not equal real title") self.assertion.check_presence(EDIT_USER_WORK_AREA_CLOSE_BUTTON, \ "Close button is not presenet on toolbar") self.assertion.check_tooltip_with_text(EDIT_USER_WORK_AREA_CLOSE_BUTTON, "Закрыть") # check edit user input form self.should_be_edit_user_input_form(user_name, role) # check input form buttons save_button_text = "Сохранить" self.assertion.check_button_presence_with_text(save_button_text, f"Edit user input form button with text {save_button_text} is not present") remove_button_text = "Удалить" self.assertion.check_button_presence_with_text(remove_button_text, f"Edit user input form button with text {remove_button_text} is not present") reset_password_button_text = "Сбросить пароль" self.assertion.check_button_presence_with_text(reset_password_button_text, f"Edit user input form button with text {reset_password_button_text} is not present") close_button_text = "Закрыть" self.assertion.check_button_presence_with_text(close_button_text, f"Edit user input form button with text {close_button_text} is not present") def should_be_users_table(self): headers = ['Имя пользователя', 'hash_password', 'Роль', 'E-mail', 'Номер для СМС'] users_table = self.read_table_data(ConfigurationPageLocators.WORK_AREA_TABLE) users_table_headers = users_table[0] self.assertion.check_equals(users_table_headers, headers, \ f"Expected table headers {users_table_headers} are not equal {headers}") self.assertion.check_not_equals(len(users_table) - 1, 0, \ "Users table is empty") self.verify_users_table_content(users_table) def should_be_add_user_input_form(self): self.assertion.check_presence(UsersTabLocators.USER_DATA_INPUT_FORM, \ "Input form for add new user is not present") n = 7 for i in range (1, n + 1): loc = f"xpath=div[{i}]/div[2]/div/div/div/div/input" input_area = self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc) if i == 1: checked = self.page.get_by_role("checkbox").first.is_checked() self.assertion.check_equals(checked, False, "Checkbox is checked by default") loc = f"xpath={UsersTabLocators.USER_DATA_INPUT_FORM}/div[1]/div[2]/div/div/div{UsersTabLocators.USER_DATA_INPUT_FORM_NOTIFICATION_LABEL}" self.assertion.have_text(loc, "ad", \ "Label for ad is not present") elif i == 3: loc = f"xpath=div[{i}]/div[2]/div/div/div/div/div[1]" self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).click() self.assertion.check_presence(UsersTabLocators.USER_DATA_INPUT_FORM_ROLES_MENU, \ "Roles drop-down menu is not present") roles = self.role_dict.values() for role in roles: self.assertion.check_menu_item_with_text(role, f"No menu item with text: {role}") else: self.assertion.check_empty_input_area(input_area, "No empty input area") checked = self.page.get_by_role("checkbox").nth(1).is_checked() self.assertion.check_equals(checked, False, "Checkbox is checked by default") loc = f"xpath={UsersTabLocators.USER_DATA_INPUT_FORM}/div[8]/div/div/div/div{UsersTabLocators.USER_DATA_INPUT_FORM_NOTIFICATION_LABEL}" self.assertion.have_text(loc, "Подписка на Push-уведомления", \ "Label for push-notifications subscribtion is not present") def should_be_edit_user_input_form(self, user_name, role): self.assertion.check_presence(UsersTabLocators.USER_DATA_INPUT_FORM, \ "Input form for edit user is not present") loc = "xpath=div[2]/div[2]/div/div/div/div/input" text_value = self.page.locator(UsersTabLocators.USER_DATA_INPUT_FORM).locator(loc).input_value() self.assertion.check_equals(text_value, user_name, "Expected user name is not equal real user name") self.assertion.check_menu_item_with_text(role, f"No menu item with text: {role}") def should_be_user_in_table(self, name, role): found = self.find_user_in_table(name, role) if found == -1: assert False, f"User with name {name} and role {role} has not been found" def should_not_be_user_in_table(self, name, role): found = self.find_user_in_table(name, role) if found != -1: assert False, f"User with name {name} and role {role} has been found" def find_user_in_table(self, name, role): users_table = self.read_table_data(ConfigurationPageLocators.WORK_AREA_TABLE) self.assertion.check_not_equals(len(users_table) - 1, 0, "Users table is empty") # remove header del users_table[0] not_found_index = -1 row_index = 0 for user_info in users_table: if name in user_info and role in user_info: return row_index row_index += 1 return not_found_index def verify_users_table_content(self, users_table): expected_users_list = [] ## temporarily tmp_dict = {"admin":"Администратор", "manager":"Контактное лицо", "operator":"Оператор"} query = {"id": ["/catalogs/user"], "data": {"namePath": True, "children": {"flatten": True}}} response = self.send_post_api_request("e-cmdb/api/query", query) response_body = self.get_response_body(response) for item in response_body[0]["children"]: user_info = [] ## temporarily user_name = item["name"] if user_name in tmp_dict.keys(): item["name"] = tmp_dict[user_name] user_info.append(item["name"]) if item["password"] is not None: user_info.append(item["password"]) else: user_info.append("") if item["role"] is not None: role = item["role"] if role in self.role_dict.keys(): item["role"] = self.role_dict[role] user_info.append(item["role"]) else: user_info.append("") if item["email"] is not None: user_info.append(item["email"]) else: user_info.append("") if item["sms_phone"] is not None: user_info.append(item["sms_phone"]) else: user_info.append("") expected_users_list.append(user_info) # remove header del users_table[0] self.assertion.check_lists_equals(users_table, expected_users_list, \ "Actual users list is not equal users list from db")