Files
张翔 08ea5fbe98 feat(admin): 添加用户管理相关文件
添加用户管理视图、API和状态管理文件
2026-03-28 14:37:29 +08:00

174 lines
6.4 KiB
Python

"""
用户管理页面
Admin后台用户管理页面的页面对象模型。
"""
from playwright.sync_api import Page
from ..base_page import BasePage
class UserManagementPage(BasePage):
"""用户管理页面"""
# 页面路径
PATH = "/users"
# 元素定位器
LOCATORS = {
"page_title": ".user-management h1, .page-title",
"create_button": ".card-header .el-button--primary, .el-button:has-text('新增用户')",
"search_input": ".search-card input[v-model='queryParams.username'], .search-card .el-input input",
"search_button": ".search-card .el-button--primary, .el-button:has-text('搜索')",
"reset_button": ".search-card .el-button:not(.el-button--primary), .el-button:has-text('重置')",
"table": ".table-card .el-table, .el-table",
"table_rows": ".el-table__row",
"pagination": ".el-pagination",
"dialog": ".el-dialog",
"dialog_title": ".el-dialog__title",
"form_username": '.el-dialog input[v-model="formState.username"], .el-dialog input[placeholder*="用户名"]',
"form_nickname": '.el-dialog input[v-model="formState.nickname"], .el-dialog input[placeholder*="昵称"]',
"form_email": '.el-dialog input[v-model="formState.email"], .el-dialog input[placeholder*="邮箱"]',
"form_phone": '.el-dialog input[v-model="formState.phone"], .el-dialog input[placeholder*="手机"]',
"form_status": '.el-dialog .el-select',
"form_submit": '.el-dialog__footer .el-button--primary',
"form_cancel": '.el-dialog__footer .el-button:not(.el-button--primary)',
"delete_confirm": ".el-message-box__btns .el-button--primary, .el-popconfirm .el-button--primary",
"success_message": ".el-message--success .el-message__content, .el-message--success",
"error_message": ".el-message--error .el-message__content, .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:
"""检查页面是否加载完成"""
try:
self.wait_for_timeout(2000)
return self.is_element_visible(self.LOCATORS["table"], timeout=10000)
except:
return False
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 click_reset(self) -> None:
"""点击重置按钮"""
self.click_element(self.LOCATORS["reset_button"])
def get_table_rows_count(self) -> int:
"""获取表格行数"""
return self.get_elements_count(self.LOCATORS["table_rows"])
def get_first_row_text(self) -> str:
"""获取第一行文本"""
rows = self.page.locator(self.LOCATORS["table_rows"]).all()
if rows:
return rows[0].text_content() or ""
return ""
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_username(self, username: str) -> None:
"""填写表单用户名"""
self.fill_input(self.LOCATORS["form_username"], username)
def fill_form_nickname(self, nickname: str) -> None:
"""填写表单昵称"""
self.fill_input(self.LOCATORS["form_nickname"], nickname)
def fill_form_email(self, email: str) -> None:
"""填写表单邮箱"""
self.fill_input(self.LOCATORS["form_email"], email)
def fill_form_phone(self, phone: str) -> None:
"""填写表单电话"""
self.fill_input(self.LOCATORS["form_phone"], phone)
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:
"""检查对话框是否可见"""
try:
dialog = self.page.locator(self.LOCATORS["dialog"])
if dialog.count() == 0:
return False
is_visible = dialog.is_visible()
if not is_visible:
return False
return True
except:
return False
def get_dialog_title(self) -> str:
"""获取对话框标题"""
return self.get_text(self.LOCATORS["dialog_title"])
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:
"""等待表格加载完成"""
try:
self.wait_for_timeout(2000)
self.wait_for_selector(self.LOCATORS["table_rows"], timeout=timeout)
except:
pass
def search_and_wait(self, keyword: str, timeout: int = 10000) -> None:
"""搜索并等待结果"""
self.fill_search(keyword)
self.click_search()
self.wait_for_table_load(timeout)