|
| 1 | +# BasePage Class |
| 2 | + |
| 3 | +**File:** `pages/BasePage.ts` |
| 4 | + |
| 5 | +## Purpose |
| 6 | + |
| 7 | +The `BasePage` class provides reusable, robust, and well-logged utility methods for interacting with web elements in Playwright. |
| 8 | +All specific page classes should extend `BasePage` to inherit these utilities, ensuring consistency and reducing code duplication. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Methods |
| 13 | + |
| 14 | +### waitForElement |
| 15 | + |
| 16 | +```typescript |
| 17 | +async waitForElement( |
| 18 | + locator: Locator, |
| 19 | + timeout: number = 5000, |
| 20 | + maxRetries: number = 3, |
| 21 | + delayBetweenRetries: number = 1000 |
| 22 | +): Promise<void> |
| 23 | +``` |
| 24 | +Waits for an element to appear/become ready, with configurable retries and delays. |
| 25 | +- Retries up to `maxRetries` times, waiting `timeout` ms each time. |
| 26 | +- Logs each attempt and failure. |
| 27 | +- Throws an error if the element is not found after all retries. |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +### clickElement |
| 32 | + |
| 33 | +```typescript |
| 34 | +async clickElement(locator: Locator, wait: boolean = true): Promise<void> |
| 35 | +``` |
| 36 | +Clicks an element, optionally waiting for it to be ready first. |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +### enterText |
| 41 | + |
| 42 | +```typescript |
| 43 | +async enterText(locator: Locator, value: string, wait: boolean = true): Promise<void> |
| 44 | +``` |
| 45 | +Fills an input field with the provided value, optionally waiting for readiness. |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +### typeText |
| 50 | + |
| 51 | +```typescript |
| 52 | +async typeText(locator: Locator, value: string, wait: boolean = true): Promise<void> |
| 53 | +``` |
| 54 | +Types text into an input field character by character, simulating real user input. |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +### selectDropdown |
| 59 | + |
| 60 | +```typescript |
| 61 | +async selectDropdown(locator: Locator, value: string, wait: boolean = true): Promise<void> |
| 62 | +``` |
| 63 | +Selects an option from a dropdown by value. |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +### getElementText |
| 68 | + |
| 69 | +```typescript |
| 70 | +async getElementText(locator: Locator, wait: boolean = true): Promise<string> |
| 71 | +``` |
| 72 | +Retrieves the text content of an element. |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +### isElementVisible |
| 77 | + |
| 78 | +```typescript |
| 79 | +async isElementVisible(locator: Locator): Promise<boolean> |
| 80 | +``` |
| 81 | +Checks if an element is visible on the page. |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +## Logging |
| 86 | + |
| 87 | +All actions are logged using the shared logger utility for traceability and easier debugging. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## Usage Example |
| 92 | + |
| 93 | +```typescript |
| 94 | +import { BasePage } from './BasePage'; |
| 95 | + |
| 96 | +class LoginPage extends BasePage { |
| 97 | + // Define locators and page-specific methods here |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Maintenance Tips |
| 104 | + |
| 105 | +- Add new generic element actions here so all pages benefit. |
| 106 | +- Avoid page-specific logic in `BasePage`. |
| 107 | +- Use logs to diagnose flaky tests or UI issues |
0 commit comments