dashboard.page.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 userManagementCard = this.page.locator('text=用户管理').first();
  28. await userManagementCard.click();
  29. await this.page.waitForLoadState('networkidle');
  30. }
  31. async navigateToSystemSettings() {
  32. // 使用更具体的定位器来避免重复元素问题
  33. const systemSettingsCard = this.page.locator('text=系统设置').first();
  34. await systemSettingsCard.click();
  35. await this.page.waitForLoadState('networkidle');
  36. }
  37. async getActiveUsersCount(): Promise<string> {
  38. // 使用更可靠的定位器来获取活跃用户统计数字
  39. const countElement = this.page.locator('text=活跃用户').locator('xpath=following::div[contains(@class, "text-2xl")][1]');
  40. await expect(countElement).toBeVisible({ timeout: 10000 });
  41. return await countElement.textContent() || '';
  42. }
  43. async getSystemMessagesCount(): Promise<string> {
  44. // 使用更可靠的定位器来获取系统消息统计数字
  45. const countElement = this.page.locator('text=系统消息').locator('xpath=following::div[contains(@class, "text-2xl")][1]');
  46. await expect(countElement).toBeVisible({ timeout: 10000 });
  47. return await countElement.textContent() || '';
  48. }
  49. async logout() {
  50. // 先点击用户头像/用户名打开下拉菜单
  51. const userMenuButton = this.page.getByRole('button', { name: 'admin' });
  52. await userMenuButton.click();
  53. // 然后查找并点击登出按钮
  54. const logoutButton = this.page.getByRole('menuitem', { name: /登出|退出|Logout|Sign out/i });
  55. await logoutButton.click();
  56. await this.page.waitForLoadState('networkidle');
  57. }
  58. clone(newPage: Page): DashboardPage {
  59. return new DashboardPage(newPage);
  60. }
  61. }