| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import { Page, Locator, expect } from '@playwright/test';
- export class AdminLoginPage {
- readonly page: Page;
- readonly usernameInput: Locator;
- readonly passwordInput: Locator;
- readonly loginButton: Locator;
- readonly errorMessage: Locator;
- constructor(page: Page) {
- this.page = page;
- this.usernameInput = page.getByPlaceholder('请输入用户名');
- this.passwordInput = page.getByPlaceholder('请输入密码');
- this.loginButton = page.getByRole('button', { name: '登录' });
- this.errorMessage = page.locator('[data-sonner-toast]');
- }
- async goto() {
- await this.page.goto('/admin/login');
- await this.page.waitForLoadState('networkidle');
- }
- 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');
- await this.page.waitForTimeout(2000);
- }
- async expectLoginSuccess() {
- // 登录成功后应该重定向到管理后台dashboard
- await expect(this.page).toHaveURL('/admin/dashboard');
- await expect(this.page.locator('text=登录成功')).toBeVisible();
- }
- async expectLoginError() {
- await expect(this.errorMessage).toBeVisible();
- }
- }
|