import { Page, Locator } from '@playwright/test'; export class UserManagementPage { readonly page: Page; readonly table: Locator; readonly createUserButton: Locator; readonly searchInput: Locator; readonly searchButton: Locator; readonly successMessage: Locator; readonly pagination: Locator; readonly nextPageButton: Locator; readonly prevPageButton: Locator; constructor(page: Page) { this.page = page; this.table = page.locator('.el-table').or(page.locator('table')); this.createUserButton = page.getByRole('button', { name: '创建用户' }).or(page.locator('button:has-text("创建用户")')); this.searchInput = page.locator('input[placeholder*="搜索"]').or(page.locator('input[name*="keyword"]')); this.searchButton = page.getByRole('button', { name: '搜索' }).or(page.locator('button:has-text("搜索")')); this.successMessage = page.locator('.el-message--success').or(page.locator('.success-message')); this.pagination = page.locator('.el-pagination').or(page.locator('.pagination')); this.nextPageButton = page.locator('.el-pagination .btn-next').or(page.locator('.pagination .next-page')); this.prevPageButton = page.locator('.el-pagination .btn-prev').or(page.locator('.pagination .prev-page')); } async goto() { await this.page.goto('/users'); await this.page.waitForLoadState('networkidle'); } async clickCreateUser() { await this.createUserButton.click(); await this.page.waitForTimeout(500); } async fillUserForm(userData: { username: string; email: string; phone?: string; password: string; confirmPassword: string; }) { await this.page.locator('input[placeholder*="用户名"]').or(page.locator('input[name*="username"]')).fill(userData.username); await this.page.locator('input[placeholder*="邮箱"]').or(page.locator('input[name*="email"]')).fill(userData.email); if (userData.phone) { await this.page.locator('input[placeholder*="手机号"]').or(page.locator('input[name*="phone"]')).fill(userData.phone); } await this.page.locator('input[placeholder*="密码"]').or(page.locator('input[name*="password"]')).first().fill(userData.password); await this.page.locator('input[placeholder*="确认密码"]').or(page.locator('input[name*="confirmPassword"]')).fill(userData.confirmPassword); } async submitForm() { await this.page.getByRole('button', { name: '确定' }).or(page.locator('button:has-text("确定")')).click(); } async editUser(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 deleteUser(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 search(keyword: string) { await this.searchInput.fill(keyword); await this.searchButton.click(); } async nextPage() { await this.nextPageButton.click(); } async prevPage() { await this.prevPageButton.click(); } async getCurrentPage(): Promise { return await this.page.locator('.el-pagination .el-pager li.active').or(page.locator('.pagination .current-page')).textContent() || '1'; } async getUserCount(): Promise { return await this.table.locator('tbody tr').count(); } async getUserName(rowNumber: number): Promise { return await this.table.locator(`tbody tr:nth-child(${rowNumber}) td:first-child`).textContent(); } 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(); } }