dashboard.page.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. await this.userManagementCard.click();
  27. await this.page.waitForLoadState('networkidle');
  28. }
  29. async navigateToSystemSettings() {
  30. await this.systemSettingsCard.click();
  31. await this.page.waitForLoadState('networkidle');
  32. }
  33. async getActiveUsersCount(): Promise<string> {
  34. return await this.activeUsersCard.locator('xpath=following-sibling::div//div[contains(@class, "text-2xl")]').textContent() || '';
  35. }
  36. async getSystemMessagesCount(): Promise<string> {
  37. return await this.systemMessagesCard.locator('xpath=following-sibling::div//div[contains(@class, "text-2xl")]').textContent() || '';
  38. }
  39. async logout() {
  40. // 先点击用户头像/用户名打开下拉菜单
  41. const userMenuButton = this.page.getByRole('button', { name: 'admin' });
  42. await userMenuButton.click();
  43. // 然后查找并点击登出按钮
  44. const logoutButton = this.page.getByRole('menuitem', { name: /登出|退出|Logout|Sign out/i });
  45. await logoutButton.click();
  46. await this.page.waitForLoadState('networkidle');
  47. }
  48. clone(newPage: Page): DashboardPage {
  49. return new DashboardPage(newPage);
  50. }
  51. }