File: pages/BasePage.ts
The BasePage class provides reusable, robust, and well-logged utility methods for interacting with web elements in Playwright.
All specific page classes should extend BasePage to inherit these utilities, ensuring consistency and reducing code duplication.
async waitForElement(
locator: Locator,
timeout: number = 5000,
maxRetries: number = 3,
delayBetweenRetries: number = 1000
): Promise<void>Waits for an element to appear/become ready, with configurable retries and delays.
- Retries up to
maxRetriestimes, waitingtimeoutms each time. - Logs each attempt and failure.
- Throws an error if the element is not found after all retries.
async clickElement(locator: Locator, wait: boolean = true): Promise<void>Clicks an element, optionally waiting for it to be ready first.
async enterText(locator: Locator, value: string, wait: boolean = true): Promise<void>Fills an input field with the provided value, optionally waiting for readiness.
async typeText(locator: Locator, value: string, wait: boolean = true): Promise<void>Types text into an input field character by character, simulating real user input.
async selectDropdown(locator: Locator, value: string, wait: boolean = true): Promise<void>Selects an option from a dropdown by value.
async getElementText(locator: Locator, wait: boolean = true): Promise<string>Retrieves the text content of an element.
async isElementVisible(locator: Locator): Promise<boolean>Checks if an element is visible on the page.
All actions are logged using the shared logger utility for traceability and easier debugging.
import { BasePage } from './BasePage';
class LoginPage extends BasePage {
// Define locators and page-specific methods here
}- Add new generic element actions here so all pages benefit.
- Avoid page-specific logic in
BasePage. - Use logs to diagnose flaky tests or UI issues