user-management.page.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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-slot="pagination"]');
  22. }
  23. async goto() {
  24. // 直接导航到用户管理页面
  25. await this.page.goto('/admin/users');
  26. // 等待页面完全加载 - 使用更可靠的等待条件
  27. await this.page.waitForLoadState('networkidle');
  28. await this.expectToBeVisible();
  29. }
  30. async expectToBeVisible() {
  31. // 等待页面完全加载,使用更精确的选择器
  32. await this.page.waitForSelector('h1:has-text("用户管理")', { state: 'visible', timeout: 15000 });
  33. await expect(this.pageTitle).toBeVisible();
  34. await expect(this.createUserButton).toBeVisible();
  35. await expect(this.userTable).toBeVisible();
  36. }
  37. async searchUsers(keyword: string) {
  38. await this.searchInput.fill(keyword);
  39. await this.searchButton.click();
  40. await this.page.waitForLoadState('networkidle');
  41. }
  42. async createUser(userData: {
  43. username: string;
  44. password: string;
  45. nickname?: string;
  46. email?: string;
  47. phone?: string;
  48. name?: string;
  49. }) {
  50. await this.createUserButton.click();
  51. // 填写用户表单
  52. await this.page.getByLabel('用户名').fill(userData.username);
  53. await this.page.getByLabel('密码').fill(userData.password);
  54. if (userData.nickname) {
  55. await this.page.getByLabel('昵称').fill(userData.nickname);
  56. }
  57. if (userData.email) {
  58. await this.page.getByLabel('邮箱').fill(userData.email);
  59. }
  60. if (userData.phone) {
  61. await this.page.getByLabel('手机号').fill(userData.phone);
  62. }
  63. if (userData.name) {
  64. await this.page.getByLabel('真实姓名').fill(userData.name);
  65. }
  66. // 提交表单 - 使用模态框中的创建按钮
  67. await this.page.locator('[role="dialog"]').getByRole('button', { name: '创建用户' }).click();
  68. await this.page.waitForLoadState('networkidle');
  69. // 等待用户创建结果提示 - 成功或失败
  70. try {
  71. await Promise.race([
  72. this.page.waitForSelector('text=创建成功', { timeout: 10000 }),
  73. this.page.waitForSelector('text=创建失败', { timeout: 10000 })
  74. ]);
  75. // 检查是否有错误提示
  76. const errorVisible = await this.page.locator('text=创建失败').isVisible().catch(() => false);
  77. if (errorVisible) {
  78. // 如果是创建失败,不需要刷新页面
  79. return;
  80. }
  81. // 如果是创建成功,刷新页面
  82. await this.page.waitForTimeout(1000);
  83. await this.page.reload();
  84. await this.page.waitForLoadState('networkidle');
  85. await this.expectToBeVisible();
  86. } catch (error) {
  87. // 如果没有提示出现,继续执行
  88. console.log('创建操作没有显示提示信息,继续执行');
  89. await this.page.reload();
  90. await this.page.waitForLoadState('networkidle');
  91. await this.expectToBeVisible();
  92. }
  93. }
  94. async getUserCount(): Promise<number> {
  95. const rows = await this.userTable.locator('tbody tr').count();
  96. return rows;
  97. }
  98. async getUserByUsername(username: string): Promise<Locator | null> {
  99. const userRow = this.userTable.locator('tbody tr').filter({ hasText: username }).first();
  100. return (await userRow.count()) > 0 ? userRow : null;
  101. }
  102. async userExists(username: string): Promise<boolean> {
  103. const userRow = this.userTable.locator('tbody tr').filter({ hasText: username }).first();
  104. return (await userRow.count()) > 0;
  105. }
  106. async editUser(username: string, updates: {
  107. nickname?: string;
  108. email?: string;
  109. phone?: string;
  110. name?: string;
  111. }) {
  112. const userRow = await this.getUserByUsername(username);
  113. if (!userRow) throw new Error(`User ${username} not found`);
  114. // 编辑按钮是图标按钮,使用按钮定位(第一个按钮是编辑,第二个是删除)
  115. const editButton = userRow.locator('button').first();
  116. await editButton.waitFor({ state: 'visible', timeout: 10000 });
  117. await editButton.click();
  118. // 等待编辑模态框出现
  119. await this.page.waitForSelector('[role="dialog"]', { state: 'visible', timeout: 10000 });
  120. // 更新字段
  121. if (updates.nickname) {
  122. await this.page.getByLabel('昵称').fill(updates.nickname);
  123. }
  124. if (updates.email) {
  125. await this.page.getByLabel('邮箱').fill(updates.email);
  126. }
  127. if (updates.phone) {
  128. await this.page.getByLabel('手机号').fill(updates.phone);
  129. }
  130. if (updates.name) {
  131. await this.page.getByLabel('真实姓名').fill(updates.name);
  132. }
  133. // 提交更新
  134. await this.page.locator('[role="dialog"]').getByRole('button', { name: '更新用户' }).click();
  135. await this.page.waitForLoadState('networkidle');
  136. // 等待操作完成
  137. await this.page.waitForTimeout(1000);
  138. }
  139. async deleteUser(username: string) {
  140. const userRow = await this.getUserByUsername(username);
  141. if (!userRow) throw new Error(`User ${username} not found`);
  142. // 删除按钮是图标按钮,使用按钮定位(第二个按钮是删除)
  143. const deleteButton = userRow.locator('button').nth(1);
  144. await deleteButton.waitFor({ state: 'visible', timeout: 10000 });
  145. await deleteButton.click();
  146. // 确认删除对话框
  147. await this.page.getByRole('button', { name: '删除' }).click();
  148. // 等待删除操作完成 - 等待成功提示或错误提示
  149. try {
  150. // 等待成功提示或错误提示出现
  151. await Promise.race([
  152. this.page.waitForSelector('text=删除成功', { timeout: 10000 }),
  153. this.page.waitForSelector('text=删除失败', { timeout: 10000 })
  154. ]);
  155. // 检查是否有错误提示
  156. const errorVisible = await this.page.locator('text=删除失败').isVisible().catch(() => false);
  157. if (errorVisible) {
  158. throw new Error('删除操作失败:前端显示删除失败提示');
  159. }
  160. } catch (error) {
  161. // 如果没有提示出现,继续执行(可能是静默删除)
  162. console.log('删除操作没有显示提示信息,继续执行');
  163. }
  164. // 刷新页面确认用户是否被删除
  165. await this.page.reload();
  166. await this.page.waitForLoadState('networkidle');
  167. await this.expectToBeVisible();
  168. }
  169. async expectUserExists(username: string) {
  170. const exists = await this.userExists(username);
  171. expect(exists).toBe(true);
  172. }
  173. async expectUserNotExists(username: string) {
  174. const exists = await this.userExists(username);
  175. expect(exists).toBe(false);
  176. }
  177. }