user-management.page.ts 7.6 KB

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