How to use BrowserContextEventsRequestfinished method of Microsoft.Playwright.Tests.BrowserContextNetworkEventTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.BrowserContextNetworkEventTests.BrowserContextEventsRequestfinished

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

BrowserContextNetworkEventTests.cs

Source: BrowserContextNetworkEventTests.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.Linq;
27using System.Threading.Tasks;
28using Microsoft.Playwright.NUnit;
29using NUnit.Framework;
30
31namespace Microsoft.Playwright.Tests
32{
33    public class BrowserContextNetworkEventTests : BrowserTestEx
34    {
35        [PlaywrightTest("browsercontext-network-event.spec.ts", "BrowserContext.Events.Request")]
36        public async Task BrowserContextEventsRequest()
37        {
38            await using var context = await Browser.NewContextAsync();
39            var page = await context.NewPageAsync();
40
41            List<IRequest> requests = new();
42            context.Request += (_, req) => requests.Add(req);
43
44            await page.GotoAsync(Server.EmptyPage);
45            await page.SetContentAsync("<a target=_blank rel=noopener href=\"/one-style.html\">yo</a>");
46
47            var page1 = await context.RunAndWaitForPageAsync(() => page.ClickAsync("a"));
48            await page1.WaitForLoadStateAsync();
49
50            var urls = requests.Select(x => x.Url).ToArray();
51            Assert.Contains(Server.EmptyPage, urls);
52            Assert.Contains($"{Server.Prefix}/one-style.html", urls);
53            Assert.Contains($"{Server.Prefix}/one-style.css", urls);
54        }
55
56        /// <playwright-file>browsercontext-network-event.spec.ts</playwright-file>
57        /// <playwright-it>BrowserContext.Events.Response</playwright-it>
58        [PlaywrightTest("browsercontext-network-event.spec.ts", "BrowserContext.Events.Response")]
59        public async Task BrowserContextEventsResponse()
60        {
61            await using var context = await Browser.NewContextAsync();
62            var page = await context.NewPageAsync();
63
64            List<IResponse> responses = new();
65            context.Response += (_, res) => responses.Add(res);
66
67            await page.GotoAsync(Server.EmptyPage);
68            await page.SetContentAsync("<a target=_blank rel=noopener href=\"/one-style.html\">yo</a>");
69
70            var page1 = await context.RunAndWaitForPageAsync(() => page.ClickAsync("a"));
71            await page1.WaitForLoadStateAsync();
72
73            var urls = responses.Select(x => x.Url).ToArray();
74            Assert.Contains(Server.EmptyPage, urls);
75            Assert.Contains($"{Server.Prefix}/one-style.html", urls);
76            Assert.Contains($"{Server.Prefix}/one-style.css", urls);
77        }
78
79        /// <playwright-file>browsercontext-network-event.spec.ts</playwright-file>
80        /// <playwright-it>BrowserContext.Events.RequestFailed</playwright-it>
81        [PlaywrightTest("browsercontext-network-event.spec.ts", "BrowserContext.Events.RequestFailed")]
82        public async Task BrowserContextEventsRequestfailed()
83        {
84            Server.SetRoute("/one-style.css", (ctx) =>
85            {
86                ctx.Response.Headers["Content-Type"] = "text/css";
87                ctx.Abort();
88                return Task.CompletedTask;
89            });
90
91            await using var context = await Browser.NewContextAsync();
92            var page = await context.NewPageAsync();
93            List<IRequest> failedRequests = new();
94            context.RequestFailed += (_, failedRequest) => failedRequests.Add(failedRequest);
95
96            await page.GotoAsync($"{Server.Prefix}/one-style.html");
97            Assert.AreEqual(1, failedRequests.Count);
98
99            var failedRequest = failedRequests[0];
100            Assert.AreEqual($"{Server.Prefix}/one-style.css", failedRequest.Url);
101
102            Assert.IsNull(await failedRequest.ResponseAsync());
103            Assert.AreEqual("stylesheet", failedRequest.ResourceType);
104            Assert.IsNotNull(failedRequest.Frame);
105        }
106
107        /// <playwright-file>browsercontext-network-event.spec.ts</playwright-file>
108        /// <playwright-it>BrowserContext.Events.RequestFinished</playwright-it>
109        [PlaywrightTest("browsercontext-network-event.spec.ts", "BrowserContext.Events.RequestFinished")]
110        public async Task BrowserContextEventsRequestfinished()
111        {
112            await using var context = await Browser.NewContextAsync();
113            var page = await context.NewPageAsync();
114
115            var request = await page.RunAndWaitForRequestFinishedAsync(() => page.GotoAsync(Server.EmptyPage));
116            Assert.AreEqual(Server.EmptyPage, request.Url);
117
118            var response = await request.ResponseAsync();
119            Assert.NotNull(response);
120            Assert.NotNull(request.Frame);
121            Assert.AreEqual(Server.EmptyPage, request.Frame.Url);
122            Assert.IsNull(request.Failure);
123        }
124
125        /// <playwright-file>browsercontext-network-event.spec.ts</playwright-file>
126        /// <playwright-it>should fire events in proper order</playwright-it>
127        [PlaywrightTest("browsercontext-network-event.spec.ts", "should fire events in proper order")]
128        public async Task ShouldFireEventsInProperOrder()
129        {
130            await using var context = await Browser.NewContextAsync();
131            var page = await context.NewPageAsync();
132
133            List<string> events = new();
134            context.Request += (_, _) => events.Add("request");
135            context.Response += (_, _) => events.Add("response");
136            context.RequestFinished += (_, _) => events.Add("requestFinished");
137
138            await page.RunAndWaitForRequestFinishedAsync(() => page.GotoAsync(Server.EmptyPage));
139
140            CollectionAssert.AreEqual(new string[]
141            {
142                "request",
143                "response",
144                "requestFinished"
145            }, events.ToArray());
146        }
147    }
148}
149
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 BrowserContextEventsRequestfinished code on LambdaTest Cloud Grid

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