Files
E2E-Test/pages/BasePage.ts
2025-11-04 16:29:07 +08:00

59 lines
1.3 KiB
TypeScript

/**
* 页面基类
* 提供所有页面共享的方法和属性
*/
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<void> {
console.log(`等待 ${timeout / 1000} 秒让页面稳定...`);
await this.page.waitForTimeout(timeout);
console.log('页面已稳定,继续下一步');
}
/**
* 导航到指定URL
*/
async goto(url: string): Promise<void> {
await this.page.goto(url, { timeout: this.config.timeouts.navigation });
}
/**
* 点击元素
*/
async click(selector: string): Promise<void> {
await this.page.click(selector);
}
/**
* 填充输入框
*/
async fill(selector: string, text: string): Promise<void> {
await this.page.fill(selector, text);
}
/**
* 检查元素是否可见
*/
async isVisible(selector: string, timeout?: number): Promise<boolean> {
try {
await expect(this.page.locator(selector)).toBeVisible({ timeout });
return true;
} catch {
return false;
}
}
}