dashboard.page.ts 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Page, Locator, expect } from '@playwright/test';
  2. export class DashboardPage {
  3. readonly page: Page;
  4. readonly pageTitle: Locator;
  5. readonly activeUsersCard: Locator;
  6. readonly systemMessagesCard: Locator;
  7. readonly onlineUsersCard: Locator;
  8. readonly userManagementCard: Locator;
  9. readonly systemSettingsCard: Locator;
  10. constructor(page: Page) {
  11. this.page = page;
  12. this.pageTitle = page.getByRole('heading', { name: '仪表盘' });
  13. this.activeUsersCard = page.getByText('活跃用户');
  14. this.systemMessagesCard = page.getByText('系统消息');
  15. this.onlineUsersCard = page.getByText('在线用户');
  16. this.userManagementCard = page.getByText('用户管理');
  17. this.systemSettingsCard = page.getByText('系统设置');
  18. }
  19. async expectToBeVisible(options?: { timeout?: number }) {
  20. await expect(this.pageTitle).toBeVisible(options);
  21. await expect(this.activeUsersCard).toBeVisible(options);
  22. await expect(this.systemMessagesCard).toBeVisible(options);
  23. await expect(this.onlineUsersCard).toBeVisible(options);
  24. }
  25. async navigateToUserManagement() {
  26. // 检查是否为移动端,需要先打开菜单
  27. const isMobile = (this.page.viewportSize()?.width || 0) < 768;
  28. if (isMobile) {
  29. // 移动端需要先点击菜单按钮 - 使用测试ID
  30. const menuButton = this.page.getByTestId('mobile-menu-button');
  31. await expect(menuButton).toBeVisible({ timeout: 10000 });
  32. await menuButton.click({ timeout: 15000 });
  33. await this.page.waitForTimeout(1000); // 等待菜单完全展开
  34. }
  35. // 移动端需要点击侧边栏菜单项,而不是快捷操作卡片
  36. const userManagementCard = isMobile
  37. ? this.page.getByRole('button', { name: '用户管理' }).first()
  38. : this.page.locator('text=用户管理').first();
  39. await expect(userManagementCard).toBeVisible({ timeout: 10000 });
  40. await userManagementCard.click({ timeout: 10000 });
  41. await this.page.waitForLoadState('networkidle');
  42. }
  43. async navigateToSystemSettings() {
  44. // 检查是否为移动端,需要先打开菜单
  45. const isMobile = (this.page.viewportSize()?.width || 0) < 768;
  46. if (isMobile) {
  47. // 移动端需要先点击菜单按钮 - 使用测试ID
  48. const menuButton = this.page.getByTestId('mobile-menu-button');
  49. await expect(menuButton).toBeVisible({ timeout: 10000 });
  50. await menuButton.click({ timeout: 15000 });
  51. await this.page.waitForTimeout(1000); // 等待菜单完全展开
  52. }
  53. // 使用更具体的定位器来避免重复元素问题
  54. const systemSettingsCard = this.page.locator('text=系统设置').first();
  55. await systemSettingsCard.click({ timeout: 10000 });
  56. await this.page.waitForLoadState('networkidle');
  57. }
  58. async getActiveUsersCount(): Promise<string> {
  59. // 使用更可靠的定位器来获取活跃用户统计数字
  60. const countElement = this.page.locator('text=活跃用户').locator('xpath=following::div[contains(@class, "text-2xl")][1]');
  61. await expect(countElement).toBeVisible({ timeout: 10000 });
  62. return await countElement.textContent() || '';
  63. }
  64. async getSystemMessagesCount(): Promise<string> {
  65. // 使用更可靠的定位器来获取系统消息统计数字
  66. const countElement = this.page.locator('text=系统消息').locator('xpath=following::div[contains(@class, "text-2xl")][1]');
  67. await expect(countElement).toBeVisible({ timeout: 10000 });
  68. return await countElement.textContent() || '';
  69. }
  70. async logout() {
  71. // 先点击用户头像/用户名打开下拉菜单
  72. const userMenuButton = this.page.getByRole('button', { name: 'admin' });
  73. await userMenuButton.click();
  74. // 然后查找并点击登出按钮
  75. const logoutButton = this.page.getByRole('menuitem', { name: /登出|退出|Logout|Sign out/i });
  76. await logoutButton.click();
  77. await this.page.waitForLoadState('networkidle');
  78. }
  79. clone(newPage: Page): DashboardPage {
  80. return new DashboardPage(newPage);
  81. }
  82. }