login.page.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. async expectLoginSuccess() {
  25. // 登录成功后应该重定向到管理后台dashboard
  26. await expect(this.page).toHaveURL('/admin/dashboard');
  27. await expect(this.page.locator('text=登录成功')).toBeVisible();
  28. }
  29. async expectLoginError() {
  30. await expect(this.errorMessage).toBeVisible();
  31. }
  32. }