import { Page, Locator, expect } from '@playwright/test'; export class UserManagementPage { readonly page: Page; readonly pageTitle: Locator; readonly createUserButton: Locator; readonly searchInput: Locator; readonly searchButton: Locator; readonly userTable: Locator; readonly editButtons: Locator; readonly deleteButtons: Locator; readonly pagination: Locator; constructor(page: Page) { this.page = page; this.pageTitle = page.getByRole('heading', { name: '用户管理' }); this.createUserButton = page.getByRole('button', { name: '创建用户' }); this.searchInput = page.getByPlaceholder('搜索用户名、昵称或邮箱...'); this.searchButton = page.getByRole('button', { name: '搜索' }); this.userTable = page.locator('table'); this.editButtons = page.locator('button').filter({ hasText: '编辑' }); this.deleteButtons = page.locator('button').filter({ hasText: '删除' }); this.pagination = page.locator('[data-testid="pagination"]'); } async goto() { await this.page.goto('/admin/users'); await this.page.waitForLoadState('networkidle'); await this.expectToBeVisible(); } async expectToBeVisible() { await expect(this.pageTitle).toBeVisible(); await expect(this.createUserButton).toBeVisible(); await expect(this.userTable).toBeVisible(); } async searchUsers(keyword: string) { await this.searchInput.fill(keyword); await this.searchButton.click(); await this.page.waitForLoadState('networkidle'); } async createUser(userData: { username: string; password: string; nickname?: string; email?: string; phone?: string; name?: string; }) { await this.createUserButton.click(); // 填写用户表单 await this.page.getByLabel('用户名').fill(userData.username); await this.page.getByLabel('密码').fill(userData.password); if (userData.nickname) { await this.page.getByLabel('昵称').fill(userData.nickname); } if (userData.email) { await this.page.getByLabel('邮箱').fill(userData.email); } if (userData.phone) { await this.page.getByLabel('手机号').fill(userData.phone); } if (userData.name) { await this.page.getByLabel('真实姓名').fill(userData.name); } // 提交表单 await this.page.getByRole('button', { name: '创建用户' }).click(); await this.page.waitForLoadState('networkidle'); } async getUserCount(): Promise { const rows = await this.userTable.locator('tbody tr').count(); return rows; } async getUserByUsername(username: string): Promise { const userRow = this.userTable.locator('tbody tr').filter({ hasText: username }).first(); return (await userRow.count()) > 0 ? userRow : null; } async editUser(username: string, updates: { nickname?: string; email?: string; phone?: string; name?: string; }) { const userRow = await this.getUserByUsername(username); if (!userRow) throw new Error(`User ${username} not found`); await userRow.locator('button').filter({ hasText: '编辑' }).click(); // 更新字段 if (updates.nickname) { await this.page.getByLabel('昵称').fill(updates.nickname); } if (updates.email) { await this.page.getByLabel('邮箱').fill(updates.email); } if (updates.phone) { await this.page.getByLabel('手机号').fill(updates.phone); } if (updates.name) { await this.page.getByLabel('真实姓名').fill(updates.name); } // 提交更新 await this.page.getByRole('button', { name: '更新用户' }).click(); await this.page.waitForLoadState('networkidle'); } async deleteUser(username: string) { const userRow = await this.getUserByUsername(username); if (!userRow) throw new Error(`User ${username} not found`); await userRow.locator('button').filter({ hasText: '删除' }).click(); await this.page.getByRole('button', { name: '删除' }).click(); await this.page.waitForLoadState('networkidle'); } async expectUserExists(username: string) { const userRow = await this.getUserByUsername(username); await expect(userRow).not.toBeNull(); } async expectUserNotExists(username: string) { const userRow = await this.getUserByUsername(username); await expect(userRow).toBeNull(); } }