How to use ShouldNotLeakListenersDuringNavigation method of Microsoft.Playwright.Tests.PageGotoTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageGotoTests.ShouldNotLeakListenersDuringNavigation

Run Playwright-dotnet automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

PageGotoTests.cs

Source: PageGotoTests.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) 2020 Darío Kondratiuk
5 * Modifications copyright (c) Microsoft Corporation.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26using System;
27using System.Collections.Generic;
28using System.IO;
29using System.Net;
30using System.Threading.Tasks;
31using Microsoft.AspNetCore.Http;
32using Microsoft.Playwright.NUnit;
33using NUnit.Framework;
34
35namespace Microsoft.Playwright.Tests
36{
37    public class PageGotoTests : PageTestEx
38    {
39        [PlaywrightTest("page-goto.spec.ts", "should work")]
40        public async Task ShouldWork()
41        {
42            await Page.GotoAsync(Server.EmptyPage);
43            Assert.AreEqual(Server.EmptyPage, Page.Url);
44        }
45
46        [PlaywrightTest("page-goto.spec.ts", "should work with file URL")]
47        public async Task ShouldWorkWithFileURL()
48        {
49            string fileurl = new Uri(TestUtils.GetAsset(Path.Combine("frames", "two-frames.html"))).AbsoluteUri;
50            await Page.GotoAsync(fileurl);
51            Assert.AreEqual(fileurl.ToLower(), Page.Url.ToLower());
52            Assert.AreEqual(3, Page.Frames.Count);
53        }
54
55        [PlaywrightTest("page-goto.spec.ts", "should use http for no protocol")]
56        public async Task ShouldUseHttpForNoProtocol()
57        {
58            await Page.GotoAsync(Server.EmptyPage.Replace("http://", string.Empty));
59            Assert.AreEqual(Server.EmptyPage, Page.Url);
60        }
61
62        [PlaywrightTest("page-goto.spec.ts", "should work cross-process")]
63        public async Task ShouldWorkCrossProcess()
64        {
65            await Page.GotoAsync(Server.EmptyPage);
66            Assert.AreEqual(Server.EmptyPage, Page.Url);
67
68            string url = Server.CrossProcessPrefix + "/empty.html";
69            IFrame requestFrame = null;
70            Page.Request += (_, e) =>
71            {
72                if (e.Url == url)
73                {
74                    requestFrame = e.Frame;
75                }
76            };
77
78            var response = await Page.GotoAsync(url);
79            Assert.AreEqual(url, Page.Url);
80            Assert.AreEqual(Page.MainFrame, response.Frame);
81            Assert.AreEqual(Page.MainFrame, requestFrame);
82            Assert.AreEqual(url, response.Url);
83        }
84
85        [PlaywrightTest("page-goto.spec.ts", "should capture iframe navigation request")]
86        public async Task ShouldCaptureIframeNavigationRequest()
87        {
88            await Page.GotoAsync(Server.EmptyPage);
89            Assert.AreEqual(Server.EmptyPage, Page.Url);
90
91            IFrame requestFrame = null;
92            Page.Request += (_, e) =>
93            {
94                if (e.Url == Server.Prefix + "/frames/frame.html")
95                {
96                    requestFrame = e.Frame;
97                }
98            };
99
100            var response = await Page.GotoAsync(Server.Prefix + "/frames/one-frame.html");
101            Assert.AreEqual(Server.Prefix + "/frames/one-frame.html", Page.Url);
102            Assert.AreEqual(Page.MainFrame, response.Frame);
103            Assert.AreEqual(Server.Prefix + "/frames/one-frame.html", response.Url);
104
105            Assert.AreEqual(2, Page.Frames.Count);
106            Assert.AreEqual(Page.FirstChildFrame(), requestFrame);
107        }
108
109        [PlaywrightTest("page-goto.spec.ts", "should capture cross-process iframe navigation request")]
110        public async Task ShouldCaptureCrossProcessIframeNavigationRequest()
111        {
112            await Page.GotoAsync(Server.EmptyPage);
113            Assert.AreEqual(Server.EmptyPage, Page.Url);
114
115            IFrame requestFrame = null;
116            Page.Request += (_, e) =>
117            {
118                if (e.Url == Server.CrossProcessPrefix + "/frames/frame.html")
119                {
120                    requestFrame = e.Frame;
121                }
122            };
123
124            var response = await Page.GotoAsync(Server.CrossProcessPrefix + "/frames/one-frame.html");
125            Assert.AreEqual(Server.CrossProcessPrefix + "/frames/one-frame.html", Page.Url);
126            Assert.AreEqual(Page.MainFrame, response.Frame);
127            Assert.AreEqual(Server.CrossProcessPrefix + "/frames/one-frame.html", response.Url);
128
129            Assert.AreEqual(2, Page.Frames.Count);
130            Assert.AreEqual(Page.FirstChildFrame(), requestFrame);
131        }
132
133        [PlaywrightTest("page-goto.spec.ts", "should work with anchor navigation")]
134        public async Task ShouldWorkWithAnchorNavigation()
135        {
136            await Page.GotoAsync(Server.EmptyPage);
137            Assert.AreEqual(Server.EmptyPage, Page.Url);
138            await Page.GotoAsync($"{Server.EmptyPage}#foo");
139            Assert.AreEqual($"{Server.EmptyPage}#foo", Page.Url);
140            await Page.GotoAsync($"{Server.EmptyPage}#bar");
141            Assert.AreEqual($"{Server.EmptyPage}#bar", Page.Url);
142        }
143
144        [PlaywrightTest("page-goto.spec.ts", "should work with redirects")]
145        public async Task ShouldWorkWithRedirects()
146        {
147            Server.SetRedirect("/redirect/1.html", "/redirect/2.html");
148            Server.SetRedirect("/redirect/2.html", "/empty.html");
149
150            var response = await Page.GotoAsync(Server.Prefix + "/redirect/1.html");
151            Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
152            await Page.GotoAsync(Server.EmptyPage);
153        }
154
155        [PlaywrightTest("page-goto.spec.ts", "should navigate to about:blank")]
156        public async Task ShouldNavigateToAboutBlank()
157        {
158            var response = await Page.GotoAsync(TestConstants.AboutBlank);
159            Assert.Null(response);
160        }
161
162        [PlaywrightTest("page-goto.spec.ts", "should return response when page changes its URL after load")]
163        public async Task ShouldReturnResponseWhenPageChangesItsURLAfterLoad()
164        {
165            var response = await Page.GotoAsync(Server.Prefix + "/historyapi.html");
166            Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
167        }
168
169        [PlaywrightTest("page-goto.spec.ts", "should work with subframes return 204")]
170        public Task ShouldWorkWithSubframesReturn204()
171        {
172            Server.SetRoute("/frames/frame.html", context =>
173            {
174                context.Response.StatusCode = 204;
175                return Task.CompletedTask;
176            });
177            return Page.GotoAsync(Server.Prefix + "/frames/one-frame.html");
178        }
179
180        [PlaywrightTest("page-goto.spec.ts", "should work with subframes return 204")]
181        public async Task ShouldFailWhenServerReturns204()
182        {
183            Server.SetRoute("/empty.html", context =>
184            {
185                context.Response.StatusCode = 204;
186                return Task.CompletedTask;
187            });
188            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(
189                () => Page.GotoAsync(Server.EmptyPage));
190
191            if (TestConstants.IsChromium)
192            {
193                StringAssert.Contains("net::ERR_ABORTED", exception.Message);
194            }
195            else if (TestConstants.IsFirefox)
196            {
197                StringAssert.Contains("NS_BINDING_ABORTED", exception.Message);
198            }
199            else
200            {
201                StringAssert.Contains("Aborted: 204 No Content", exception.Message);
202            }
203        }
204
205        [PlaywrightTest("page-goto.spec.ts", "should navigate to empty page with domcontentloaded")]
206        public async Task ShouldNavigateToEmptyPageWithDOMContentLoaded()
207        {
208            var response = await Page.GotoAsync(Server.EmptyPage, new() { WaitUntil = WaitUntilState.DOMContentLoaded });
209            Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
210        }
211
212        [PlaywrightTest("page-goto.spec.ts", "should work when page calls history API in beforeunload")]
213        public async Task ShouldWorkWhenPageCallsHistoryAPIInBeforeunload()
214        {
215            await Page.GotoAsync(Server.EmptyPage);
216            await Page.EvaluateAsync(@"() =>
217            {
218                window.addEventListener('beforeunload', () => history.replaceState(null, 'initial', window.location.href), false);
219            }");
220            var response = await Page.GotoAsync(Server.Prefix + "/grid.html");
221            Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
222        }
223
224        [PlaywrightTest("page-goto.spec.ts", "should fail when navigating to bad url")]
225        public async Task ShouldFailWhenNavigatingToBadUrl()
226        {
227            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.GotoAsync("asdfasdf"));
228            if (TestConstants.IsChromium || TestConstants.IsWebKit)
229            {
230                StringAssert.Contains("Cannot navigate to invalid URL", exception.Message);
231            }
232            else
233            {
234                StringAssert.Contains("Invalid url", exception.Message);
235            }
236        }
237
238        [PlaywrightTest("page-goto.spec.ts", "should fail when navigating to bad SSL")]
239        public async Task ShouldFailWhenNavigatingToBadSSL()
240        {
241            Page.Request += (_, e) => Assert.NotNull(e);
242            Page.RequestFinished += (_, e) => Assert.NotNull(e);
243            Page.RequestFailed += (_, e) => Assert.NotNull(e);
244
245            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.GotoAsync(HttpsServer.Prefix + "/empty.html"));
246            TestUtils.AssertSSLError(exception.Message);
247        }
248
249        [PlaywrightTest("page-goto.spec.ts", "should fail when navigating to bad SSL after redirects")]
250        public async Task ShouldFailWhenNavigatingToBadSSLAfterRedirects()
251        {
252            Server.SetRedirect("/redirect/1.html", "/redirect/2.html");
253            Server.SetRedirect("/redirect/2.html", "/empty.html");
254            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.GotoAsync(HttpsServer.Prefix + "/redirect/1.html"));
255            TestUtils.AssertSSLError(exception.Message);
256        }
257
258        [PlaywrightTest("page-goto.spec.ts", "should not crash when navigating to bad SSL after a cross origin navigation")]
259        public async Task ShouldNotCrashWhenNavigatingToBadSSLAfterACrossOriginNavigation()
260        {
261            await Page.GotoAsync(Server.CrossProcessPrefix + "/empty.html");
262            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.GotoAsync(HttpsServer.Prefix + "/empty.html"));
263            TestUtils.AssertSSLError(exception.Message);
264        }
265
266        [PlaywrightTest("page-goto.spec.ts", "should throw if networkidle0 is passed as an option")]
267        [Ignore("We don't need this test")]
268        public void ShouldThrowIfNetworkIdle0IsPassedAsAnOption()
269        { }
270
271        [PlaywrightTest("page-goto.spec.ts", "should throw if networkidle2 is passed as an option")]
272        [Ignore("We don't need this test")]
273        public void ShouldThrowIfNetworkIdle2IsPassedAsAnOption()
274        { }
275
276        [PlaywrightTest("page-goto.spec.ts", "should throw if networkidle is passed as an option")]
277        public async Task ShouldFailWhenMainResourcesFailedToLoad()
278        {
279            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.GotoAsync("http://localhost:44123/non-existing-url"));
280
281            if (TestConstants.IsChromium)
282            {
283                StringAssert.Contains("net::ERR_CONNECTION_REFUSED", exception.Message);
284            }
285            else if (TestConstants.IsWebKit && TestConstants.IsWindows)
286            {
287                StringAssert.Contains("Couldn't connect to server", exception.Message);
288            }
289            else if (TestConstants.IsWebKit)
290            {
291                StringAssert.Contains("Could not connect", exception.Message);
292            }
293            else
294            {
295                StringAssert.Contains("NS_ERROR_CONNECTION_REFUSED", exception.Message);
296            }
297        }
298
299        [PlaywrightTest("page-goto.spec.ts", "should fail when exceeding maximum navigation timeout")]
300        public async Task ShouldFailWhenExceedingMaximumNavigationTimeout()
301        {
302            Server.SetRoute("/empty.html", _ => Task.Delay(-1));
303            var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(()
304                => Page.GotoAsync(Server.EmptyPage, new() { Timeout = 1 }));
305            StringAssert.Contains("Timeout 1ms exceeded", exception.Message);
306            StringAssert.Contains(Server.EmptyPage, exception.Message);
307        }
308
309        [PlaywrightTest("page-goto.spec.ts", "should fail when exceeding maximum navigation timeout")]
310        public async Task ShouldFailWhenExceedingDefaultMaximumNavigationTimeout()
311        {
312            Server.SetRoute("/empty.html", _ => Task.Delay(-1));
313            Page.Context.SetDefaultNavigationTimeout(2);
314            Page.SetDefaultNavigationTimeout(1);
315            var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(() => Page.GotoAsync(Server.EmptyPage));
316            StringAssert.Contains("Timeout 1ms exceeded", exception.Message);
317            StringAssert.Contains(Server.EmptyPage, exception.Message);
318        }
319
320        [PlaywrightTest("page-goto.spec.ts", "should fail when exceeding browser context navigation timeout")]
321        public async Task ShouldFailWhenExceedingBrowserContextNavigationTimeout()
322        {
323            Server.SetRoute("/empty.html", _ => Task.Delay(-1));
324            Page.Context.SetDefaultNavigationTimeout(2);
325            var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(() => Page.GotoAsync(Server.EmptyPage));
326            StringAssert.Contains("Timeout 2ms exceeded", exception.Message);
327            StringAssert.Contains(Server.EmptyPage, exception.Message);
328        }
329
330        [PlaywrightTest("page-goto.spec.ts", "should fail when exceeding default maximum timeout")]
331        public async Task ShouldFailWhenExceedingDefaultMaximumTimeout()
332        {
333            Server.SetRoute("/empty.html", _ => Task.Delay(-1));
334            Page.Context.SetDefaultTimeout(2);
335            Page.SetDefaultTimeout(1);
336            var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(() => Page.GotoAsync(Server.EmptyPage));
337            StringAssert.Contains("Timeout 1ms exceeded", exception.Message);
338            StringAssert.Contains(Server.EmptyPage, exception.Message);
339        }
340
341        [PlaywrightTest("page-goto.spec.ts", "should fail when exceeding browser context timeout")]
342        public async Task ShouldFailWhenExceedingBrowserContextTimeout()
343        {
344            Server.SetRoute("/empty.html", _ => Task.Delay(-1));
345            Page.Context.SetDefaultTimeout(2);
346            var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(() => Page.GotoAsync(Server.EmptyPage));
347            StringAssert.Contains("Timeout 2ms exceeded", exception.Message);
348            StringAssert.Contains(Server.EmptyPage, exception.Message);
349        }
350
351        [PlaywrightTest("page-goto.spec.ts", "should prioritize default navigation timeout over default timeout")]
352        public async Task ShouldPrioritizeDefaultNavigationTimeoutOverDefaultTimeout()
353        {
354            // Hang for request to the empty.html
355            Server.SetRoute("/empty.html", _ => Task.Delay(-1));
356            Page.SetDefaultTimeout(0);
357            Page.SetDefaultNavigationTimeout(1);
358            var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(() => Page.GotoAsync(Server.EmptyPage));
359            StringAssert.Contains("Timeout 1ms exceeded", exception.Message);
360            StringAssert.Contains(Server.EmptyPage, exception.Message);
361        }
362
363        [PlaywrightTest("page-goto.spec.ts", "should disable timeout when its set to 0")]
364        public async Task ShouldDisableTimeoutWhenItsSetTo0()
365        {
366            bool loaded = false;
367            void OnLoad(object sender, IPage e)
368            {
369                loaded = true;
370                Page.Load -= OnLoad;
371            }
372            Page.Load += OnLoad;
373
374            await Page.GotoAsync(Server.Prefix + "/grid.html", new() { WaitUntil = WaitUntilState.Load, Timeout = 0 });
375            Assert.True(loaded);
376        }
377
378        [PlaywrightTest("page-goto.spec.ts", "should fail when replaced by another navigation")]
379        public async Task ShouldFailWhenReplacedByAnotherNavigation()
380        {
381            Task anotherTask = null;
382
383            // Hang for request to the empty.html
384            Server.SetRoute("/empty.html", _ =>
385            {
386                anotherTask = Page.GotoAsync(Server.Prefix + "/one-style.html");
387                return Task.Delay(-1);
388            });
389
390            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.GotoAsync(Server.EmptyPage));
391            await anotherTask;
392
393            if (TestConstants.IsChromium)
394            {
395                StringAssert.Contains("net::ERR_ABORTED", exception.Message);
396            }
397            else if (TestConstants.IsWebKit)
398            {
399                StringAssert.Contains("Navigation interrupted by another one", exception.Message);
400            }
401            else
402            {
403                StringAssert.Contains("NS_BINDING_ABORTED", exception.Message);
404            }
405        }
406
407        [PlaywrightTest("page-goto.spec.ts", "should work when navigating to valid url")]
408        public async Task ShouldWorkWhenNavigatingToValidUrl()
409        {
410            var response = await Page.GotoAsync(Server.EmptyPage);
411            Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
412        }
413
414        [PlaywrightTest("page-goto.spec.ts", "should work when navigating to data url")]
415        public async Task ShouldWorkWhenNavigatingToDataUrl()
416        {
417            var response = await Page.GotoAsync("data:text/html,hello");
418            Assert.Null(response);
419        }
420
421        [PlaywrightTest("page-goto.spec.ts", "should work when navigating to 404")]
422        public async Task ShouldWorkWhenNavigatingTo404()
423        {
424            var response = await Page.GotoAsync(Server.Prefix + "/not-found");
425            Assert.AreEqual((int)HttpStatusCode.NotFound, response.Status);
426        }
427
428        [PlaywrightTest("page-goto.spec.ts", "should return last response in redirect chain")]
429        public async Task ShouldReturnLastResponseInRedirectChain()
430        {
431            Server.SetRedirect("/redirect/1.html", "/redirect/2.html");
432            Server.SetRedirect("/redirect/2.html", "/redirect/3.html");
433            Server.SetRedirect("/redirect/3.html", Server.EmptyPage);
434
435            var response = await Page.GotoAsync(Server.Prefix + "/redirect/1.html");
436            Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
437            Assert.AreEqual(Server.EmptyPage, response.Url);
438        }
439
440        [PlaywrightTest("page-goto.spec.ts", "should not leak listeners during navigation")]
441        [Ignore("We don't need this test")]
442        public void ShouldNotLeakListenersDuringNavigation()
443        { }
444
445        [PlaywrightTest("page-goto.spec.ts", "should not leak listeners during bad navigation")]
446        [Ignore("We don't need this test")]
447        public void ShouldNotLeakListenersDuringBadNavigation()
448        { }
449
450        [PlaywrightTest("page-goto.spec.ts", "should not leak listeners during navigation of 11 pages")]
451        [Ignore("We don't need this test")]
452        public void ShouldNotLeakListenersDuringNavigationOf11Pages()
453        { }
454
455        [PlaywrightTest("page-goto.spec.ts", "should navigate to dataURL and not fire dataURL requests")]
456        public async Task ShouldNavigateToDataURLAndNotFireDataURLRequests()
457        {
458            var requests = new List<IRequest>();
459            Page.Request += (_, e) => requests.Add(e);
460
461            string dataUrl = "data:text/html,<div>yo</div>";
462            var response = await Page.GotoAsync(dataUrl);
463            Assert.Null(response);
464            Assert.IsEmpty(requests);
465        }
466
467        [PlaywrightTest("page-goto.spec.ts", "should navigate to URL with hash and fire requests without hash")]
468        public async Task ShouldNavigateToURLWithHashAndFireRequestsWithoutHash()
469        {
470            var requests = new List<IRequest>();
471            Page.Request += (_, e) => requests.Add(e);
472
473            var response = await Page.GotoAsync(Server.EmptyPage + "#hash");
474            Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
475            Assert.AreEqual(Server.EmptyPage, response.Url);
476            Assert.That(requests, Has.Count.EqualTo(1));
477            Assert.AreEqual(Server.EmptyPage, requests[0].Url);
478        }
479
480        [PlaywrightTest("page-goto.spec.ts", "should work with self requesting page")]
481        public async Task ShouldWorkWithSelfRequestingPage()
482        {
483            var response = await Page.GotoAsync(Server.Prefix + "/self-request.html");
484            Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
485            StringAssert.Contains("self-request.html", response.Url);
486        }
487
488        [PlaywrightTest("page-goto.spec.ts", "should fail when navigating and show the url at the error message")]
489        public async Task ShouldFailWhenNavigatingAndShowTheUrlAtTheErrorMessage()
490        {
491            string url = HttpsServer.Prefix + "/redirect/1.html";
492            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.GotoAsync(url));
493            StringAssert.Contains(url, exception.Message);
494        }
495
496        [PlaywrightTest("page-goto.spec.ts", "should be able to navigate to a page controlled by service worker")]
497        public async Task ShouldBeAbleToNavigateToAPageControlledByServiceWorker()
498        {
499            await Page.GotoAsync(Server.Prefix + "/serviceworkers/fetch/sw.html");
500            await Page.EvaluateAsync("() => window.activationPromise");
501            await Page.GotoAsync(Server.Prefix + "/serviceworkers/fetch/sw.html");
502        }
503
504        [PlaywrightTest("page-goto.spec.ts", "should send referer")]
505        public async Task ShouldSendReferer()
506        {
507            string referer1 = null;
508            string referer2 = null;
509
510            await TaskUtils.WhenAll(
511                Server.WaitForRequest("/grid.html", r => referer1 = r.Headers["Referer"]),
512                Server.WaitForRequest("/digits/1.png", r => referer2 = r.Headers["Referer"]),
513                Page.GotoAsync(Server.Prefix + "/grid.html", new() { Referer = "http://google.com/" })
514            );
515
516            Assert.AreEqual("http://google.com/", referer1);
517            // Make sure subresources do not inherit referer.
518            Assert.AreEqual(Server.Prefix + "/grid.html", referer2);
519            Assert.AreEqual(Server.Prefix + "/grid.html", Page.Url);
520        }
521
522        [PlaywrightTest("page-goto.spec.ts", "should reject referer option when setExtraHTTPHeaders provides referer")]
523        public async Task ShouldRejectRefererOptionWhenSetExtraHTTPHeadersProvidesReferer()
524        {
525            await Page.SetExtraHTTPHeadersAsync(new Dictionary<string, string>
526            {
527                ["referer"] = "http://microsoft.com/"
528            });
529
530            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() =>
531                Page.GotoAsync(Server.Prefix + "/grid.html", new() { Referer = "http://google.com/" }));
532
533            StringAssert.Contains("\"referer\" is already specified as extra HTTP header", exception.Message);
534            StringAssert.Contains(Server.Prefix + "/grid.html", exception.Message);
535        }
536
537        [PlaywrightTest("page-goto.spec.ts", "should override referrer-policy")]
538        public async Task ShouldOverrideReferrerPolicy()
539        {
540            Server.Subscribe("/grid.html", context =>
541            {
542                context.Response.Headers["Referrer-Policy"] = "no-referrer";
543            });
544
545            string referer1 = null;
546            string referer2 = null;
547
548            var reqTask1 = Server.WaitForRequest("/grid.html", r => referer1 = r.Headers["Referer"]);
549            var reqTask2 = Server.WaitForRequest("/digits/1.png", r => referer2 = r.Headers["Referer"]);
550            await TaskUtils.WhenAll(
551                reqTask1,
552                reqTask2,
553                Page.GotoAsync(Server.Prefix + "/grid.html", new() { Referer = "http://microsoft.com/" }));
554
555            Assert.AreEqual("http://microsoft.com/", referer1);
556            // Make sure subresources do not inherit referer.
557            Assert.Null(referer2);
558            Assert.AreEqual(Server.Prefix + "/grid.html", Page.Url);
559        }
560
561        [PlaywrightTest("page-goto.spec.ts", "should fail when canceled by another navigation")]
562        public async Task ShouldFailWhenCanceledByAnotherNavigation()
563        {
564            Server.SetRoute("/one-style.html", _ => Task.Delay(10_000));
565            var request = Server.WaitForRequest("/one-style.html");
566            var failed = Page.GotoAsync(Server.Prefix + "/one-style.html", new() { WaitUntil = TestConstants.IsFirefox ? WaitUntilState.NetworkIdle : WaitUntilState.Load });
567            await request;
568            await Page.GotoAsync(Server.EmptyPage);
569
570            await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => failed);
571        }
572
573        [PlaywrightTest("page-goto.spec.ts", "should return when navigation is comitted if commit is specified")]
574        [Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]
575        public async Task ShouldReturnWhenNavigationIsComittedIfCommitIsSpecified()
576        {
577            Server.SetRoute("/empty.html", async context =>
578            {
579                context.Response.StatusCode = 200;
580                context.Response.Headers.Add("content-type", "text/html");
581                context.Response.Headers.Add("content-length", "8192");
582                // Write enought bytes of the body to trigge response received event.
583                var str = "<title>" + new string('a', 4100);
584                await context.Response.WriteAsync(str);
585                await context.Response.BodyWriter.FlushAsync();
586            });
587
588            var response = await Page.GotoAsync(Server.EmptyPage, new() { WaitUntil = TestConstants.IsFirefox ? WaitUntilState.NetworkIdle : WaitUntilState.Load });
589            Assert.AreEqual(200, response.Status);
590        }
591
592    }
593}
594
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in PageGotoTests

Trigger ShouldNotLeakListenersDuringNavigation code on LambdaTest Cloud Grid

Execute automation tests with ShouldNotLeakListenersDuringNavigation on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)