|
| 1 | +using Microsoft.Playwright; |
| 2 | +using System; |
| 3 | +using System.Threading.Tasks; |
| 4 | + |
| 5 | +namespace QA_Automation_Framework_Playwright.Pages |
| 6 | +{ |
| 7 | + public class HomePage |
| 8 | + { |
| 9 | + private readonly IPage _page; |
| 10 | + |
| 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 | + ); |
| 56 | + |
| 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 | + } |
| 73 | + |
| 74 | + throw new TimeoutException($"❌ Product '{productName}' not found after 3 retries."); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + public async Task AddToCartAsync() |
| 79 | + { |
| 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); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments