|
1 | 1 | using Microsoft.Playwright; |
| 2 | +using System; |
2 | 3 | using System.Threading.Tasks; |
3 | 4 |
|
4 | 5 | namespace QA_Automation_Framework_Playwright.Pages |
5 | 6 | { |
6 | 7 | public class HomePage |
7 | 8 | { |
8 | 9 | private readonly IPage _page; |
9 | | - private readonly string _url = "https://demoblaze.com"; // sample e-commerce site |
10 | 10 |
|
11 | | - public HomePage(IPage page) => _page = page; |
| 11 | + public HomePage(IPage page) |
| 12 | + { |
| 13 | + _page = page; |
| 14 | + } |
| 15 | + |
| 16 | + public async Task NavigateToHomePageAsync() |
| 17 | + { |
| 18 | + for (int attempt = 1; attempt <= 2; attempt++) |
| 19 | + { |
| 20 | + try |
| 21 | + { |
| 22 | + await _page.GotoAsync("https://www.demoblaze.com/", new() |
| 23 | + { |
| 24 | + WaitUntil = WaitUntilState.DOMContentLoaded, |
| 25 | + Timeout = 15000 |
| 26 | + }); |
| 27 | + |
| 28 | + await _page.WaitForSelectorAsync(".hrefch", new() |
| 29 | + { |
| 30 | + State = WaitForSelectorState.Visible, |
| 31 | + Timeout = 10000 |
| 32 | + }); |
| 33 | + |
| 34 | + // Small delay for stability |
| 35 | + await _page.WaitForTimeoutAsync(500); |
| 36 | + return; // success |
| 37 | + } |
| 38 | + catch (TimeoutException) when (attempt < 2) |
| 39 | + { |
| 40 | + Console.WriteLine($"[Retry] Home page load attempt {attempt} failed, retrying..."); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public async Task ClickProductAsync(string productName) |
| 46 | +{ |
| 47 | + // Wait for the page to be interactive |
| 48 | + await _page.WaitForLoadStateAsync(LoadState.DOMContentLoaded); |
| 49 | + |
| 50 | + // Wait explicitly until product cards are loaded dynamically |
| 51 | + await _page.WaitForFunctionAsync( |
| 52 | + @"() => Array.from(document.querySelectorAll('.hrefch')).length >= 3", |
| 53 | + null, |
| 54 | + new() { Timeout = 60000 } // allow more time for GitHub runners |
| 55 | + ); |
12 | 56 |
|
13 | | - public async Task NavigateAsync() => await _page.GotoAsync(_url); |
| 57 | + // Wait until the desired product is visible |
| 58 | + var productLocator = _page.Locator($".hrefch:has-text('{productName}')").First; |
| 59 | + for (int attempt = 1; attempt <= 3; attempt++) |
| 60 | + { |
| 61 | + if (await productLocator.IsVisibleAsync()) |
| 62 | + { |
| 63 | + await productLocator.ScrollIntoViewIfNeededAsync(); |
| 64 | + await productLocator.ClickAsync(); |
| 65 | + await _page.WaitForSelectorAsync("text=Add to cart", new() { Timeout = 20000 }); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + Console.WriteLine($"[Retry {attempt}] Product '{productName}' not visible, refreshing..."); |
| 70 | + await _page.ReloadAsync(); |
| 71 | + await _page.WaitForTimeoutAsync(2000); |
| 72 | + } |
14 | 73 |
|
15 | | - public async Task ClickProductAsync(string productName) |
| 74 | + throw new TimeoutException($"❌ Product '{productName}' not found after 3 retries."); |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | + public async Task AddToCartAsync() |
16 | 79 | { |
17 | | - await _page.ClickAsync($"text={productName}"); |
| 80 | + var addToCartButton = _page.Locator("text=Add to cart"); |
| 81 | + |
| 82 | + // Handle alert dialogs |
| 83 | + _page.Dialog += async (_, dialog) => |
| 84 | + { |
| 85 | + await dialog.AcceptAsync(); |
| 86 | + }; |
| 87 | + |
| 88 | + await addToCartButton.ClickAsync(); |
| 89 | + await _page.WaitForTimeoutAsync(1000); |
18 | 90 | } |
19 | 91 | } |
20 | 92 | } |
0 commit comments