login.page.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { Page, Locator, expect } from '@playwright/test';
  2. export class AdminLoginPage {
  3. readonly page: Page;
  4. readonly usernameInput: Locator;
  5. readonly passwordInput: Locator;
  6. readonly loginButton: Locator;
  7. readonly errorMessage: Locator;
  8. constructor(page: Page) {
  9. this.page = page;
  10. this.usernameInput = page.getByPlaceholder('请输入用户名');
  11. this.passwordInput = page.getByPlaceholder('请输入密码');
  12. this.loginButton = page.getByRole('button', { name: '登录' });
  13. this.errorMessage = page.locator('[data-sonner-toast]');
  14. }
  15. async goto() {
  16. await this.page.goto('/admin/login');
  17. await this.page.waitForLoadState('networkidle');
  18. }
  19. async login(username: string, password: string) {
  20. await this.usernameInput.fill(username);
  21. await this.passwordInput.fill(password);
  22. await this.loginButton.click();
  23. // 等待登录完成
  24. await this.page.waitForLoadState('networkidle');
  25. await this.page.waitForTimeout(2000);
  26. }
  27. async expectLoginSuccess() {
  28. // 登录成功后应该重定向到管理后台dashboard
  29. await expect(this.page).toHaveURL('/admin/dashboard');
  30. await expect(this.page.locator('text=登录成功')).toBeVisible();
  31. }
  32. async expectLoginError() {
  33. await expect(this.errorMessage).toBeVisible();
  34. }
  35. }