How to use Coverage class of PuppeteerSharp.PageCoverage package

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

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

CoverageEntryPoint.cs

Source: CoverageEntryPoint.cs Github

copy
1using System;
2using PuppeteerSharp.Messaging;
3
4namespace PuppeteerSharp.PageCoverage
5{
6    internal class CoverageEntryPoint : IComparable<CoverageEntryPoint>
7    {
8        public int Offset { get; internal set; }
9        public int Type { get; internal set; }
10        public CoverageResponseRange Range { get; internal set; }
11
12        public int CompareTo(CoverageEntryPoint other)
13        {
14            // Sort with increasing offsets.
15            if (Offset != other.Offset)
16            {
17                return Offset - other.Offset;
18            }
19
20            // All "end" points should go before "start" points.
21            if (Type != other.Type)
22            {
23                return Type - other.Type;
24            }
25
26            var aLength = Range.EndOffset - Range.StartOffset;
27            var bLength = other.Range.EndOffset - other.Range.StartOffset;
28            // For two "start" points, the one with longer range goes first.
29            if (Type == 0)
30            {
31                return bLength - aLength;
32            }
33            // For two "end" points, the one with shorter range goes first.
34            return aLength - bLength;
35        }
36    }
37}
Full Screen

JSResetOnNavigationTests.cs

Source: JSResetOnNavigationTests.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.JSCoverageTests
10{
11    [Collection("PuppeteerLoaderFixture collection")]
12    public class JSResetOnNavigationTests : PuppeteerPageBaseTest
13    {
14        public JSResetOnNavigationTests(ITestOutputHelper output) : base(output)
15        {
16        }
17
18        [Fact]
19        public async Task ShouldReportScriptsAcrossNavigationsWhenDisabled()
20        {
21            await Page.Coverage.StartJSCoverageAsync(new CoverageStartOptions
22            {
23                ResetOnNavigation = false
24            });
25            await Page.GoToAsync(TestConstants.ServerUrl + "/jscoverage/multiple.html");
26            await Page.GoToAsync(TestConstants.EmptyPage);
27            var coverage = await Page.Coverage.StopJSCoverageAsync();
28            Assert.Equal(2, coverage.Length);
29        }
30
31        [Fact]
32        public async Task ShouldNotReportScriptsAcrossNavigationsWhenEnabled()
33        {
34            await Page.Coverage.StartJSCoverageAsync();
35            await Page.GoToAsync(TestConstants.ServerUrl + "/jscoverage/multiple.html");
36            await Page.GoToAsync(TestConstants.EmptyPage);
37            var coverage = await Page.Coverage.StopJSCoverageAsync();
38            Assert.Empty(coverage);
39        }
40    }
41}
42
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)