How to use ShouldAwaitNavigationWhenAssigningLocationTwice method of Microsoft.Playwright.Tests.PageAutoWaitingBasicTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageAutoWaitingBasicTests.ShouldAwaitNavigationWhenAssigningLocationTwice

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

PageAutoWaitingBasicTests.cs

Source: PageAutoWaitingBasicTests.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25using System.Collections.Generic;
26using System.Threading.Tasks;
27using Microsoft.AspNetCore.Http;
28using Microsoft.Playwright.NUnit;
29using NUnit.Framework;
30
31namespace Microsoft.Playwright.Tests
32{
33    public class PageAutoWaitingBasicTests : PageTestEx
34    {
35        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should await navigation when clicking anchor")]
36        [Ignore("Flacky")]
37        public async Task ShouldAwaitNavigationWhenClickingAnchor()
38        {
39            var messages = new List<string>();
40            Server.SetRoute("/empty.html", context =>
41            {
42                messages.Add("route");
43                context.Response.ContentType = "text/html";
44                return context.Response.WriteAsync("<link rel='stylesheet' href='./one-style.css'>");
45            });
46
47            await Page.SetContentAsync($"<a href=\"{Server.EmptyPage}\">empty.html</a>");
48            await TaskUtils.WhenAll(
49                Page.ClickAsync("a").ContinueWith(_ => messages.Add("click")),
50                Page.WaitForNavigationAsync().ContinueWith(_ => messages.Add("navigated")));
51
52            Assert.AreEqual("route|navigated|click", string.Join("|", messages));
53        }
54
55        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should await cross-process navigation when clicking anchor")]
56        [Ignore("Flacky")]
57        public async Task ShouldAwaitCrossProcessNavigationWhenClickingAnchor()
58        {
59            var messages = new List<string>();
60            Server.SetRoute("/empty.html", context =>
61            {
62                messages.Add("route");
63                context.Response.ContentType = "text/html";
64                return context.Response.WriteAsync("<link rel='stylesheet' href='./one-style.css'>");
65            });
66
67            await Page.SetContentAsync($"<a href=\"{Server.CrossProcessPrefix}/empty.html\">empty.html</a>");
68            await TaskUtils.WhenAll(
69                Page.ClickAsync("a").ContinueWith(_ => messages.Add("click")),
70                Page.WaitForNavigationAsync().ContinueWith(_ => messages.Add("navigated")));
71
72            Assert.AreEqual("route|navigated|click", string.Join("|", messages));
73        }
74
75        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should await form-get on click")]
76        [Ignore("Flacky")]
77        public async Task ShouldAwaitFormGetOnClick()
78        {
79            var messages = new List<string>();
80            Server.SetRoute("/empty.html?foo=bar", context =>
81            {
82                messages.Add("route");
83                context.Response.ContentType = "text/html";
84                return context.Response.WriteAsync("<link rel='stylesheet' href='./one-style.css'>");
85            });
86
87            await Page.SetContentAsync([email protected]"
88                <form action=""{Server.EmptyPage}"" method=""get"">
89                    <input name=""foo"" value=""bar"">
90                    <input type=""submit"" value=""Submit"">
91                </form>");
92
93            await TaskUtils.WhenAll(
94                Page.ClickAsync("input[type=submit]").ContinueWith(_ => messages.Add("click")),
95                Page.WaitForNavigationAsync().ContinueWith(_ => messages.Add("navigated")));
96
97            Assert.AreEqual("route|navigated|click", string.Join("|", messages));
98        }
99
100        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should await form-post on click")]
101        [Ignore("Flacky")]
102        public async Task ShouldAwaitFormPostOnClick()
103        {
104            var messages = new List<string>();
105            Server.SetRoute("/empty.html", context =>
106            {
107                messages.Add("route");
108                context.Response.ContentType = "text/html";
109                return context.Response.WriteAsync("<link rel='stylesheet' href='./one-style.css'>");
110            });
111
112            await Page.SetContentAsync([email protected]"
113                <form action=""{ Server.EmptyPage}"" method=""post"">
114                    <input name=""foo"" value=""bar"">
115                    <input type=""submit"" value=""Submit"">
116                </form>");
117
118            await TaskUtils.WhenAll(
119                Page.ClickAsync("input[type=submit]").ContinueWith(_ => messages.Add("click")),
120                Page.WaitForNavigationAsync().ContinueWith(_ => messages.Add("navigated")));
121
122            Assert.AreEqual("route|navigated|click", string.Join("|", messages));
123        }
124
125        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should await navigation when assigning location")]
126        [Ignore("Flacky")]
127        public async Task ShouldAwaitNavigationWhenAssigningLocation()
128        {
129            var messages = new List<string>();
130            Server.SetRoute("/empty.html", context =>
131            {
132                messages.Add("route");
133                context.Response.ContentType = "text/html";
134                return context.Response.WriteAsync("<link rel='stylesheet' href='./one-style.css'>");
135            });
136
137            await TaskUtils.WhenAll(
138                Page.EvaluateAsync($"window.location.href = '{Server.EmptyPage}'").ContinueWith(_ => messages.Add("evaluate")),
139                Page.WaitForNavigationAsync().ContinueWith(_ => messages.Add("navigated")));
140
141            Assert.AreEqual("route|navigated|evaluate", string.Join("|", messages));
142        }
143
144        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should await navigation when assigning location twice")]
145        public async Task ShouldAwaitNavigationWhenAssigningLocationTwice()
146        {
147            var messages = new List<string>();
148            Server.SetRoute("/empty.html?cancel", context =>
149            {
150                return context.Response.WriteAsync("done");
151            });
152
153            Server.SetRoute("/empty.html?override", context =>
154            {
155                messages.Add("routeoverride");
156                return context.Response.WriteAsync("done");
157            });
158
159            await Page.EvaluateAsync([email protected]"
160                window.location.href = '{Server.EmptyPage}?cancel';
161                window.location.href = '{Server.EmptyPage}?override';");
162            messages.Add("evaluate");
163
164            Assert.AreEqual("routeoverride|evaluate", string.Join("|", messages));
165        }
166
167        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should await navigation when evaluating reload")]
168        [Ignore("Flacky")]
169        public async Task ShouldAwaitNavigationWhenEvaluatingReload()
170        {
171            var messages = new List<string>();
172            await Page.GotoAsync(Server.EmptyPage);
173            Server.SetRoute("/empty.html", context =>
174            {
175                messages.Add("route");
176                context.Response.ContentType = "text/html";
177                return context.Response.WriteAsync("<link rel='stylesheet' href='./one-style.css'>");
178            });
179
180            await TaskUtils.WhenAll(
181                Page.EvaluateAsync($"window.location.reload();").ContinueWith(_ => messages.Add("evaluate")),
182                Page.WaitForNavigationAsync().ContinueWith(_ => messages.Add("navigated")));
183
184            Assert.AreEqual("route|navigated|evaluate", string.Join("|", messages));
185        }
186
187        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should await navigating specified target")]
188        [Ignore("Flacky")]
189        public async Task ShouldAwaitNavigatingSpecifiedTarget()
190        {
191            var messages = new List<string>();
192            Server.SetRoute("/empty.html", context =>
193            {
194                messages.Add("route");
195                context.Response.ContentType = "text/html";
196                return context.Response.WriteAsync("<link rel='stylesheet' href='./one-style.css'>");
197            });
198
199            await Page.SetContentAsync([email protected]"
200                <a href=""{ Server.EmptyPage}"" target=target>empty.html</a>
201                <iframe name=target></iframe>");
202
203            var frame = Page.Frame("target");
204
205            await TaskUtils.WhenAll(
206                Page.ClickAsync("a").ContinueWith(_ => messages.Add("click")),
207                Page.WaitForNavigationAsync().ContinueWith(_ => messages.Add("navigated")));
208
209            Assert.AreEqual(Server.EmptyPage, frame.Url);
210            Assert.AreEqual("route|navigated|click", string.Join("|", messages));
211        }
212
213        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should work with noWaitAfter: true")]
214        public async Task ShouldWorkWithNoWaitAfterTrue()
215        {
216            Server.SetRoute("/empty.html", _ => Task.CompletedTask);
217            await Page.SetContentAsync($"<a id=anchor href='{Server.EmptyPage}'>empty.html</a>");
218            await Page.ClickAsync("a", new() { NoWaitAfter = true });
219        }
220
221        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should work with waitForLoadState(load)")]
222        [Ignore("Flacky")]
223        public async Task ShouldWorkWithWaitForLoadStateLoad()
224        {
225            var messages = new List<string>();
226            Server.SetRoute("/empty.html", context =>
227            {
228                messages.Add("route");
229                context.Response.ContentType = "text/html";
230                return context.Response.WriteAsync("<link rel='stylesheet' href='./one-style.css'>");
231            });
232
233            await Page.SetContentAsync($"<a href=\"{Server.EmptyPage}\">empty.html</a>");
234            var clickLoaded = new TaskCompletionSource<bool>();
235
236            await TaskUtils.WhenAll(
237                Page.ClickAsync("a").ContinueWith(_ => Page.WaitForLoadStateAsync(LoadState.Load).ContinueWith(_ =>
238                {
239                    messages.Add("clickload");
240                    clickLoaded.TrySetResult(true);
241                })),
242                clickLoaded.Task,
243                Page.WaitForNavigationAsync(new() { WaitUntil = WaitUntilState.DOMContentLoaded }).ContinueWith(_ => messages.Add("domcontentloaded")));
244
245            Assert.AreEqual("route|domcontentloaded|clickload", string.Join("|", messages));
246        }
247
248        [PlaywrightTest("page-autowaiting-basic.spec.ts", "should work with goto following click")]
249        public async Task ShouldWorkWithGotoFollowingClick()
250        {
251            var messages = new List<string>();
252            Server.SetRoute("/empty.html", context =>
253            {
254                messages.Add("route");
255                context.Response.ContentType = "text/html";
256                return context.Response.WriteAsync("You are logged in");
257            });
258
259            await Page.SetContentAsync([email protected]"
260                <form action=""{ Server.EmptyPage}/login.html"" method=""get"">
261                    <input type=""text"">
262                    <input type=""submit"" value=""Submit"">
263                </form>");
264
265            await Page.FillAsync("input[type=text]", "admin");
266            await Page.ClickAsync("input[type=submit]");
267            await Page.GotoAsync(Server.EmptyPage);
268        }
269    }
270}
271
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

Trigger ShouldAwaitNavigationWhenAssigningLocationTwice code on LambdaTest Cloud Grid

Execute automation tests with ShouldAwaitNavigationWhenAssigningLocationTwice 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)