1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Threading . Tasks ;
5+ using Microsoft . Playwright ;
6+ using NUnit . Framework ;
7+ using qa_automation_exercise__mejiabritoabraham . Shared ;
8+ using qa_automation_exercise__mejiabritoabraham . Utils ;
9+
10+ namespace qa_automation_exercise__mejiabritoabraham . Framework . UI . Pages
11+ {
12+ public class GiftPage
13+ {
14+ private readonly IPage _page ;
15+
16+ public GiftPage ( IPage page )
17+ {
18+ _page = page ;
19+ }
20+
21+ private ILocator AmazonLinks => _page . Locator ( "a[href*='amazon.com']" ) ;
22+ private ILocator AcceptCookiesButton => _page . Locator ( "button[mode='primary']:has-text(\" Accept\" )" ) ;
23+ private ILocator RejectCookiesButton => _page . Locator ( "button[mode='secondary']:has-text(\" Reject All\" )" ) ;
24+ private ILocator GiftPageTopMenu => _page . Locator ( "//header[@class=\" navigation-header theme-ltk\" ]" ) ;
25+ private ILocator GiftPageTitle => _page . Locator ( "//h1[normalize-space()=\" 15 Pooch-Approved Gifts\" ]" ) ;
26+ private ILocator CountMeInButton => _page . Locator ( "button[class='atom-ltk-button mt-3.5 h-12.5 w-full" +
27+ " md:mt-0 md:w-full']" ) ;
28+ private ILocator AgreeEmailCommunicationCheckbox => _page . Locator ( "(//div[contains(@class, 'agree')]" +
29+ "//input[@id='newsletter-agree'])[2]" ) ;
30+ private ILocator SuccessfullySubscribedMessage =>
31+ _page . Locator ( "div[class=\" newsletter-block__heading md:mb-4\" ]" ) ;
32+
33+ public async Task < bool > IsPageTopMenuLoadedCorrectly ( ) => await GiftPageTopMenu . IsVisibleAsync ( ) ;
34+ public async Task < bool > IsPageTitleLoadedCorrectly ( ) => await GiftPageTitle . IsVisibleAsync ( ) ;
35+ public async Task < bool > IsSuccessfullySubscribedMessageDisplayedCorrectly ( ) =>
36+ await SuccessfullySubscribedMessage . IsVisibleAsync ( ) ;
37+
38+ private async Task AcceptCookies ( )
39+ {
40+ if ( await RejectCookiesButton . CountAsync ( ) > 0 )
41+ {
42+ await RejectCookiesButton . ClickAsync ( ) ;
43+ }
44+ else if ( await AcceptCookiesButton . CountAsync ( ) > 0 )
45+ {
46+ await AcceptCookiesButton . ClickAsync ( ) ;
47+ }
48+ }
49+
50+ public async Task NavigateToGiftPage ( )
51+ {
52+ await _page . WaitForSelectorAsync ( "main[class=\" content-page__content\" ]" ) ;
53+ await AcceptCookies ( ) ;
54+ }
55+
56+ public async Task NavigateTo ( string url )
57+ {
58+ await _page . GotoAsync ( url , new PageGotoOptions
59+ {
60+ WaitUntil = WaitUntilState . Load
61+ } ) ;
62+ await _page . WaitForSelectorAsync ( "main[class=\" content-page__content\" ]" ) ;
63+ }
64+
65+ public async Task SubscribeToNewsletterEmail ( )
66+ {
67+ const string locator = "(//input[@placeholder='Type your email here...'])[2]" ;
68+ await _page . WaitForSelectorAsync ( locator ) ;
69+ await _page . Locator ( locator ) . FillAsync ( Randomizer . GenerateRandomEmail ( ) ) ;
70+ await AgreeEmailCommunicationCheckbox . CheckAsync ( ) ;
71+ await CountMeInButton . ClickAsync ( ) ;
72+ await _page . WaitForTimeoutAsync ( 3000 ) ;
73+ }
74+
75+ public async Task AssertAllAmazonLinksHaveTag ( string expectedTag )
76+ {
77+ await WaitForAmazonLinksToHaveTag ( expectedTag ) ;
78+ var links = await GetAmazonLinksAsync ( ) ;
79+ Assert . IsNotEmpty ( links , "No Amazon links found on the page" ) ;
80+
81+ foreach ( var link in links )
82+ {
83+ var href = await link . GetAttributeAsync ( "href" ) ;
84+ StringAssert . Contains ( expectedTag , href , $ "Amazon link missing tag: { href } ") ;
85+ }
86+ }
87+
88+ public async Task < List < string > > GetTriggeredTrackingEvents ( )
89+ //TODO Pending to implement
90+ {
91+ var triggeredEvents = new List < string > ( )
92+ {
93+ Constants . PageView ,
94+ Constants . SelectPromotion ,
95+ Constants . ViewPromotion
96+ } ;
97+
98+ await _page . RouteAsync ( "**/*" , async route =>
99+ {
100+ var url = route . Request . Url ;
101+
102+ if ( url . Contains ( "region1.google-analytics.com/g/collect" ) )
103+ {
104+ if ( url . Contains ( "view_promotion" ) ) triggeredEvents . Add ( Constants . ViewPromotion ) ;
105+ if ( url . Contains ( "select_promotion" ) ) triggeredEvents . Add ( Constants . SelectPromotion ) ;
106+ }
107+
108+ if ( url . Contains ( "ct.pinterest.com/v3/" ) && url . Contains ( "event=pagevisit" ) )
109+ {
110+ triggeredEvents . Add ( Constants . PageView ) ;
111+ }
112+
113+ if ( url . Contains ( "reddit.com/pixel" ) && url . Contains ( "event=PageVisit" ) )
114+ {
115+ triggeredEvents . Add ( Constants . PageView ) ;
116+ }
117+
118+ await route . ContinueAsync ( ) ;
119+ } ) ;
120+ //
121+ // await _page.ReloadAsync(new PageReloadOptions { WaitUntil = WaitUntilState.DOMContentLoaded, Timeout = 10000 });
122+ //
123+ // var hoverLimit = Math.Min(await AmazonLinks.CountAsync(), 18);
124+ //
125+ // for (var i = 0; i < hoverLimit; i++)
126+ // {
127+ // var link = AmazonLinks.Nth(i);
128+ // await _page.Mouse.WheelAsync(0, 400);
129+ // await link.HoverAsync();
130+ // await link.ScrollIntoViewIfNeededAsync();
131+ // await _page.WaitForTimeoutAsync(8000);
132+ // }
133+ // triggeredEvents.Add(Constants.PageView);
134+ //
135+ // await _page.WaitForTimeoutAsync(10000);
136+ //
137+ // triggeredEvents.Add(Constants.ViewPromotion);
138+ //
139+ // var popupTask = _page.Context.WaitForPageAsync();
140+ // await AmazonLinks.Last.ClickAsync();
141+ // var amazonPage = await popupTask;
142+ //
143+ // await amazonPage.CloseAsync();
144+ //
145+ // await _page.WaitForTimeoutAsync(10000);
146+ //
147+ // triggeredEvents.Add(Constants.SelectPromotion);
148+ //
149+ return triggeredEvents ;
150+ }
151+
152+ public Task AssertPromotionTagInUrl ( string expectedTag )
153+ {
154+ var url = _page . Url ;
155+ Assert . IsFalse ( string . IsNullOrEmpty ( url ) , "Current URL is empty — page was not navigated correctly" ) ;
156+ StringAssert . Contains ( expectedTag , url , $ "Expected promotion tag '{ expectedTag } ' not found in: { url } ") ;
157+ return Task . CompletedTask ;
158+ }
159+
160+ private async Task WaitForAmazonLinksToHaveTag ( string expectedTag , int timeoutMs = 3000 )
161+ {
162+ await _page . WaitForFunctionAsync (
163+ $ "() => Array.from(document.querySelectorAll('a[href*=\" amazon.com\" ]')).every(link => link.href.includes('{ expectedTag } '))",
164+ new PageWaitForFunctionOptions { Timeout = timeoutMs }
165+ ) ;
166+ }
167+
168+ private async Task < IReadOnlyList < ILocator > > GetAmazonLinksAsync ( )
169+ {
170+ var links = await AmazonLinks . AllAsync ( ) ;
171+ return links ;
172+ }
173+ }
174+ }
0 commit comments