import { Page, Locator } from '@playwright/test'; export class RoleManagementPage { readonly page: Page; readonly table: Locator; readonly createRoleButton: Locator; readonly successMessage: Locator; readonly roleNameInput: Locator; readonly roleKeyInput: Locator; readonly roleSortInput: Locator; readonly statusSelect: Locator; readonly remarkInput: Locator; readonly permissionDialog: Locator; readonly savePermissionButton: Locator; constructor(page: Page) { this.page = page; this.table = page.locator('.el-table').or(page.locator('table')); this.createRoleButton = page.getByRole('button', { name: '创建角色' }).or(page.locator('button:has-text("创建角色")')); this.successMessage = page.locator('.el-message--success').or(page.locator('.success-message')); this.roleNameInput = page.locator('input[placeholder*="角色名称"]').or(page.locator('input[name*="roleName"]')); this.roleKeyInput = page.locator('input[placeholder*="角色权限字符串"]').or(page.locator('input[name*="roleKey"]')); this.roleSortInput = page.locator('input[placeholder*="显示顺序"]').or(page.locator('input[name*="roleSort"]')); this.statusSelect = page.locator('select[name*="status"]').or(page.locator('.el-select')); this.remarkInput = page.locator('textarea[placeholder*="备注"]').or(page.locator('textarea[name*="remark"]')); this.permissionDialog = page.locator('.permission-dialog').or(page.locator('.el-dialog')); this.savePermissionButton = page.getByRole('button', { name: '保存' }).or(page.locator('.permission-dialog .save-button')); } async goto() { await this.page.goto('/roles'); await this.page.waitForLoadState('networkidle'); } async clickCreateRole() { await this.createRoleButton.click(); await this.page.waitForTimeout(500); } async fillRoleForm(roleData: { roleName: string; roleKey: string; roleSort?: string; status?: string; remark?: string; }) { await this.roleNameInput.fill(roleData.roleName); await this.roleKeyInput.fill(roleData.roleKey); if (roleData.roleSort) { await this.roleSortInput.fill(roleData.roleSort); } if (roleData.status) { await this.statusSelect.selectOption(roleData.status); } if (roleData.remark) { await this.remarkInput.fill(roleData.remark); } } async submitForm() { await this.page.getByRole('button', { name: '确定' }).or(page.locator('button:has-text("确定")')).click(); } async editRole(rowNumber: number) { await this.table.locator(`tbody tr:nth-child(${rowNumber})`).getByRole('button', { name: '编辑' }).or(page.locator(`tbody tr:nth-child(${rowNumber}) .edit-button`)).click(); } async deleteRole(rowNumber: number) { await this.table.locator(`tbody tr:nth-child(${rowNumber})`).getByRole('button', { name: '删除' }).or(page.locator(`tbody tr:nth-child(${rowNumber}) .delete-button`)).click(); } async confirmDelete() { await this.page.getByRole('button', { name: '确定' }).or(page.locator('.confirm-dialog .confirm-button')).click(); } async openPermissionDialog(rowNumber: number) { await this.table.locator(`tbody tr:nth-child(${rowNumber})`).getByRole('button', { name: '权限' }).or(page.locator(`tbody tr:nth-child(${rowNumber}) .permission-button`)).click(); } async selectPermission(permissionValue: string) { await this.page.click(`input[type="checkbox"][value="${permissionValue}"]`); } async savePermissions() { await this.savePermissionButton.click(); } async containsText(text: string): Promise { return await this.table.getByText(text).count() > 0; } async isSuccessMessageVisible(): Promise { try { return await this.successMessage.isVisible({ timeout: 3000 }); } catch { return false; } } async reload() { await this.page.reload(); } async getRoleName(rowNumber: number): Promise { return await this.table.locator(`tbody tr:nth-child(${rowNumber}) td:first-child`).textContent(); } }