|
1 | | -import { Locator } from '@playwright/test'; |
2 | | -import { logger } from '../utils/logger'; // adjust path based on your project |
3 | | -import { expect } from '@playwright/test'; |
| 1 | +import { Locator } from "@playwright/test"; |
| 2 | +import { logger } from "../utils/logger"; |
4 | 3 |
|
5 | 4 | export class BasePage { |
6 | | - /** |
7 | | - * Waits for an element with retry and timeout support. |
8 | | - */ |
9 | | - async waitForElement( |
10 | | - locator: Locator, |
11 | | - timeout: number = 5000, |
12 | | - maxRetries: number = 3, |
13 | | - delayBetweenRetries: number = 1000 |
14 | | - ): Promise<void> { |
15 | | - let attempt = 0; |
16 | | - while (attempt < maxRetries) { |
17 | | - try { |
18 | | - logger.info(`Attempt ${attempt + 1}: Waiting for element → ${locator.toString()}`); |
19 | | - await locator.waitFor({ timeout }); |
20 | | - logger.info(`Element is ready: ${locator.toString()}`); |
21 | | - return; |
22 | | - } catch (error) { |
23 | | - logger.warn(`Attempt ${attempt + 1} failed: ${(error as Error).message}`); |
24 | | - attempt++; |
25 | | - if (attempt < maxRetries) { |
26 | | - await new Promise(res => setTimeout(res, delayBetweenRetries)); |
27 | | - } else { |
28 | | - logger.error(`Element not found after ${maxRetries} retries: ${locator.toString()}`); |
29 | | - throw new Error(`Failed to locate element after ${maxRetries} retries: ${locator.toString()}`); |
30 | | - } |
31 | | - } |
32 | | - } |
33 | | - } |
| 5 | + async clickElement(locator: Locator, wait: boolean = true): Promise<void> { |
| 6 | + if (wait) await this.waitForElement(locator); |
| 7 | + await locator.click(); |
| 8 | + logger.info(`Clicked on element: ${locator.toString()}`); |
| 9 | + } |
34 | 10 |
|
35 | | - async clickElement(locator: Locator, wait: boolean = true): Promise<void> { |
36 | | - if (wait) await this.waitForElement(locator); |
37 | | - await locator.click(); |
38 | | - logger.info(`Clicked on element: ${locator.toString()}`); |
39 | | - } |
| 11 | + async enterText( |
| 12 | + locator: Locator, |
| 13 | + value: string, |
| 14 | + wait: boolean = true |
| 15 | + ): Promise<void> { |
| 16 | + if (wait) await this.waitForElement(locator); |
| 17 | + const response = await locator.fill(value); |
40 | 18 |
|
41 | | - async enterText(locator: Locator, value: string, wait: boolean = true): Promise<void> { |
42 | | - if (wait) await this.waitForElement(locator); |
43 | | - const response = await locator.fill(value); |
44 | | - |
45 | | - logger.info(`Filled input ${locator.toString()} with value: ${value}`); |
46 | | - } |
| 19 | + logger.info(`Filled input ${locator.toString()} with value: ${value}`); |
| 20 | + } |
47 | 21 |
|
48 | | - async typeText(locator: Locator, value: string, wait: boolean = true): Promise<void> { |
49 | | - if (wait) await this.waitForElement(locator); |
50 | | - await locator.type(value); |
51 | | - logger.info(`Typed into ${locator.toString()} with value: ${value}`); |
52 | | - } |
| 22 | + async typeText( |
| 23 | + locator: Locator, |
| 24 | + value: string, |
| 25 | + wait: boolean = true |
| 26 | + ): Promise<void> { |
| 27 | + if (wait) await this.waitForElement(locator); |
| 28 | + await locator.type(value); |
| 29 | + logger.info(`Typed into ${locator.toString()} with value: ${value}`); |
| 30 | + } |
53 | 31 |
|
54 | | - async selectDropdown(locator: Locator, value: string, wait: boolean = true): Promise<void> { |
55 | | - if (wait) await this.waitForElement(locator); |
56 | | - await locator.selectOption(value); |
57 | | - logger.info(`Selected dropdown value '${value}' on ${locator.toString()}`); |
58 | | - } |
| 32 | + async selectDropdown( |
| 33 | + locator: Locator, |
| 34 | + value: string, |
| 35 | + wait: boolean = true |
| 36 | + ): Promise<void> { |
| 37 | + if (wait) await this.waitForElement(locator); |
| 38 | + await locator.selectOption(value); |
| 39 | + logger.info(`Selected dropdown value '${value}' on ${locator.toString()}`); |
| 40 | + } |
| 41 | + |
| 42 | + async getElementText( |
| 43 | + locator: Locator, |
| 44 | + wait: boolean = true |
| 45 | + ): Promise<string> { |
| 46 | + if (wait) await this.waitForElement(locator); |
| 47 | + const text = await locator.textContent(); |
| 48 | + logger.info(`Extracted text from ${locator.toString()}: ${text}`); |
| 49 | + return text || ""; |
| 50 | + } |
59 | 51 |
|
60 | | - async getElementText(locator: Locator, wait: boolean = true): Promise<string> { |
61 | | - if (wait) await this.waitForElement(locator); |
62 | | - const text = await locator.textContent(); |
63 | | - logger.info(`Extracted text from ${locator.toString()}: ${text}`); |
64 | | - return text || ''; |
| 52 | + async isElementVisible(locator: Locator): Promise<boolean> { |
| 53 | + try { |
| 54 | + logger.info(`Verifying the visibility of locator: ${locator.toString()}`); |
| 55 | + return await locator.isVisible(); |
| 56 | + } catch { |
| 57 | + return false; |
65 | 58 | } |
| 59 | + } |
66 | 60 |
|
67 | | - async isElementVisible(locator: Locator): Promise<boolean> { |
68 | | - try { |
69 | | - logger.info(`Verifying the visibility of locator: ${locator.toString()}`); |
70 | | - return await locator.isVisible(); |
71 | | - } catch { |
72 | | - return false; |
| 61 | + async waitForElement( |
| 62 | + locator: Locator, |
| 63 | + timeout: number = 5000, |
| 64 | + maxRetries: number = 3, |
| 65 | + delayBetweenRetries: number = 1000 |
| 66 | + ): Promise<void> { |
| 67 | + let attempt = 0; |
| 68 | + while (attempt < maxRetries) { |
| 69 | + try { |
| 70 | + logger.info( |
| 71 | + `Attempt ${attempt + 1}: Waiting for element → ${locator.toString()}` |
| 72 | + ); |
| 73 | + await locator.waitFor({ timeout }); |
| 74 | + logger.info(`Element is ready: ${locator.toString()}`); |
| 75 | + return; |
| 76 | + } catch (error) { |
| 77 | + logger.warn( |
| 78 | + `Attempt ${attempt + 1} failed: ${(error as Error).message}` |
| 79 | + ); |
| 80 | + attempt++; |
| 81 | + if (attempt < maxRetries) { |
| 82 | + await new Promise((res) => setTimeout(res, delayBetweenRetries)); |
| 83 | + } else { |
| 84 | + logger.error( |
| 85 | + `Element not found after ${maxRetries} retries: ${locator.toString()}` |
| 86 | + ); |
| 87 | + throw new Error( |
| 88 | + `Failed to locate element after ${maxRetries} retries: ${locator.toString()}` |
| 89 | + ); |
73 | 90 | } |
| 91 | + } |
74 | 92 | } |
| 93 | + } |
75 | 94 | } |
0 commit comments