import { Page, Locator, expect } from '@playwright/test'; export class StatisticsDashboardPage { readonly page: Page; readonly summaryCards: Locator; readonly statCards: Locator; constructor(page: Page) { this.page = page; this.summaryCards = page.locator('.el-card').or(page.locator('.statistics-card')).or(page.locator('.summary-card')).or(page.locator('.dashboard-card')); this.statCards = page.locator('.stat-card').or(page.locator('.el-card')).or(page.locator('.summary-card')); } async goto() { try { console.log('导航到数据统计页面...'); await this.page.goto('/statistics'); await this.page.waitForLoadState('networkidle'); await expect(this.page).toHaveURL(/.*statistics/); console.log('数据统计页面加载完成'); } catch (error) { await this.page.screenshot({ path: `test-results/statistics-dashboard-error-${Date.now()}.png` }); console.error('导航到数据统计页面失败:', error); throw new Error(`导航到数据统计页面失败: ${error instanceof Error ? error.message : String(error)}`); } } async verifyStatisticsLoaded(): Promise { try { await this.summaryCards.first().waitFor({ state: 'visible', timeout: 10000 }); const cardCount = await this.summaryCards.count(); console.log(`数据统计页面加载完成,找到 ${cardCount} 个统计卡片`); return cardCount > 0; } catch (error) { console.log('统计卡片未找到,尝试备选选择器'); const altCards = this.page.locator('.statistics-container').or(this.page.locator('.dashboard-container')); if (await altCards.count() > 0) { await altCards.first().waitFor({ state: 'visible', timeout: 10000 }); return true; } return false; } } async getSummaryCardCount(): Promise { return await this.summaryCards.count(); } async getSummaryCardText(index: number): Promise { if (await this.summaryCards.count() > index) { return await this.summaryCards.nth(index).textContent(); } return null; } async verifyStatCardsLoaded(): Promise { try { await this.statCards.first().waitFor({ state: 'visible', timeout: 15000 }); const cardCount = await this.statCards.count(); console.log(`统计卡片加载完成,共 ${cardCount} 个`); return cardCount > 0; } catch { return false; } } async getCardValue(index: number): Promise { const cards = this.statCards; if (await cards.count() > index) { return (await cards.nth(index).textContent()) || '0'; } return '0'; } async verifySectionCardsLoaded(): Promise { await this.page.waitForTimeout(500); const allCards = this.page.locator('.el-card, .stat-card, .chart-container'); if (await allCards.count() > 0) { console.log(`详细统计区域已加载,共 ${await allCards.count()} 个卡片/图表`); } } async switchRange(range: string): Promise { const rangeBtn = this.page.locator('.el-radio-button').filter({ hasText: range }).or(this.page.locator('button').filter({ hasText: range })); if (await rangeBtn.count() > 0) { await rangeBtn.first().click(); await this.page.waitForTimeout(500); } } async switchTab(tabName: string): Promise { const tab = this.page.locator('.el-tabs__item').filter({ hasText: tabName }).or(this.page.locator('.el-tab-pane')).filter({ hasText: tabName }).or(this.page.locator('[role="tab"]').filter({ hasText: tabName })); if (await tab.count() > 0) { await tab.first().click(); await this.page.waitForTimeout(800); } } }