import { Page, expect } from '@playwright/test'; import { BasePage } from './BasePage'; import { PATHS } from '../config/staging-config'; /** * Page object representing the login page */ export class LoginPage extends BasePage { // Login form elements private readonly usernameInput = '#user_login'; private readonly passwordInput = '#user_pass'; private readonly loginButton = '#wp-submit'; private readonly rememberMeCheckbox = '#rememberme'; private readonly loginError = '.login-error, .login_error'; private readonly forgotPasswordLink = 'a.forgot-password, a:text("Lost your password?")'; constructor(page: Page) { super(page); } /** * Navigate to the login page */ async navigate(): Promise { await this.page.goto(PATHS.login); await this.page.waitForSelector(this.usernameInput); } /** * Alternative name for navigate for backward compatibility */ async navigateToLogin(): Promise { await this.navigate(); } /** * Login with provided credentials * @param username Username or email * @param password Password */ async login(username: string, password: string): Promise { this.log(`Logging in as ${username}`); await this.page.fill(this.usernameInput, username); await this.page.fill(this.passwordInput, password); await this.page.click(this.loginButton); await this.page.waitForLoadState('networkidle'); } /** * Check if we're logged in */ async isLoggedIn(): Promise { const url = await this.getUrl(); return url.includes('hvac-dashboard'); } /** * Check if username field is visible */ async isUsernameFieldVisible(): Promise { return await this.page.isVisible(this.usernameInput); } /** * Get error message if login failed */ async getErrorMessage(): Promise { if (await this.page.isVisible(this.loginError)) { return await this.page.textContent(this.loginError); } return null; } /** * Click on "forgot password" link */ async clickForgotPassword(): Promise { await this.page.click(this.forgotPasswordLink); await this.page.waitForLoadState('networkidle'); } /** * Toggle "remember me" checkbox * @param check If true, check the box; if false, uncheck it */ async setRememberMe(check: boolean): Promise { const isChecked = await this.page.isChecked(this.rememberMeCheckbox); if (check !== isChecked) { await this.page.click(this.rememberMeCheckbox); } } }