| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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<number> {
- const rows = await this.userTable.locator('tbody tr').count();
- return rows;
- }
- async getUserByUsername(username: string): Promise<Locator | null> {
- 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();
- }
- }
|