login.page.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { Page, Locator, expect } from '@playwright/test';
  2. export class LoginPage {
  3. readonly page: Page;
  4. readonly usernameInput: Locator;
  5. readonly passwordInput: Locator;
  6. readonly loginButton: Locator;
  7. readonly registerLink: Locator;
  8. readonly errorMessage: Locator;
  9. constructor(page: Page) {
  10. this.page = page;
  11. this.usernameInput = page.getByPlaceholder('请输入用户名');
  12. this.passwordInput = page.getByPlaceholder('请输入密码');
  13. this.loginButton = page.getByRole('button', { name: '登录' });
  14. this.registerLink = page.getByRole('link', { name: '立即注册' });
  15. this.errorMessage = page.locator('[data-sonner-toast]');
  16. }
  17. async goto() {
  18. await this.page.goto('/login');
  19. await this.page.waitForLoadState('networkidle');
  20. }
  21. async login(username: string, password: string) {
  22. await this.usernameInput.fill(username);
  23. await this.passwordInput.fill(password);
  24. await this.loginButton.click();
  25. }
  26. async expectLoginSuccess() {
  27. await expect(this.page).toHaveURL('/');
  28. await expect(this.page.locator('text=登录成功')).toBeVisible();
  29. }
  30. async expectLoginError() {
  31. await expect(this.errorMessage).toBeVisible();
  32. }
  33. async navigateToRegister() {
  34. await this.registerLink.click();
  35. }
  36. }