How to use ShouldIgnorePptrInternalScriptsIfReportAnonymousScriptsIsTrue method of PuppeteerSharp.Tests.CoverageTests.JSCoverageTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.CoverageTests.JSCoverageTests.ShouldIgnorePptrInternalScriptsIfReportAnonymousScriptsIsTrue

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

JSCoverageTests.cs

Source: JSCoverageTests.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 JSCoverageTests : PuppeteerPageBaseTest
16    {
17        public JSCoverageTests(ITestOutputHelper output) : base(output)
18        {
19        }
20
21        [PuppeteerTest("coverage.spec.ts", "JSCoverage", "should work")]
22        [SkipBrowserFact(skipFirefox: true)]
23        public async Task ShouldWork()
24        {
25            await Page.Coverage.StartJSCoverageAsync();
26            await Page.GoToAsync(TestConstants.ServerUrl + "/jscoverage/simple.html", WaitUntilNavigation.Networkidle0);
27            var coverage = await Page.Coverage.StopJSCoverageAsync();
28            Assert.Single(coverage);
29            Assert.Contains("/jscoverage/simple.html", coverage[0].Url);
30            Assert.Equal(new CoverageEntryRange[]
31            {
32                new CoverageEntryRange
33                {
34                    Start = 0,
35                    End = 17
36                },
37                new CoverageEntryRange
38                {
39                    Start = 35,
40                    End = 61
41                },
42            }, coverage[0].Ranges);
43        }
44
45        [PuppeteerTest("coverage.spec.ts", "JSCoverage", "should report sourceURLs")]
46        [SkipBrowserFact(skipFirefox: true)]
47        public async Task ShouldReportSourceUrls()
48        {
49            await Page.Coverage.StartJSCoverageAsync();
50            await Page.GoToAsync(TestConstants.ServerUrl + "/jscoverage/sourceurl.html");
51            var coverage = await Page.Coverage.StopJSCoverageAsync();
52            Assert.Single(coverage);
53            Assert.Equal("nicename.js", coverage[0].Url);
54        }
55
56        [PuppeteerTest("coverage.spec.ts", "JSCoverage", "should ignore eval() scripts by default")]
57        [SkipBrowserFact(skipFirefox: true)]
58        public async Task ShouldIgnoreEvalScriptsByDefault()
59        {
60            await Page.Coverage.StartJSCoverageAsync();
61            await Page.GoToAsync(TestConstants.ServerUrl + "/jscoverage/eval.html");
62            var coverage = await Page.Coverage.StopJSCoverageAsync();
63            Assert.Single(coverage);
64        }
65
66        [PuppeteerTest("coverage.spec.ts", "JSCoverage", "shouldn't ignore eval() scripts if reportAnonymousScripts is true")]
67        [SkipBrowserFact(skipFirefox: true)]
68        public async Task ShouldntIgnoreEvalScriptsIfReportAnonymousScriptsIsTrue()
69        {
70            await Page.Coverage.StartJSCoverageAsync(new CoverageStartOptions
71            {
72                ReportAnonymousScripts = true
73            });
74            await Page.GoToAsync(TestConstants.ServerUrl + "/jscoverage/eval.html");
75            var coverage = await Page.Coverage.StopJSCoverageAsync();
76            Assert.NotNull(coverage.FirstOrDefault(entry => entry.Url.StartsWith("debugger://", StringComparison.Ordinal)));
77            Assert.Equal(2, coverage.Count());
78        }
79
80        [PuppeteerTest("coverage.spec.ts", "JSCoverage", "should ignore pptr internal scripts if reportAnonymousScripts is true")]
81        [SkipBrowserFact(skipFirefox: true)]
82        public async Task ShouldIgnorePptrInternalScriptsIfReportAnonymousScriptsIsTrue()
83        {
84            await Page.Coverage.StartJSCoverageAsync(new CoverageStartOptions
85            {
86                ReportAnonymousScripts = true
87            });
88            await Page.GoToAsync(TestConstants.EmptyPage);
89            await Page.EvaluateExpressionAsync("console.log('foo')");
90            await Page.EvaluateFunctionAsync("() => console.log('bar')");
91            var coverage = await Page.Coverage.StopJSCoverageAsync();
92            Assert.Empty(coverage);
93        }
94
95        [PuppeteerTest("coverage.spec.ts", "JSCoverage", "should report multiple scripts")]
96        [SkipBrowserFact(skipFirefox: true)]
97        public async Task ShouldReportMultipleScripts()
98        {
99            await Page.Coverage.StartJSCoverageAsync();
100            await Page.GoToAsync(TestConstants.ServerUrl + "/jscoverage/multiple.html");
101            var coverage = await Page.Coverage.StopJSCoverageAsync();
102            Assert.Equal(2, coverage.Length);
103            var orderedList = coverage.OrderBy(c => c.Url);
104            Assert.Contains("/jscoverage/script1.js", orderedList.ElementAt(0).Url);
105            Assert.Contains("/jscoverage/script2.js", orderedList.ElementAt(1).Url);
106        }
107
108        [PuppeteerTest("coverage.spec.ts", "JSCoverage", "should report right ranges")]
109        [SkipBrowserFact(skipFirefox: true)]
110        public async Task ShouldReportRightRanges()
111        {
112            await Page.Coverage.StartJSCoverageAsync();
113            await Page.GoToAsync(TestConstants.ServerUrl + "/jscoverage/ranges.html");
114            var coverage = await Page.Coverage.StopJSCoverageAsync();
115            Assert.Single(coverage);
116            var entry = coverage[0];
117            Assert.Single(entry.Ranges);
118            var range = entry.Ranges[0];
119            Assert.Equal("console.log('used!');", entry.Text.Substring(range.Start, range.End - range.Start));
120        }
121
122        [PuppeteerTest("coverage.spec.ts", "JSCoverage", "should report scripts that have no coverage")]
123        [SkipBrowserFact(skipFirefox: true)]
124        public async Task ShouldReportScriptsThatHaveNoCoverage()
125        {
126            await Page.Coverage.StartJSCoverageAsync();
127            await Page.GoToAsync(TestConstants.ServerUrl + "/jscoverage/unused.html");
128            var coverage = await Page.Coverage.StopJSCoverageAsync();
129            Assert.Single(coverage);
130            var entry = coverage[0];
131            Assert.Contains("unused.html", entry.Url);
132            Assert.Empty(entry.Ranges);
133        }
134
135        [PuppeteerTest("coverage.spec.ts", "JSCoverage", "should work with conditionals")]
136        [SkipBrowserFact(skipFirefox: true)]
137        public async Task ShouldWorkWithConditionals()
138        {
139            const string involved = @"[
140              {
141                ""Url"": ""http://localhost:<PORT>/jscoverage/involved.html"",
142                ""Ranges"": [
143                  {
144                    ""Start"": 0,
145                    ""End"": 35
146                  },
147                  {
148                    ""Start"": 50,
149                    ""End"": 100
150                  },
151                  {
152                    ""Start"": 107,
153                    ""End"": 141
154                  },
155                  {
156                    ""Start"": 148,
157                    ""End"": 160
158                  },
159                  {
160                    ""Start"": 168,
161                    ""End"": 207
162                  }
163                ],
164                ""Text"": ""\nfunction foo() {\n  if (1 > 2)\n    console.log(1);\n  if (1 < 2)\n    console.log(2);\n  let x = 1 > 2 ? 'foo' : 'bar';\n  let y = 1 < 2 ? 'foo' : 'bar';\n  let z = () => {};\n  let q = () => {};\n  q();\n}\n\nfoo();\n""
165              }
166            ]";
167            await Page.Coverage.StartJSCoverageAsync();
168            await Page.GoToAsync(TestConstants.ServerUrl + "/jscoverage/involved.html");
169            var coverage = await Page.Coverage.StopJSCoverageAsync();
170            Assert.Equal(
171                TestUtils.CompressText(involved),
172                Regex.Replace(TestUtils.CompressText(JsonConvert.SerializeObject(coverage)), @"\d{4}\/", "<PORT>/"));
173        }
174
175        [PuppeteerTest("coverage.spec.ts", "JSCoverage", "should not hang when there is a debugger statement")]
176        [PuppeteerFact(Skip = "Skipped in puppeteer")]
177        public async Task ShouldNotHangWhenThereIsADebuggerStatement()
178        {
179            await Page.Coverage.StartJSCoverageAsync();
180            await Page.GoToAsync(TestConstants.EmptyPage);
181            await Page.EvaluateFunctionAsync(@"() => {
182                debugger;
183            }");
184            await Page.Coverage.StopJSCoverageAsync();
185        }
186    }
187}
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 ShouldIgnorePptrInternalScriptsIfReportAnonymousScriptsIsTrue code on LambdaTest Cloud Grid

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