Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageRouteTests.ShouldIntercept
PageRouteTests.cs
Source:PageRouteTests.cs
...36{37 public class PageRouteTests : PageTestEx38 {39 [PlaywrightTest("page-route.spec.ts", "should intercept")]40 public async Task ShouldIntercept()41 {42 bool intercepted = false;43 await Page.RouteAsync("**/empty.html", (route) =>44 {45 StringAssert.Contains("empty.html", route.Request.Url);46#pragma warning disable 061247 Assert.False(string.IsNullOrEmpty(route.Request.Headers["user-agent"]));48#pragma warning restore 061249 Assert.AreEqual(HttpMethod.Get.Method, route.Request.Method);50 Assert.Null(route.Request.PostData);51 Assert.True(route.Request.IsNavigationRequest);52 Assert.AreEqual("document", route.Request.ResourceType);53 Assert.AreEqual(route.Request.Frame, Page.MainFrame);54 Assert.AreEqual("about:blank", route.Request.Frame.Url);55 route.ContinueAsync();56 intercepted = true;57 });58 var response = await Page.GotoAsync(Server.EmptyPage);59 Assert.True(response.Ok);60 Assert.True(intercepted);61 }62 [PlaywrightTest("page-route.spec.ts", "should unroute")]63 public async Task ShouldUnroute()64 {65 var intercepted = new List<int>();66 await Page.RouteAsync("**/*", (route) =>67 {68 intercepted.Add(1);69 route.ContinueAsync();70 });71 await Page.RouteAsync("**/empty.html", (route) =>72 {73 intercepted.Add(2);74 route.ContinueAsync();75 });76 await Page.RouteAsync("**/empty.html", (route) =>77 {78 intercepted.Add(3);79 route.ContinueAsync();80 });81 Action<IRoute> handler4 = (route) =>82 {83 intercepted.Add(4);84 route.ContinueAsync();85 };86 await Page.RouteAsync("**/empty.html", handler4);87 await Page.GotoAsync(Server.EmptyPage);88 Assert.AreEqual(new[] { 4 }, intercepted.ToArray());89 intercepted.Clear();90 await Page.UnrouteAsync("**/empty.html", handler4);91 await Page.GotoAsync(Server.EmptyPage);92 Assert.AreEqual(new[] { 3 }, intercepted.ToArray());93 intercepted.Clear();94 await Page.UnrouteAsync("**/empty.html");95 await Page.GotoAsync(Server.EmptyPage);96 Assert.AreEqual(new[] { 1 }, intercepted.ToArray());97 }98 [PlaywrightTest("page-route.spec.ts", "should work when POST is redirected with 302")]99 public async Task ShouldWorkWhenPostIsRedirectedWith302()100 {101 Server.SetRedirect("/rredirect", "/empty.html");102 await Page.GotoAsync(Server.EmptyPage);103 await Page.RouteAsync("**/*", (route) => route.ContinueAsync());104 await Page.SetContentAsync(@"105 <form action='/rredirect' method='post'>106 <input type=""hidden"" id=""foo"" name=""foo"" value=""FOOBAR"">107 </form>");108 await TaskUtils.WhenAll(109 Page.EvalOnSelectorAsync("form", "form => form.submit()"),110 Page.WaitForNavigationAsync()111 );112 }113 [PlaywrightTest("page-route.spec.ts", "should work when header manipulation headers with redirect")]114 public async Task ShouldWorkWhenHeaderManipulationHeadersWithRedirect()115 {116 Server.SetRedirect("/rrredirect", "/empty.html");117 await Page.RouteAsync("**/*", (route) =>118 {119#pragma warning disable 0612120 var headers = new Dictionary<string, string>(route.Request.Headers.ToDictionary(x => x.Key, x => x.Value)) { ["foo"] = "bar" };121#pragma warning restore 0612122 route.ContinueAsync(new() { Headers = headers });123 });124 await Page.GotoAsync(Server.Prefix + "/rrredirect");125 }126 [PlaywrightTest("page-route.spec.ts", "should be able to remove headers")]127 public async Task ShouldBeAbleToRemoveHeaders()128 {129 await Page.RouteAsync("**/*", (route) =>130 {131#pragma warning disable 0612132 var headers = new Dictionary<string, string>(route.Request.Headers.ToDictionary(x => x.Key, x => x.Value)) { ["foo"] = "bar" };133#pragma warning restore 0612134 headers.Remove("origin");135 route.ContinueAsync(new() { Headers = headers });136 });137 var originRequestHeader = Server.WaitForRequest("/empty.html", request => request.Headers["origin"]);138 await TaskUtils.WhenAll(139 originRequestHeader,140 Page.GotoAsync(Server.EmptyPage)141 );142 Assert.AreEqual(StringValues.Empty, originRequestHeader.Result);143 }144 [PlaywrightTest("page-route.spec.ts", "should contain referer header")]145 public async Task ShouldContainRefererHeader()146 {147 var requests = new List<IRequest>();148 await Page.RouteAsync("**/*", (route) =>149 {150 requests.Add(route.Request);151 route.ContinueAsync();152 });153 await Page.GotoAsync(Server.Prefix + "/one-style.html");154 StringAssert.Contains("/one-style.css", requests[1].Url);155#pragma warning disable 0612156 StringAssert.Contains("/one-style.html", requests[1].Headers["referer"]);157#pragma warning restore 0612158 }159 [PlaywrightTest("page-route.spec.ts", "should properly return navigation response when URL has cookies")]160 public async Task ShouldProperlyReturnNavigationResponseWhenURLHasCookies()161 {162 // Setup cookie.163 await Page.GotoAsync(Server.EmptyPage);164 await Context.AddCookiesAsync(new[]165 {166 new Cookie167 {168 Url = Server.EmptyPage,169 Name = "foo",170 Value = "bar"171 }172 });173 // Setup request interception.174 await Page.RouteAsync("**/*", (route) => route.ContinueAsync());175 var response = await Page.ReloadAsync();176 Assert.AreEqual((int)HttpStatusCode.OK, response.Status);177 }178 [PlaywrightTest("page-route.spec.ts", "should show custom HTTP headers")]179 public async Task ShouldShowCustomHTTPHeaders()180 {181 await Page.SetExtraHTTPHeadersAsync(new Dictionary<string, string>182 {183 ["foo"] = "bar"184 });185 await Page.RouteAsync("**/*", (route) =>186 {187#pragma warning disable 0612188 Assert.AreEqual("bar", route.Request.Headers["foo"]);189#pragma warning restore 0612190 route.ContinueAsync();191 });192 var response = await Page.GotoAsync(Server.EmptyPage);193 Assert.True(response.Ok);194 }195 [PlaywrightTest("page-route.spec.ts", "should work with redirect inside sync XHR")]196 public async Task ShouldWorkWithRedirectInsideSyncXHR()197 {198 await Page.GotoAsync(Server.EmptyPage);199 Server.SetRedirect("/logo.png", "/pptr.png");200 await Page.RouteAsync("**/*", (route) => route.ContinueAsync());201 int status = await Page.EvaluateAsync<int>(@"async () => {202 var request = new XMLHttpRequest();203 request.open('GET', '/logo.png', false); // `false` makes the request synchronous204 request.send(null);205 return request.status;206 }");207 Assert.AreEqual(200, status);208 }209 [PlaywrightTest("page-route.spec.ts", "should work with custom referer headers")]210 public async Task ShouldWorkWithCustomRefererHeaders()211 {212 await Page.SetExtraHTTPHeadersAsync(new Dictionary<string, string> { ["referer"] = Server.EmptyPage });213 await Page.RouteAsync("**/*", (route) =>214 {215 if (TestConstants.IsChromium)216 {217#pragma warning disable 0612218 Assert.AreEqual(Server.EmptyPage + ", " + Server.EmptyPage, route.Request.Headers["referer"]);219#pragma warning restore 0612220 }221 else222 {223#pragma warning disable 0612224 Assert.AreEqual(Server.EmptyPage, route.Request.Headers["referer"]);225#pragma warning restore 0612226 }227 route.ContinueAsync();228 });229 var response = await Page.GotoAsync(Server.EmptyPage);230 Assert.True(response.Ok);231 }232 [PlaywrightTest("page-route.spec.ts", "should be abortable")]233 public async Task ShouldBeAbortable()234 {235 await Page.RouteAsync(new Regex("\\.css"), (route) => route.AbortAsync());236 int failedRequests = 0;237 Page.RequestFailed += (_, _) => ++failedRequests;238 var response = await Page.GotoAsync(Server.Prefix + "/one-style.html");239 Assert.True(response.Ok);240 Assert.Null(response.Request.Failure);241 Assert.AreEqual(1, failedRequests);242 }243 [PlaywrightTest("page-route.spec.ts", "should be abortable with custom error codes")]244 public async Task ShouldBeAbortableWithCustomErrorCodes()245 {246 await Page.RouteAsync("**/*", (route) =>247 {248 route.AbortAsync(RequestAbortErrorCode.InternetDisconnected);249 });250 IRequest failedRequest = null;251 Page.RequestFailed += (_, e) => failedRequest = e;252 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.GotoAsync(Server.EmptyPage));253 Assert.NotNull(failedRequest);254 StringAssert.StartsWith(failedRequest.Failure, exception.Message);255 if (TestConstants.IsWebKit)256 {257 Assert.AreEqual("Request intercepted", failedRequest.Failure);258 }259 else if (TestConstants.IsFirefox)260 {261 Assert.AreEqual("NS_ERROR_OFFLINE", failedRequest.Failure);262 }263 else264 {265 Assert.AreEqual("net::ERR_INTERNET_DISCONNECTED", failedRequest.Failure);266 }267 }268 [PlaywrightTest("page-route.spec.ts", "should send referer")]269 public async Task ShouldSendReferer()270 {271 await Page.SetExtraHTTPHeadersAsync(new Dictionary<string, string> { ["referer"] = "http://google.com/" });272 await Page.RouteAsync("**/*", (route) => route.ContinueAsync());273#pragma warning disable 0612274 var requestTask = Server.WaitForRequest("/grid.html", request => request.Headers["referer"]);275#pragma warning restore 0612276 await TaskUtils.WhenAll(277 requestTask,278 Page.GotoAsync(Server.Prefix + "/grid.html")279 );280 Assert.AreEqual("http://google.com/", requestTask.Result);281 }282 [PlaywrightTest("page-route.spec.ts", "should fail navigation when aborting main resource")]283 public async Task ShouldFailNavigationWhenAbortingMainResource()284 {285 await Page.RouteAsync("**/*", (route) => route.AbortAsync());286 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.GotoAsync(Server.EmptyPage));287 Assert.NotNull(exception);288 if (TestConstants.IsWebKit)289 {290 StringAssert.Contains("Request intercepted", exception.Message);291 }292 else if (TestConstants.IsFirefox)293 {294 StringAssert.Contains("NS_ERROR_FAILURE", exception.Message);295 }296 else297 {298 StringAssert.Contains("net::ERR_FAILED", exception.Message);299 }300 }301 [PlaywrightTest("page-route.spec.ts", "should not work with redirects")]302 public async Task ShouldNotWorkWithRedirects()303 {304 var requests = new List<IRequest>();305 await Page.RouteAsync("**/*", (route) =>306 {307 route.ContinueAsync();308 requests.Add(route.Request);309 });310 Server.SetRedirect("/non-existing-page.html", "/non-existing-page-2.html");311 Server.SetRedirect("/non-existing-page-2.html", "/non-existing-page-3.html");312 Server.SetRedirect("/non-existing-page-3.html", "/non-existing-page-4.html");313 Server.SetRedirect("/non-existing-page-4.html", "/empty.html");314 var response = await Page.GotoAsync(Server.Prefix + "/non-existing-page.html");315 StringAssert.Contains("non-existing-page.html", requests[0].Url);316 Assert.That(requests, Has.Count.EqualTo(1));317 Assert.AreEqual("document", requests[0].ResourceType);318 Assert.True(requests[0].IsNavigationRequest);319 var chain = new List<IRequest>();320 for (var request = response.Request; request != null; request = request.RedirectedFrom)321 {322 chain.Add(request);323 Assert.True(request.IsNavigationRequest);324 }325 Assert.AreEqual(5, chain.Count);326 StringAssert.Contains("/empty.html", chain[0].Url);327 StringAssert.Contains("/non-existing-page-4.html", chain[1].Url);328 StringAssert.Contains("/non-existing-page-3.html", chain[2].Url);329 StringAssert.Contains("/non-existing-page-2.html", chain[3].Url);330 StringAssert.Contains("/non-existing-page.html", chain[4].Url);331 for (int i = 0; i < chain.Count; ++i)332 {333 var request = chain[i];334 Assert.True(request.IsNavigationRequest);335 Assert.AreEqual(i > 0 ? chain[i - 1] : null, chain[i].RedirectedTo);336 }337 }338 [PlaywrightTest("page-route.spec.ts", "should work with redirects for subresources")]339 public async Task ShouldWorkWithRedirectsForSubresources()340 {341 var requests = new List<IRequest>();342 await Page.RouteAsync("**/*", (route) =>343 {344 route.ContinueAsync();345 requests.Add(route.Request);346 });347 Server.SetRedirect("/one-style.css", "/two-style.css");348 Server.SetRedirect("/two-style.css", "/three-style.css");349 Server.SetRedirect("/three-style.css", "/four-style.css");350 Server.SetRoute("/four-style.css", context => context.Response.WriteAsync("body {box-sizing: border-box; }"));351 var response = await Page.GotoAsync(Server.Prefix + "/one-style.html");352 Assert.AreEqual((int)HttpStatusCode.OK, response.Status);353 StringAssert.Contains("one-style.html", response.Url);354 Assert.AreEqual(2, requests.Count);355 Assert.AreEqual("document", requests[0].ResourceType);356 StringAssert.Contains("one-style.html", requests[0].Url);357 var request = requests[1];358 foreach (string url in new[] { "/one-style.css", "/two-style.css", "/three-style.css", "/four-style.css" })359 {360 Assert.AreEqual("stylesheet", request.ResourceType);361 StringAssert.Contains(url, request.Url);362 request = request.RedirectedTo;363 }364 Assert.Null(request);365 }366 [PlaywrightTest("page-route.spec.ts", "should work with equal requests")]367 public async Task ShouldWorkWithEqualRequests()368 {369 await Page.GotoAsync(Server.EmptyPage);370 int responseCount = 1;371 Server.SetRoute("/zzz", context => context.Response.WriteAsync((responseCount++ * 11).ToString()));372 bool spinner = false;373 // Cancel 2nd request.374 await Page.RouteAsync("**/*", (route) =>375 {376 if (spinner)377 {378 _ = route.AbortAsync();379 }380 else381 {382 _ = route.ContinueAsync();383 }384 spinner = !spinner;385 });386 var results = new List<string>();387 for (int i = 0; i < 3; ++i)388 {389 results.Add(await Page.EvaluateAsync<string>("fetch('/zzz').then(response => response.text()).catch (e => 'FAILED')"));390 }391 Assert.AreEqual(new[] { "11", "FAILED", "22" }, results);392 }393 [PlaywrightTest("page-route.spec.ts", "should navigate to dataURL and not fire dataURL requests")]394 public async Task ShouldNavigateToDataURLAndNotFireDataURLRequests()395 {396 var requests = new List<IRequest>();397 await Page.RouteAsync("**/*", (route) =>398 {399 requests.Add(route.Request);400 route.ContinueAsync();401 });402 string dataURL = "data:text/html,<div>yo</div>";403 var response = await Page.GotoAsync(dataURL);404 Assert.Null(response);405 Assert.IsEmpty(requests);406 }407 [PlaywrightTest("page-route.spec.ts", "should be able to fetch dataURL and not fire dataURL requests")]408 public async Task ShouldBeAbleToFetchDataURLAndNotFireDataURLRequests()409 {410 await Page.GotoAsync(Server.EmptyPage);411 var requests = new List<IRequest>();412 await Page.RouteAsync("**/*", (route) =>413 {414 requests.Add(route.Request);415 route.ContinueAsync();416 });417 string dataURL = "data:text/html,<div>yo</div>";418 string text = await Page.EvaluateAsync<string>("url => fetch(url).then(r => r.text())", dataURL);419 Assert.AreEqual("<div>yo</div>", text);420 Assert.IsEmpty(requests);421 }422 [PlaywrightTest("page-route.spec.ts", "should navigate to URL with hash and and fire requests without hash")]423 public async Task ShouldNavigateToURLWithHashAndAndFireRequestsWithoutHash()424 {425 var requests = new List<IRequest>();426 await Page.RouteAsync("**/*", (route) =>427 {428 requests.Add(route.Request);429 route.ContinueAsync();430 });431 var response = await Page.GotoAsync(Server.EmptyPage + "#hash");432 Assert.AreEqual((int)HttpStatusCode.OK, response.Status);433 Assert.AreEqual(Server.EmptyPage, response.Url);434 Assert.That(requests, Has.Count.EqualTo(1));435 Assert.AreEqual(Server.EmptyPage, requests[0].Url);436 }437 [PlaywrightTest("page-route.spec.ts", "should work with encoded server")]438 public async Task ShouldWorkWithEncodedServer()439 {440 // The requestWillBeSent will report encoded URL, whereas interception will441 // report URL as-is. @see crbug.com/759388442 await Page.RouteAsync("**/*", (route) => route.ContinueAsync());443 var response = await Page.GotoAsync(Server.Prefix + "/some nonexisting page");444 Assert.AreEqual((int)HttpStatusCode.NotFound, response.Status);445 }446 [PlaywrightTest("page-route.spec.ts", "should work with badly encoded server")]447 public async Task ShouldWorkWithBadlyEncodedServer()448 {449 Server.SetRoute("/malformed?rnd=%911", _ => Task.CompletedTask);450 await Page.RouteAsync("**/*", (route) => route.ContinueAsync());451 var response = await Page.GotoAsync(Server.Prefix + "/malformed?rnd=%911");452 Assert.AreEqual((int)HttpStatusCode.OK, response.Status);453 }454 [PlaywrightTest("page-route.spec.ts", "should work with encoded server - 2")]455 public async Task ShouldWorkWithEncodedServer2()456 {457 // The requestWillBeSent will report URL as-is, whereas interception will458 // report encoded URL for stylesheet. @see crbug.com/759388459 var requests = new List<IRequest>();460 await Page.RouteAsync("**/*", (route) =>461 {462 route.ContinueAsync();463 requests.Add(route.Request);464 });465 var response = await Page.GotoAsync($"data:text/html,<link rel=\"stylesheet\" href=\"{Server.EmptyPage}/fonts?helvetica|arial\"/>");466 Assert.Null(response);467 // TODO: https://github.com/microsoft/playwright/issues/12789468 if (TestConstants.IsFirefox)469 Assert.That(requests, Has.Count.EqualTo(2));470 else471 Assert.That(requests, Has.Count.EqualTo(1));472 Assert.AreEqual((int)HttpStatusCode.NotFound, (await requests[0].ResponseAsync()).Status);473 }474 [PlaywrightTest("page-route.spec.ts", @"should not throw ""Invalid Interception Id"" if the request was cancelled")]475 public async Task ShouldNotThrowInvalidInterceptionIdIfTheRequestWasCancelled()476 {477 await Page.SetContentAsync("<iframe></iframe>");478 IRoute route = null;479 await Page.RouteAsync("**/*", (r) => route = r);480 _ = Page.EvalOnSelectorAsync("iframe", "(frame, url) => frame.src = url", Server.EmptyPage);481 // Wait for request interception.482 await Page.WaitForRequestAsync("**/*");483 // Delete frame to cause request to be canceled.484 await Page.EvalOnSelectorAsync("iframe", "frame => frame.remove()");485 await route.ContinueAsync();486 }487 [PlaywrightTest("page-route.spec.ts", "should intercept main resource during cross-process navigation")]488 public async Task ShouldInterceptMainResourceDuringCrossProcessNavigation()489 {490 await Page.GotoAsync(Server.EmptyPage);491 bool intercepted = false;492 await Page.RouteAsync(Server.CrossProcessPrefix + "/empty.html", (route) =>493 {494 if (route.Request.Url.Contains(Server.CrossProcessPrefix + "/empty.html"))495 {496 intercepted = true;497 }498 route.ContinueAsync();499 });500 var response = await Page.GotoAsync(Server.CrossProcessPrefix + "/empty.html");501 Assert.True(response.Ok);502 Assert.True(intercepted);...
ShouldIntercept
Using AI Code Generation
1{2 [PlaywrightTest("page-route.spec.ts", "should intercept")]3 [Fact(Timeout=PlaywrightSharp.Playwright.DefaultTimeout)]4 public async Task ShouldIntercept()5 {6 await Page.RouteAsync("**/*", route => Task.CompletedTask);7 var response = await Page.GotoAsync(Server.EmptyPage);8 Assert.True(response.Ok);9 }10}11{12 [PlaywrightTest("page-route.spec.ts", "should intercept")]13 [Fact(Timeout=PlaywrightSharp.Playwright.DefaultTimeout)]14 public async Task ShouldIntercept()15 {16 await Page.RouteAsync("**/*", route => Task.CompletedTask);17 var response = await Page.GotoAsync(Server.EmptyPage);18 Assert.True(response.Ok);19 }20}21{22 [PlaywrightTest("page-route.spec.ts", "should intercept")]23 [Fact(Timeout=PlaywrightSharp.Playwright.DefaultTimeout)]24 public async Task ShouldIntercept()25 {26 await Page.RouteAsync("**/*", route => Task.CompletedTask);27 var response = await Page.GotoAsync(Server.EmptyPage);28 Assert.True(response.Ok);29 }30}31{32 [PlaywrightTest("page-route.spec.ts", "should intercept")]33 [Fact(Timeout=PlaywrightSharp.Playwright.DefaultTimeout)]34 public async Task ShouldIntercept()35 {36 await Page.RouteAsync("**/*", route => Task.CompletedTask);37 var response = await Page.GotoAsync(Server.EmptyPage);38 Assert.True(response.Ok);39 }40}41{42 [PlaywrightTest("page-route.spec.ts", "should intercept")]43 [Fact(Timeout=PlaywrightSharp.Playwright.DefaultTimeout)]44 public async Task ShouldIntercept()45 {46 await Page.RouteAsync("**/*", route => Task.CompletedTask);47 var response = await Page.GotoAsync(Server
ShouldIntercept
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.Playwright.Tests;7using Microsoft.Playwright.Tests.Helpers;8using NUnit.Framework;9using NUnit.Framework.Interfaces;10{11 [Parallelizable(ParallelScope.Self)]12 {13 [PlaywrightTest("page-route.spec.ts", "should intercept")]14 [Test, Timeout(TestConstants.DefaultTestTimeout)]15 public async Task ShouldIntercept()16 {17 await Page.RouteAsync("**/empty.html", (route, _) => Task.CompletedTask);18 await Page.GotoAsync(TestConstants.EmptyPage);19 }20 }21}
ShouldIntercept
Using AI Code Generation
1Microsoft.Playwright.Tests.PageRouteTests.ShouldIntercept()2Microsoft.Playwright.Tests.PageRouteTests.ShouldNotIntercept()3Microsoft.Playwright.Tests.PageRouteTests.ShouldWorkWithPostData()4Microsoft.Playwright.Tests.PageRouteTests.ShouldWorkWithPostData()5Microsoft.Playwright.Tests.PageRouteTests.ShouldWorkWithPostData()6Microsoft.Playwright.Tests.PageRouteTests.ShouldWorkWithPostData()7Microsoft.Playwright.Tests.PageRouteTests.ShouldWorkWithPostData()8Microsoft.Playwright.Tests.PageRouteTests.ShouldWorkWithPostData()
ShouldIntercept
Using AI Code Generation
1public async Task ShouldIntercept()2{3 await Page.RouteAsync("**/*", (route, request) => {4 if (request.Url.Contains("css"))5 {6 route.AbortAsync();7 }8 {9 route.ContinueAsync();10 }11 });12 await Page.GotoAsync(Server.Prefix + "/one-style.html");13 Assert.AreEqual(1, Page.StyleTags.Count());14 await Page.GotoAsync(Server.Prefix + "/two-style.html");15 Assert.AreEqual(2, Page.StyleTags.Count());16 await Page.GotoAsync(Server.Prefix + "/three-style.html");17 Assert.AreEqual(3, Page.StyleTags.Count());18 await Page.GotoAsync(Server.Prefix + "/one-style.html");19 Assert.AreEqual(1, Page.StyleTags.Count());20}21public async Task ShouldIntercept()22{23 await Page.RouteAsync("**/*", (route, request) => {24 if (request.Url.Contains("css"))25 {26 route.AbortAsync();27 }28 {29 route.ContinueAsync();30 }31 });32 await Page.GotoAsync(Server.Prefix + "/one-style.html");33 Assert.AreEqual(1, Page.StyleTags.Count());34 await Page.GotoAsync(Server.Prefix + "/two-style.html");35 Assert.AreEqual(2, Page.StyleTags.Count());36 await Page.GotoAsync(Server.Prefix + "/three-style.html");37 Assert.AreEqual(3, Page.StyleTags.Count());38 await Page.GotoAsync(Server.Prefix + "/one-style.html");39 Assert.AreEqual(1, Page.StyleTags.Count());40}41public async Task ShouldIntercept()42{43 await Page.RouteAsync("**/*", (route, request) => {44 if (request.Url.Contains("css"))45 {46 route.AbortAsync();47 }48 {49 route.ContinueAsync();50 }51 });52 await Page.GotoAsync(Server.Prefix + "/one-style.html");53 Assert.AreEqual(1, Page.StyleTags.Count());54 await Page.GotoAsync(Server.Prefix + "/two-style.html");55 Assert.AreEqual(2, Page.StyleTags.Count());
ShouldIntercept
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.Playwright;7using Microsoft.Playwright.Tests;8using Xunit;9using Xunit.Abstractions;10{11 [Collection(TestConstants.TestFixtureBrowserCollectionName)]12 {13 public PageRouteTests(ITestOutputHelper output) : base(output)14 {15 }16 [PlaywrightTest("page-route.spec.ts", "should intercept")]17 [Fact(Timeout = TestConstants.DefaultTestTimeout)]18 public async Task ShouldIntercept()19 {20 await Page.RouteAsync("**/*", (route, _) => route.AbortAsync());21 var exception = await Assert.ThrowsAsync<PlaywrightSharpException>(() => Page.GotoAsync(TestConstants.EmptyPage));22 Assert.Contains("net::ERR_FAILED", exception.Message);23 }24 }25}26using System;27using System.Collections.Generic;28using System.Linq;29using System.Text;30using System.Threading.Tasks;31using Microsoft.Playwright;32using Microsoft.Playwright.Tests;33using Xunit;34using Xunit.Abstractions;35{36 [Collection(TestConstants.TestFixtureBrowserCollectionName)]37 {38 public PageRouteTests(ITestOutputHelper output) : base(output)39 {40 }41 [PlaywrightTest("page-route.spec.ts", "should intercept main resource during cross-process navigation")]42 [Fact(Timeout = TestConstants.DefaultTestTimeout)]43 public async Task ShouldInterceptMainResourceDuringCrossProcessNavigation()44 {45 await Page.RouteAsync(TestConstants.EmptyPage, (route, _) => route.AbortAsync());46 var exception = await Assert.ThrowsAsync<PlaywrightSharpException>(() => Page.GotoAsync(TestConstants.CrossProcessHttpPrefix + "/empty.html"));47 Assert.Contains("net::ERR_FAILED", exception.Message);48 }49 }50}51using System;52using System.Collections.Generic;53using System.Linq;
ShouldIntercept
Using AI Code Generation
1public async Task ShouldIntercept()2{3 await Page.RouteAsync("**/*", route => route.ContinueAsync());4 await Page.GotoAsync(Server.EmptyPage);5 await Page.RouteAsync("**/*", route => route.ContinueAsync());6 await Page.GotoAsync(Server.EmptyPage);7 await Page.RouteAsync("**/*", route => route.ContinueAsync());8 await Page.GotoAsync(Server.EmptyPage);9 await Page.RouteAsync("**/*", route => route.ContinueAsync());10 await Page.GotoAsync(Server.EmptyPage);11 await Page.RouteAsync("**/*", route => route.ContinueAsync());12 await Page.GotoAsync(Server.EmptyPage);13}14public async Task ShouldNotIntercept()15{16 await Page.RouteAsync("**/*", route => route.ContinueAsync());17 await Page.GotoAsync(Server.EmptyPage);18 await Page.RouteAsync("**/*", route => route.ContinueAsync());19 await Page.GotoAsync(Server.EmptyPage);20 await Page.RouteAsync("**/*", route => route.ContinueAsync());21 await Page.GotoAsync(Server.EmptyPage);22 await Page.RouteAsync("**/*", route => route.ContinueAsync());23 await Page.GotoAsync(Server.EmptyPage);24 await Page.RouteAsync("**/*", route => route.ContinueAsync());25 await Page.GotoAsync(Server.EmptyPage);26}27public async Task ShouldWork()28{29 await Page.RouteAsync("**/*", route => route.ContinueAsync());
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!