import { Page, expect } from '@playwright/test'; /** * 登录页面对象模型 (POM) */ export class LoginPage { readonly page: Page; readonly usernameInput = page.locator('input[placeholder*="用户名"], input[placeholder*="账号"]'); readonly passwordInput = page.locator('input[placeholder*="密码"]'); readonly loginButton = page.locator('button:has-text("登录"), button[type="submit"]'); readonly errorMessage = page.locator('.el-message--error'); readonly successMessage = page.locator('.el-message--success'); constructor(page: Page) { this.page = page; } async goto() { await this.page.goto('/'); await this.page.waitForLoadState('domcontentloaded'); } async login(username: string, password: string) { await this.usernameInput.fill(username); await this.passwordInput.fill(password); await this.loginButton.click(); await this.page.waitForLoadState('networkidle'); } async expectLoginSuccess() { await expect(this.page).toHaveURL(/.*(dashboard|home|index).*/, { timeout: 15000 }); } async expectLoginFailed() { await expect(this.errorMessage).toBeVisible({ timeout: 5000 }); } async expectOnLoginPage() { await expect(this.usernameInput).toBeVisible(); } }