import { Page, expect } from '@playwright/test'; import { BasePage } from './base-page'; import { SELECTORS, TIMEOUTS } from '../constants'; export class DashboardPage extends BasePage { private readonly selectors = { dashboardContainer: '.dashboard-container', pageTitle: '.page-title', statisticsCards: '.statistic-card', charts: '.chart-container', menuItems: '.ant-menu-item', welcomeMessage: '.welcome-message', quickActions: '.quick-action' }; constructor(page: Page) { super(page); } async navigate(): Promise { testLogger.info('导航到仪表盘页面'); await super.navigate('/dashboard'); } async waitForLoad(): Promise { testLogger.info('等待仪表盘页面加载'); try { await this.page.waitForSelector(this.selectors.dashboardContainer, { state: 'visible', timeout: this.timeout.default }); testLogger.info('仪表盘页面加载完成'); } catch (error) { testLogger.error('仪表盘页面加载超时', error as Error); await this.screenshotHelper.takeScreenshot('dashboard-load-error'); throw error; } } async getPageTitle(): Promise { testLogger.debug('获取页面标题'); try { const titleElement = this.page.locator(this.selectors.pageTitle); await titleElement.waitFor({ state: 'visible', timeout: this.timeout.element }); const title = await titleElement.textContent(); testLogger.debug(`页面标题: ${title}`); return title || ''; } catch (error) { testLogger.error('获取页面标题失败', error as Error); return ''; } } async getWelcomeMessage(): Promise { testLogger.debug('获取欢迎消息'); try { const welcomeElement = this.page.locator(this.selectors.welcomeMessage); await welcomeElement.waitFor({ state: 'visible', timeout: this.timeout.element }); const message = await welcomeElement.textContent(); testLogger.debug(`欢迎消息: ${message}`); return message || ''; } catch (error) { testLogger.error('获取欢迎消息失败', error as Error); return ''; } } async getStatisticsCardCount(): Promise { testLogger.debug('获取统计卡片数量'); try { const cards = this.page.locator(this.selectors.statisticsCards); const count = await cards.count(); testLogger.debug(`统计卡片数量: ${count}`); return count; } catch (error) { testLogger.error('获取统计卡片数量失败', error as Error); return 0; } } async getChartCount(): Promise { testLogger.debug('获取图表数量'); try { const charts = this.page.locator(this.selectors.charts); const count = await charts.count(); testLogger.debug(`图表数量: ${count}`); return count; } catch (error) { testLogger.error('获取图表数量失败', error as Error); return 0; } } async clickMenuItem(menuName: string): Promise { testLogger.info(`点击菜单项: ${menuName}`); try { const menuItem = this.page.locator(this.selectors.menuItems).filter({ hasText: menuName }); await menuItem.waitFor({ state: 'visible', timeout: this.timeout.element }); await menuItem.click(); testLogger.info(`菜单项点击成功: ${menuName}`); } catch (error) { testLogger.error(`点击菜单项失败: ${menuName}`, error as Error); await this.screenshotHelper.takeScreenshot(`click-menu-${menuName}-error`); throw error; } } async clickQuickAction(actionName: string): Promise { testLogger.info(`点击快捷操作: ${actionName}`); try { const quickAction = this.page.locator(this.selectors.quickActions).filter({ hasText: actionName }); await quickAction.waitFor({ state: 'visible', timeout: this.timeout.element }); await quickAction.click(); testLogger.info(`快捷操作点击成功: ${actionName}`); } catch (error) { testLogger.error(`点击快捷操作失败: ${actionName}`, error as Error); await this.screenshotHelper.takeScreenshot(`click-quick-action-${actionName}-error`); throw error; } } async isDashboardVisible(): Promise { testLogger.debug('检查仪表盘是否可见'); try { const dashboard = this.page.locator(this.selectors.dashboardContainer); const isVisible = await dashboard.isVisible(); testLogger.debug(`仪表盘可见性: ${isVisible}`); return isVisible; } catch (error) { testLogger.error('检查仪表盘可见性失败', error as Error); return false; } } async waitForStatistics(): Promise { testLogger.info('等待统计数据加载'); try { await this.page.waitForSelector(this.selectors.statisticsCards, { state: 'visible', timeout: this.timeout.network }); testLogger.info('统计数据加载完成'); } catch (error) { testLogger.error('等待统计数据加载超时', error as Error); await this.screenshotHelper.takeScreenshot('statistics-load-error'); throw error; } } async waitForCharts(): Promise { testLogger.info('等待图表加载'); try { await this.page.waitForSelector(this.selectors.charts, { state: 'visible', timeout: this.timeout.network }); testLogger.info('图表加载完成'); } catch (error) { testLogger.error('等待图表加载超时', error as Error); await this.screenshotHelper.takeScreenshot('charts-load-error'); throw error; } } }