How to use ShouldIgnoreInjectedStylesheets method of PuppeteerSharp.Tests.CoverageTests.CSSCoverageTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.CoverageTests.CSSCoverageTests.ShouldIgnoreInjectedStylesheets

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

CSSCoverageTests.cs

Source: CSSCoverageTests.cs Github

copy
1using System;
2using System.Linq;
3using System.Text.RegularExpressions;
4using System.Threading.Tasks;
5using Newtonsoft.Json;
6using PuppeteerSharp.PageCoverage;
7using PuppeteerSharp.Tests.Attributes;
8using PuppeteerSharp.Xunit;
9using Xunit;
10using Xunit.Abstractions;
11
12namespace PuppeteerSharp.Tests.CoverageTests
13{
14    [Collection(TestConstants.TestFixtureCollectionName)]
15    public class CSSCoverageTests : PuppeteerPageBaseTest
16    {
17        public CSSCoverageTests(ITestOutputHelper output) : base(output)
18        {
19        }
20
21        [PuppeteerTest("coverage.spec.ts", "CSSCoverage", "should work")]
22        [SkipBrowserFact(skipFirefox: true)]
23        public async Task ShouldWork()
24        {
25            await Page.Coverage.StartCSSCoverageAsync();
26            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/simple.html");
27            var coverage = await Page.Coverage.StopCSSCoverageAsync();
28            Assert.Single(coverage);
29            Assert.Contains("/csscoverage/simple.html", coverage[0].Url);
30            Assert.Equal(new CoverageEntryRange[]
31            {
32                new CoverageEntryRange
33                {
34                    Start = 1,
35                    End = 22
36                }
37            }, coverage[0].Ranges);
38            var range = coverage[0].Ranges[0];
39            Assert.Equal("div { color: green; }", coverage[0].Text.Substring(range.Start, range.End - range.Start));
40        }
41
42        [PuppeteerTest("coverage.spec.ts", "CSSCoverage", "should report sourceURLs")]
43        [SkipBrowserFact(skipFirefox: true)]
44        public async Task ShouldReportSourceUrls()
45        {
46            await Page.Coverage.StartCSSCoverageAsync();
47            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/sourceurl.html");
48            var coverage = await Page.Coverage.StopCSSCoverageAsync();
49            Assert.Single(coverage);
50            Assert.Equal("nicename.css", coverage[0].Url);
51        }
52
53        [PuppeteerTest("coverage.spec.ts", "CSSCoverage", "should report multiple stylesheets")]
54        [SkipBrowserFact(skipFirefox: true)]
55        public async Task ShouldReportMultipleStylesheets()
56        {
57            await Page.Coverage.StartCSSCoverageAsync();
58            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/multiple.html");
59            var coverage = await Page.Coverage.StopCSSCoverageAsync();
60            Assert.Equal(2, coverage.Length);
61            var orderedList = coverage.OrderBy(c => c.Url);
62            Assert.Contains("/csscoverage/stylesheet1.css", orderedList.ElementAt(0).Url);
63            Assert.Contains("/csscoverage/stylesheet2.css", orderedList.ElementAt(1).Url);
64        }
65
66        [PuppeteerTest("coverage.spec.ts", "CSSCoverage", "should report stylesheets that have no coverage")]
67        [SkipBrowserFact(skipFirefox: true)]
68        public async Task ShouldReportStylesheetsThatHaveNoCoverage()
69        {
70            await Page.Coverage.StartCSSCoverageAsync();
71            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/unused.html");
72            var coverage = await Page.Coverage.StopCSSCoverageAsync();
73            Assert.Single(coverage);
74            var entry = coverage[0];
75            Assert.Contains("unused.css", entry.Url);
76            Assert.Empty(entry.Ranges);
77        }
78
79        [PuppeteerTest("coverage.spec.ts", "CSSCoverage", "should work with media queries")]
80        [SkipBrowserFact(skipFirefox: true)]
81        public async Task ShouldWorkWithMediaQueries()
82        {
83            await Page.Coverage.StartCSSCoverageAsync();
84            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/media.html");
85            var coverage = await Page.Coverage.StopCSSCoverageAsync();
86            Assert.Single(coverage);
87            var entry = coverage[0];
88            Assert.Contains("/csscoverage/media.html", entry.Url);
89            Assert.Equal(new CoverageEntryRange[]
90            {
91                new CoverageEntryRange
92                {
93                    Start = 17,
94                    End = 38
95                }
96            }, coverage[0].Ranges);
97        }
98
99        [PuppeteerTest("coverage.spec.ts", "CSSCoverage", "should work with complicated usecases")]
100        [SkipBrowserFact(skipFirefox: true)]
101        public async Task ShouldWorkWithComplicatedUsecases()
102        {
103            const string involved = @"[
104              {
105                ""Url"": ""http://localhost:<PORT>/csscoverage/involved.html"",
106                ""Ranges"": [
107                  {
108                    ""Start"": 149,
109                    ""End"": 297
110                  },
111                  {
112                    ""Start"": 327,
113                    ""End"": 433
114                  }
115                ],
116                ""Text"": ""\n @charset \""utf - 8\"";\[email protected] svg url(http://www.w3.org/2000/svg);\[email protected] {\n  font-family: \""Example Font\"";\n src: url(\""./Dosis-Regular.ttf\"");\n}\n\n#fluffy {\n  border: 1px solid black;\n  z-index: 1;\n  /* -webkit-disabled-property: rgb(1, 2, 3) */\n  -lol-cats: \""dogs\"" /* non-existing property */\n}\n\[email protected] (min-width: 1px) {\n  span {\n    -webkit-border-radius: 10px;\n    font-family: \""Example Font\"";\n    animation: 1s identifier;\n  }\n}\n""
117              }
118            ]";
119            await Page.Coverage.StartCSSCoverageAsync();
120            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/involved.html");
121            var coverage = await Page.Coverage.StopCSSCoverageAsync();
122            Assert.Equal(
123                TestUtils.CompressText(involved),
124                Regex.Replace(TestUtils.CompressText(JsonConvert.SerializeObject(coverage)), @":\d{4}\/", ":<PORT>/"));
125        }
126
127        [PuppeteerTest("coverage.spec.ts", "CSSCoverage", "should ignore injected stylesheets")]
128        [SkipBrowserFact(skipFirefox: true)]
129        public async Task ShouldIgnoreInjectedStylesheets()
130        {
131            await Page.Coverage.StartCSSCoverageAsync();
132            await Page.AddStyleTagAsync(new AddTagOptions
133            {
134                Content = "body { margin: 10px;}"
135            });
136            // trigger style recalc
137            var margin = await Page.EvaluateExpressionAsync<string>("window.getComputedStyle(document.body).margin");
138            Assert.Equal("10px", margin);
139            var coverage = await Page.Coverage.StopCSSCoverageAsync();
140            Assert.Empty(coverage);
141        }
142
143        [PuppeteerTest("coverage.spec.ts", "CSSCoverage", "should work with a recently loaded stylesheet")]
144        [SkipBrowserFact(skipFirefox: true)]
145        public async Task ShouldWorkWithArRecentlyLoadedStylesheet()
146        {
147            await Page.Coverage.StartCSSCoverageAsync();
148            await Page.EvaluateFunctionAsync(@"async url => {
149                document.body.textContent = 'hello, world';
150
151                const link = document.createElement('link');
152                link.rel = 'stylesheet';
153                link.href = url;
154                document.head.appendChild(link);
155                await new Promise(x => link.onload = x);
156            }", TestConstants.ServerUrl + "/csscoverage/stylesheet1.css");
157            var coverage = await Page.Coverage.StopCSSCoverageAsync();
158            Assert.Single(coverage);
159        }
160    }
161}
162
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 ShouldIgnoreInjectedStylesheets code on LambdaTest Cloud Grid

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