2
0

user-management.page.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { Page, Locator, expect } from '@playwright/test';
  2. export class UserManagementPage {
  3. readonly page: Page;
  4. readonly pageTitle: Locator;
  5. readonly createUserButton: Locator;
  6. readonly searchInput: Locator;
  7. readonly searchButton: Locator;
  8. readonly userTable: Locator;
  9. readonly editButtons: Locator;
  10. readonly deleteButtons: Locator;
  11. readonly pagination: Locator;
  12. constructor(page: Page) {
  13. this.page = page;
  14. this.pageTitle = page.getByRole('heading', { name: '用户管理' });
  15. this.createUserButton = page.getByRole('button', { name: '创建用户' });
  16. this.searchInput = page.getByPlaceholder('搜索用户名、昵称或邮箱...');
  17. this.searchButton = page.getByRole('button', { name: '搜索' });
  18. this.userTable = page.locator('table');
  19. this.editButtons = page.locator('button').filter({ hasText: '编辑' });
  20. this.deleteButtons = page.locator('button').filter({ hasText: '删除' });
  21. this.pagination = page.locator('[data-testid="pagination"]');
  22. }
  23. async goto() {
  24. await this.page.goto('/admin/users');
  25. await this.page.waitForLoadState('networkidle');
  26. await this.expectToBeVisible();
  27. }
  28. async expectToBeVisible() {
  29. await expect(this.pageTitle).toBeVisible();
  30. await expect(this.createUserButton).toBeVisible();
  31. await expect(this.userTable).toBeVisible();
  32. }
  33. async searchUsers(keyword: string) {
  34. await this.searchInput.fill(keyword);
  35. await this.searchButton.click();
  36. await this.page.waitForLoadState('networkidle');
  37. }
  38. async createUser(userData: {
  39. username: string;
  40. password: string;
  41. nickname?: string;
  42. email?: string;
  43. phone?: string;
  44. name?: string;
  45. }) {
  46. await this.createUserButton.click();
  47. // 填写用户表单
  48. await this.page.getByLabel('用户名').fill(userData.username);
  49. await this.page.getByLabel('密码').fill(userData.password);
  50. if (userData.nickname) {
  51. await this.page.getByLabel('昵称').fill(userData.nickname);
  52. }
  53. if (userData.email) {
  54. await this.page.getByLabel('邮箱').fill(userData.email);
  55. }
  56. if (userData.phone) {
  57. await this.page.getByLabel('手机号').fill(userData.phone);
  58. }
  59. if (userData.name) {
  60. await this.page.getByLabel('真实姓名').fill(userData.name);
  61. }
  62. // 提交表单
  63. await this.page.getByRole('button', { name: '创建用户' }).click();
  64. await this.page.waitForLoadState('networkidle');
  65. }
  66. async getUserCount(): Promise<number> {
  67. const rows = await this.userTable.locator('tbody tr').count();
  68. return rows;
  69. }
  70. async getUserByUsername(username: string): Promise<Locator | null> {
  71. const userRow = this.userTable.locator('tbody tr').filter({ hasText: username }).first();
  72. return (await userRow.count()) > 0 ? userRow : null;
  73. }
  74. async editUser(username: string, updates: {
  75. nickname?: string;
  76. email?: string;
  77. phone?: string;
  78. name?: string;
  79. }) {
  80. const userRow = await this.getUserByUsername(username);
  81. if (!userRow) throw new Error(`User ${username} not found`);
  82. await userRow.locator('button').filter({ hasText: '编辑' }).click();
  83. // 更新字段
  84. if (updates.nickname) {
  85. await this.page.getByLabel('昵称').fill(updates.nickname);
  86. }
  87. if (updates.email) {
  88. await this.page.getByLabel('邮箱').fill(updates.email);
  89. }
  90. if (updates.phone) {
  91. await this.page.getByLabel('手机号').fill(updates.phone);
  92. }
  93. if (updates.name) {
  94. await this.page.getByLabel('真实姓名').fill(updates.name);
  95. }
  96. // 提交更新
  97. await this.page.getByRole('button', { name: '更新用户' }).click();
  98. await this.page.waitForLoadState('networkidle');
  99. }
  100. async deleteUser(username: string) {
  101. const userRow = await this.getUserByUsername(username);
  102. if (!userRow) throw new Error(`User ${username} not found`);
  103. await userRow.locator('button').filter({ hasText: '删除' }).click();
  104. await this.page.getByRole('button', { name: '删除' }).click();
  105. await this.page.waitForLoadState('networkidle');
  106. }
  107. async expectUserExists(username: string) {
  108. const userRow = await this.getUserByUsername(username);
  109. await expect(userRow).not.toBeNull();
  110. }
  111. async expectUserNotExists(username: string) {
  112. const userRow = await this.getUserByUsername(username);
  113. await expect(userRow).toBeNull();
  114. }
  115. }