/** * 页面基类 * 提供所有页面共享的方法和属性 */ import { Page, expect } from '@playwright/test'; import { TestConfig } from '../config/test.config'; export class BasePage { protected page: Page; protected config = TestConfig; constructor(page: Page) { this.page = page; } /** * 等待页面稳定 */ async waitForPageStable(timeout: number = this.config.waits.pageStable): Promise { console.log(`等待 ${timeout / 1000} 秒让页面稳定...`); await this.page.waitForTimeout(timeout); console.log('页面已稳定,继续下一步'); } /** * 导航到指定URL */ async goto(url: string): Promise { await this.page.goto(url, { timeout: this.config.timeouts.navigation }); } /** * 点击元素 */ async click(selector: string): Promise { await this.page.click(selector); } /** * 填充输入框 */ async fill(selector: string, text: string): Promise { await this.page.fill(selector, text); } /** * 检查元素是否可见 */ async isVisible(selector: string, timeout?: number): Promise { try { await expect(this.page.locator(selector)).toBeVisible({ timeout }); return true; } catch { return false; } } }