How to use GlobTests class of Microsoft.Playwright.Tests package

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.GlobTests

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

InterceptionTests.cs

Source: InterceptionTests.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.Net;
28using System.Threading.Tasks;
29using Microsoft.Playwright.Helpers;
30using NUnit.Framework;
31
32namespace Microsoft.Playwright.Tests
33{
34    public class GlobTests : PageTestEx
35    {
36        [PlaywrightTest("interception.spec.ts", "should work with glob")]
37        public void ShouldWorkWithGlob()
38        {
39            Assert.That("https://localhost:8080/foo.js", Does.Match(StringExtensions.GlobToRegex("**/*.js")));
40            Assert.That("https://localhost:8080/foo.js", Does.Match(StringExtensions.GlobToRegex("https://**/*.js")));
41            Assert.That("http://localhost:8080/simple/path.js", Does.Match(StringExtensions.GlobToRegex("http://localhost:8080/simple/path.js")));
42            Assert.That("http://localhost:8080/Simple/path.js", Does.Match(StringExtensions.GlobToRegex("http://localhost:8080/?imple/path.js")));
43            Assert.That("https://localhost:8080/a.js", Does.Match(StringExtensions.GlobToRegex("**/{a,b}.js")));
44            Assert.That("https://localhost:8080/b.js", Does.Match(StringExtensions.GlobToRegex("**/{a,b}.js")));
45            Assert.That("https://localhost:8080/c.jpg", Does.Match(StringExtensions.GlobToRegex("**/*.{png,jpg,jpeg}")));
46            Assert.That("https://localhost:8080/c.jpeg", Does.Match(StringExtensions.GlobToRegex("**/*.{png,jpg,jpeg}")));
47            Assert.That("https://localhost:8080/c.png", Does.Match(StringExtensions.GlobToRegex("**/*.{png,jpg,jpeg}")));
48            Assert.That("https://localhost:8080/c.css", Does.Not.Match(StringExtensions.GlobToRegex("**/*.{png,jpg,jpeg}")));
49            Assert.That("https://localhost:8080/foo.js", Does.Not.Match(StringExtensions.GlobToRegex("**/*.css")));
50            Assert.That("https://localhost:8080/foo.js", Does.Not.Match(StringExtensions.GlobToRegex("*.js")));
51            Assert.That("https://localhost:8080/c.js", Does.Not.Match(StringExtensions.GlobToRegex("**/{a,b}.js")));
52            Assert.That("foo.js", Does.Match(StringExtensions.GlobToRegex("foo*")));
53            Assert.That("foo/bar.js", Does.Not.Match(StringExtensions.GlobToRegex("foo*")));
54            Assert.That("http://localhost:3000/signin-oidc/foo", Does.Not.Match(StringExtensions.GlobToRegex("http://localhost:3000/signin-oidc*")));
55            Assert.That("http://localhost:3000/signin-oidcnice", Does.Match(StringExtensions.GlobToRegex("http://localhost:3000/signin-oidc*")));
56        }
57
58        [PlaywrightTest("interception.spec.ts", "should work with ignoreHTTPSErrors")]
59        public async Task ShouldWorkWithIgnoreHTTPSErrors()
60        {
61            var context = await Browser.NewContextAsync(new()
62            {
63                IgnoreHTTPSErrors = true
64            });
65
66            var page = await context.NewPageAsync();
67
68            await page.RouteAsync("**/*", (route) => route.ContinueAsync());
69            var response = await page.GotoAsync(HttpsServer.EmptyPage);
70            Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
71            await context.CloseAsync();
72        }
73
74
75        [PlaywrightTest("interception.spec.ts", "should work with navigation")]
76        public async Task ShouldWorkWithNavigation()
77        {
78            var requests = new Dictionary<string, IRequest>();
79            await Page.RouteAsync("**/*", (route) =>
80            {
81                requests.Add(route.Request.Url.Split('/').Last(), route.Request);
82                route.ContinueAsync();
83            });
84
85            Server.SetRedirect("/rrredirect", "/frames/one-frame.html");
86            await Page.GotoAsync(Server.Prefix + "/rrredirect");
87            Assert.True(requests["rrredirect"].IsNavigationRequest);
88            Assert.True(requests["frame.html"].IsNavigationRequest);
89            Assert.False(requests["script.js"].IsNavigationRequest);
90            Assert.False(requests["style.css"].IsNavigationRequest);
91        }
92
93        [PlaywrightTest("interception.spec.ts", "should intercept after a service worker")]
94        public async Task ShouldInterceptAfterAServiceWorker()
95        {
96            await Page.GotoAsync(Server.Prefix + "/serviceworkers/fetchdummy/sw.html");
97            await Page.EvaluateAsync("() => window.activationPromise");
98
99            string swResponse = await Page.EvaluateAsync<string>("() => fetchDummy('foo')");
100            Assert.AreEqual("responseFromServiceWorker:foo", swResponse);
101
102            await Page.RouteAsync("**/foo", (route) =>
103            {
104                int slash = route.Request.Url.LastIndexOf("/");
105                string name = route.Request.Url.Substring(slash + 1);
106
107                route.FulfillAsync(new() { Status = (int)HttpStatusCode.OK, Body = "responseFromInterception:" + name, ContentType = "text/css" });
108            });
109
110            string swResponse2 = await Page.EvaluateAsync<string>("() => fetchDummy('foo')");
111            Assert.AreEqual("responseFromServiceWorker:foo", swResponse2);
112
113            string nonInterceptedResponse = await Page.EvaluateAsync<string>("() => fetchDummy('passthrough')");
114            Assert.AreEqual("FAILURE: Not Found", nonInterceptedResponse);
115        }
116    }
117}
118
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

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)