""" 角色管理页面 Admin后台角色管理页面的页面对象模型。 """ from playwright.sync_api import Page from ..base_page import BasePage class RoleManagementPage(BasePage): """角色管理页面""" # 页面路径 PATH = "/system/role" # 元素定位器 LOCATORS = { "page_title": ".page-title, h1", "create_button": ".el-button--primary", "search_input": ".search-input input", "search_button": ".search-btn", "table": ".el-table, table.el-table", "table_rows": ".el-table__row, tr.el-table__row", "dialog": ".el-dialog", "dialog_title": ".el-dialog__title", "form_name": 'input[name="name"], input[placeholder*="名称"]', "form_code": 'input[name="code"], input[placeholder*="编码"]', "form_description": 'textarea[name="description"], textarea[placeholder*="描述"]', "form_status": '.el-select', "form_submit": '.el-dialog__footer .el-button--primary', "form_cancel": '.el-dialog__footer .el-button--default', "permission_tree": ".el-tree", "delete_confirm": ".el-message-box__btns .el-button--primary", "success_message": ".el-message--success", "error_message": ".el-message--error", } def __init__(self, page: Page, base_url: str = ""): super().__init__(page, base_url) def navigate(self, path: str = "") -> None: """导航到角色管理页面""" target_path = path if path else self.PATH self.page.goto(f"{self.base_url}{target_path}") self.wait_for_load() def is_loaded(self) -> bool: """检查页面是否加载完成""" return self.is_element_visible(self.LOCATORS["table"]) def click_create_button(self) -> None: """点击新建按钮""" self.click_element(self.LOCATORS["create_button"]) def fill_search(self, keyword: str) -> None: """填写搜索关键词""" self.fill_input(self.LOCATORS["search_input"], keyword) def click_search(self) -> None: """点击搜索按钮""" self.click_element(self.LOCATORS["search_button"]) def get_table_rows_count(self) -> int: """获取表格行数""" return self.get_elements_count(self.LOCATORS["table_rows"]) def click_row_edit(self, row_index: int = 0) -> None: """点击行编辑按钮""" row = self.page.locator(self.LOCATORS["table_rows"]).nth(row_index) edit_btn = row.locator(".el-button--primary").first edit_btn.click() def click_row_delete(self, row_index: int = 0) -> None: """点击行删除按钮""" row = self.page.locator(self.LOCATORS["table_rows"]).nth(row_index) delete_btn = row.locator(".el-button--danger").first delete_btn.click() def confirm_delete(self) -> None: """确认删除""" self.click_element(self.LOCATORS["delete_confirm"]) def fill_form_name(self, name: str) -> None: """填写表单角色名称""" self.fill_input(self.LOCATORS["form_name"], name) def fill_form_code(self, code: str) -> None: """填写表单角色编码""" self.fill_input(self.LOCATORS["form_code"], code) def fill_form_description(self, description: str) -> None: """填写表单角色描述""" self.fill_input(self.LOCATORS["form_description"], description) def select_form_status(self, status: str) -> None: """选择表单状态""" self.click_element(self.LOCATORS["form_status"]) self.page.locator(".el-select-dropdown__item").filter( has_text=status ).click() def click_form_submit(self) -> None: """点击表单提交按钮""" self.click_element(self.LOCATORS["form_submit"]) def click_form_cancel(self) -> None: """点击表单取消按钮""" self.click_element(self.LOCATORS["form_cancel"]) def is_dialog_visible(self) -> bool: """检查对话框是否可见""" return self.is_element_visible(self.LOCATORS["dialog"]) def get_dialog_title(self) -> str: """获取对话框标题""" return self.get_text(self.LOCATORS["dialog_title"]) def check_permission(self, permission_name: str) -> None: """勾选权限""" self.page.locator(self.LOCATORS["permission_tree"]).locator( ".el-tree-node__label" ).filter(has_text=permission_name).locator("..").locator( ".el-checkbox__input" ).first.click() def has_success_message(self) -> bool: """检查是否有成功消息""" return self.is_element_visible( self.LOCATORS["success_message"], timeout=3000 ) def has_error_message(self) -> bool: """检查是否有错误消息""" return self.is_element_visible( self.LOCATORS["error_message"], timeout=3000 ) def wait_for_table_load(self, timeout: int = 10000) -> None: """等待表格加载完成""" self.wait_for_selector(self.LOCATORS["table_rows"], timeout=timeout)