How to use StopCSSCoverageAsync method of PuppeteerSharp.PageCoverage.Coverage class

Best Puppeteer-sharp code snippet using PuppeteerSharp.PageCoverage.Coverage.StopCSSCoverageAsync

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

CSSResetOnNavigationTests.cs

Source: CSSResetOnNavigationTests.cs Github

copy
1using System;
2using System.Linq;
3using System.Threading.Tasks;
4using Newtonsoft.Json;
5using PuppeteerSharp.PageCoverage;
6using Xunit;
7using Xunit.Abstractions;
8
9namespace PuppeteerSharp.Tests.CSSCoverageTests
10{
11    [Collection("PuppeteerLoaderFixture collection")]
12    public class CSSResetOnNavigationTests : PuppeteerPageBaseTest
13    {
14        public CSSResetOnNavigationTests(ITestOutputHelper output) : base(output)
15        {
16        }
17
18        [Fact]
19        public async Task ShouldReportStylesheetsAcrossNavigationsWhenDisabled()
20        {
21            await Page.Coverage.StartCSSCoverageAsync(new CoverageStartOptions
22            {
23                ResetOnNavigation = false
24            });
25            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/multiple.html");
26            await Page.GoToAsync(TestConstants.EmptyPage);
27            var coverage = await Page.Coverage.StopCSSCoverageAsync();
28            Assert.Equal(2, coverage.Length);
29        }
30
31        [Fact]
32        public async Task ShouldNotReportScriptsAcrossNavigationsWhenEnabled()
33        {
34            await Page.Coverage.StartCSSCoverageAsync();
35            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/multiple.html");
36            await Page.GoToAsync(TestConstants.EmptyPage);
37            var coverage = await Page.Coverage.StopCSSCoverageAsync();
38            Assert.Empty(coverage);
39        }
40    }
41}
42
Full Screen

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 Xunit;
8using Xunit.Abstractions;
9
10namespace PuppeteerSharp.Tests.CSSCoverageTests
11{
12    [Collection("PuppeteerLoaderFixture collection")]
13    public class CSSCoverageTests : PuppeteerPageBaseTest
14    {
15        public CSSCoverageTests(ITestOutputHelper output) : base(output)
16        {
17        }
18
19        [Fact]
20        public async Task ShouldWork()
21        {
22            await Page.Coverage.StartCSSCoverageAsync();
23            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/simple.html");
24            var coverage = await Page.Coverage.StopCSSCoverageAsync();
25            Assert.Single(coverage);
26            Assert.Contains("/csscoverage/simple.html", coverage[0].Url);
27            Assert.Equal(new CoverageEntryRange[]
28            {
29                new CoverageEntryRange
30                {
31                    Start = 1,
32                    End = 22
33                }
34            }, coverage[0].Ranges);
35            var range = coverage[0].Ranges[0];
36            Assert.Equal("div { color: green; }", coverage[0].Text.Substring(range.Start, range.End - range.Start));
37        }
38
39        [Fact]
40        public async Task ShouldReportSourceUrls()
41        {
42            await Page.Coverage.StartCSSCoverageAsync();
43            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/sourceurl.html");
44            var coverage = await Page.Coverage.StopCSSCoverageAsync();
45            Assert.Single(coverage);
46            Assert.Equal("nicename.css", coverage[0].Url);
47        }
48
49        [Fact]
50        public async Task ShouldReportMultipleStylesheets()
51        {
52            await Page.Coverage.StartCSSCoverageAsync();
53            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/multiple.html");
54            var coverage = await Page.Coverage.StopCSSCoverageAsync();
55            Assert.Equal(2, coverage.Length);
56            var orderedList = coverage.OrderBy(c => c.Url);
57            Assert.Contains("/csscoverage/stylesheet1.css", orderedList.ElementAt(0).Url);
58            Assert.Contains("/csscoverage/stylesheet2.css", orderedList.ElementAt(1).Url);
59        }
60
61        [Fact]
62        public async Task ShouldReportStylesheetsThatHaveNoCoverage()
63        {
64            await Page.Coverage.StartCSSCoverageAsync();
65            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/unused.html");
66            var coverage = await Page.Coverage.StopCSSCoverageAsync();
67            Assert.Single(coverage);
68            var entry = coverage[0];
69            Assert.Contains("unused.css", entry.Url);
70            Assert.Empty(entry.Ranges);
71        }
72
73        [Fact]
74        public async Task ShouldWorkWithMediaQueries()
75        {
76            await Page.Coverage.StartCSSCoverageAsync();
77            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/media.html");
78            var coverage = await Page.Coverage.StopCSSCoverageAsync();
79            Assert.Single(coverage);
80            var entry = coverage[0];
81            Assert.Contains("/csscoverage/media.html", entry.Url);
82            Assert.Equal(new CoverageEntryRange[]
83            {
84                new CoverageEntryRange
85                {
86                    Start = 17,
87                    End = 38
88                }
89            }, coverage[0].Ranges);
90        }
91
92        [Fact]
93        public async Task ShouldWorkWithComplicatedUsecases()
94        {
95            const string involved = @"[
96              {
97                ""Url"": ""http://localhost:<PORT>/csscoverage/involved.html"",
98                ""Ranges"": [
99                  {
100                    ""Start"": 149,
101                    ""End"": 297
102                  },
103                  {
104                    ""Start"": 327,
105                    ""End"": 433
106                  }
107                ],
108                ""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""
109              }
110            ]";
111            await Page.Coverage.StartCSSCoverageAsync();
112            await Page.GoToAsync(TestConstants.ServerUrl + "/csscoverage/involved.html");
113            var coverage = await Page.Coverage.StopCSSCoverageAsync();
114            Assert.Equal(
115                TestUtils.CompressText(involved),
116                Regex.Replace(TestUtils.CompressText(JsonConvert.SerializeObject(coverage)), @":\d{4}\/", ":<PORT>/"));
117        }
118
119        [Fact]
120        public async Task ShouldIgnoreInjectedStylesheets()
121        {
122            await Page.Coverage.StartCSSCoverageAsync();
123            await Page.AddStyleTagAsync(new AddTagOptions
124            {
125                Content = "body { margin: 10px;}"
126            });
127            // trigger style recalc
128            var margin = await Page.EvaluateExpressionAsync<string>("window.getComputedStyle(document.body).margin");
129            Assert.Equal("10px", margin);
130            var coverage = await Page.Coverage.StopCSSCoverageAsync();
131            Assert.Empty(coverage);
132        }
133    }
134}
135
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 StopCSSCoverageAsync code on LambdaTest Cloud Grid

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