Best Puppeteer-sharp code snippet using PuppeteerSharp.TracingOptions
Program.cs
Source:Program.cs
...90 var browser = await Puppeteer.LaunchAsync(options).ConfigureAwait(false);91 92 var page = await browser.NewPageAsync().ConfigureAwait(false);93 94 //await page.Tracing.StartAsync(new TracingOptions { Path = "" }).ConfigureAwait(false);95 //await page.Tracing.StopAsync().ConfigureAwait(false);96 //await page.CloseAsync().ConfigureAwait(false);97 var tryLimit = 30;98 var currentTry = 0;99 while (currentTry < tryLimit)100 {101 while (links.TryDequeue(out var nextUrl))102 {103 currentTry = 0;104 try105 {106 logger.LogInformation("{threadId} Getting url {nextUrl}", threadId, nextUrl);107 Response? pageNav = null;108 var tries = 0;...
Scrape.cs
Source:Scrape.cs
...49 {50 Width = width,51 Height = height52 });53 await page.Tracing.StartAsync(new TracingOptions54 {55 Categories = new List<string> { "devtools.timeline" }56 });57 var client = await page.Target.CreateCDPSessionAsync();58 await client.SendAsync("Network.clearBrowserCache");59 await page.GoToAsync(url, timeout: 0);60 string rawTrace = await page.Tracing.StopAsync();61 string content = await page.GetContentAsync();62 //Image63 var imageElements = await page.QuerySelectorAllAsync("img");64 if (imageElements != null)65 {66 imageCount = imageElements.Count();67 imageProperties = await new ImageHelper().GetImageData(imageElements);...
TracingTests.cs
Source:TracingTests.cs
...49 [PuppeteerTest("tracing.spec.ts", "Tracing", "should output a trace")]50 [SkipBrowserFact(skipFirefox: true)]51 public async Task ShouldOutputATrace()52 {53 await Page.Tracing.StartAsync(new TracingOptions54 {55 Screenshots = true,56 Path = _file57 });58 await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");59 await Page.Tracing.StopAsync();60 Assert.True(File.Exists(_file));61 }62 [PuppeteerTest("tracing.spec.ts", "Tracing", "should run with custom categories if provided")]63 [SkipBrowserFact(skipFirefox: true)]64 public async Task ShouldRunWithCustomCategoriesProvided()65 {66 await Page.Tracing.StartAsync(new TracingOptions67 {68 Screenshots = true,69 Path = _file,70 Categories = new List<string>71 {72 "disabled-by-default-v8.cpu_profiler.hires"73 }74 });75 await Page.Tracing.StopAsync();76 using (var file = File.OpenText(_file))77 using (var reader = new JsonTextReader(file))78 {79 var traceJson = JToken.ReadFrom(reader);80 Assert.Contains("disabled-by-default-v8.cpu_profiler.hires", traceJson["metadata"]["trace-config"].ToString());81 }82 }83 [PuppeteerTest("tracing.spec.ts", "Tracing", "should throw if tracing on two pages")]84 [SkipBrowserFact(skipFirefox: true)]85 public async Task ShouldThrowIfTracingOnTwoPages()86 {87 await Page.Tracing.StartAsync(new TracingOptions88 {89 Path = _file90 });91 var newPage = await Browser.NewPageAsync();92 await Assert.ThrowsAsync<InvalidOperationException>(async () =>93 {94 await Page.Tracing.StartAsync(new TracingOptions95 {96 Path = _file97 });98 });99 await newPage.CloseAsync();100 await Page.Tracing.StopAsync();101 }102 [PuppeteerTest("tracing.spec.ts", "Tracing", "should return a buffer")]103 [SkipBrowserFact(skipFirefox: true)]104 public async Task ShouldReturnABuffer()105 {106 await Page.Tracing.StartAsync(new TracingOptions107 {108 Screenshots = true,109 Path = _file110 });111 await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");112 var trace = await Page.Tracing.StopAsync();113 var buf = File.ReadAllText(_file);114 Assert.Equal(trace, buf);115 }116 [PuppeteerTest("tracing.spec.ts", "Tracing", "should work without options")]117 [SkipBrowserFact(skipFirefox: true)]118 public async Task ShouldWorkWithoutOptions()119 {120 await Page.Tracing.StartAsync();121 await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");122 var trace = await Page.Tracing.StopAsync();123 Assert.NotNull(trace);124 }125 [PuppeteerTest("tracing.spec.ts", "Tracing", "should support a buffer without a path")]126 [SkipBrowserFact(skipFirefox: true)]127 public async Task ShouldSupportABufferWithoutAPath()128 {129 await Page.Tracing.StartAsync(new TracingOptions130 {131 Screenshots = true132 });133 await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");134 var trace = await Page.Tracing.StopAsync();135 Assert.Contains("screenshot", trace);136 }137 }138}...
Tracing.cs
Source:Tracing.cs
...5using PuppeteerSharp.Messaging;6namespace PuppeteerSharp7{8 /// <summary>9 /// You can use <see cref="Tracing.StartAsync(TracingOptions)"/> and <see cref="Tracing.StopAsync"/> to create a trace file which can be opened in Chrome DevTools or timeline viewer.10 /// </summary>11 /// <example>12 /// <code>13 /// await Page.Tracing.StartAsync(new TracingOptions14 /// {15 /// Screenshots = true,16 /// Path = _file17 /// });18 /// await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");19 /// await Page.Tracing.StopAsync();20 /// </code>21 /// </example>22 public class Tracing23 {24 private readonly CDPSession _client;25 private bool _recording;26 private string _path;27 private static readonly List<string> _defaultCategories = new List<string>()28 {29 "-*",30 "devtools.timeline",31 "v8.execute",32 "disabled-by-default-devtools.timeline",33 "disabled-by-default-devtools.timeline.frame",34 "toplevel",35 "blink.console",36 "blink.user_timing",37 "latencyInfo",38 "disabled-by-default-devtools.timeline.stack",39 "disabled-by-default-v8.cpu_profiler"40 };41 internal Tracing(CDPSession client)42 {43 _client = client;44 }45 /// <summary>46 /// Starts tracing.47 /// </summary>48 /// <returns>Start task</returns>49 /// <param name="options">Tracing options</param>50 public async Task StartAsync(TracingOptions options)51 {52 if (_recording)53 {54 throw new InvalidOperationException("Cannot start recording trace while already recording trace.");55 }5657 if (string.IsNullOrEmpty(options.Path))58 {59 throw new ArgumentException("Must specify a path to write trace file to.");60 }616263 var categories = options.Categories ?? _defaultCategories;64
...
TracingOptions.cs
Source:TracingOptions.cs
1using System.Collections.Generic;2namespace PuppeteerSharp3{4 /// <summary>5 /// Tracing options used on <see cref="Tracing.StartAsync(TracingOptions)"/>.6 /// </summary>7 public class TracingOptions8 {9 /// <summary>10 /// Gets or sets a value indicating whether Tracing should captures screenshots in the trace11 /// </summary>12 /// <value>Screenshots option</value>13 public bool Screenshots { get; set; }14 /// <summary>15 /// A path to write the trace file to16 /// </summary>17 /// <value>The path.</value>18 public string Path { get; set; }19 /// <summary>20 /// Specify custom categories to use instead of default.21 /// </summary>...
TracingOptions
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browser = await Puppeteer.LaunchAsync(new LaunchOptions9 {10 });11 var page = await browser.NewPageAsync();12 await page.Tracing.StartAsync(new TracingOptions13 {14 Categories = new[] { "devtools.timeline" }15 });16 await page.Tracing.StopAsync();17 await browser.CloseAsync();18 }19 }20}21using System;22using System.Threading.Tasks;23using PuppeteerSharp;24{25 {26 static async Task Main(string[] args)27 {28 var browser = await Puppeteer.LaunchAsync(new LaunchOptions29 {30 });31 var page = await browser.NewPageAsync();32 await page.Tracing.StartAsync(new TracingOptions33 {34 Categories = new[] { "devtools.timeline" },35 });36 await page.Tracing.StopAsync();37 await browser.CloseAsync();38 }39 }40}
TracingOptions
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions10 {11 }))12 {13 var page = await browser.NewPageAsync();14 await page.SetRequestInterceptionAsync(true);15 page.Request += async (sender, e) =>16 {17 if (e.Request.ResourceType == ResourceType.Image)18 {19 await e.Request.AbortAsync();20 }21 {22 await e.Request.ContinueAsync();23 }24 };25 await page.ScreenshotAsync("google.png");26 }27 }28 }29}30using PuppeteerSharp;31using System;32using System.Threading.Tasks;33{34 {35 static async Task Main(string[] args)36 {37 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);38 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions39 {40 }))41 {42 var page = await browser.NewPageAsync();43 await page.SetRequestInterceptionAsync(true);44 page.RequestFailed += async (sender, e) =>45 {46 if (e.Request.ResourceType == ResourceType.Image)47 {48 if (e.Request.Url.Contains(".jpg"))49 {50 await e.Request.AbortAsync();51 }52 }53 };54 await page.ScreenshotAsync("google.png");55 }56 }
TracingOptions
Using AI Code Generation
1{2 Args = new string[] { "--no-sandbox" }3};4var browser = await Puppeteer.LaunchAsync(options);5var page = await browser.NewPageAsync();6var client = page.GetClient();7await client.SendAsync("Tracing.start", new { categories = new[] { "-*", "devtools.timeline" } });8{9 Args = new string[] { "--no-sandbox" }10};11var browser = await Puppeteer.LaunchAsync(options);12var page = await browser.NewPageAsync();13await page.Tracing.StartAsync(new TracingOptions { Categories = new[] { "-*", "devtools.timeline" } });14using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true, Args = new string[] { "--no-sandbox" } }))15using (var page = await browser.NewPageAsync())16{17 await page.Tracing.StartAsync(new TracingOptions { Categories = new[] { "-*", "devtools.timeline" } });18}19var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true, Args = new string[] { "--no-sandbox" } });20var page = await browser.NewPageAsync();21await page.Tracing.StartAsync(new TracingOptions { Categories = new[] { "-*", "devtools.timeline" } });22var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true, Args = new string[] { "--no-sandbox" } });23var page = await browser.NewPageAsync();24await page.Tracing.StartAsync(new TracingOptions { Categories = new[] { "-*", "devtools.timeline" } });
TracingOptions
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 var options = new LaunchOptions();9 options.Headless = false;10 options.Args = new string[] { "--disable-extensions" };11 options.Devtools = true;12 options.TracingCategories = new string[] { "devtools.timeline" };13 options.TracingRecordMode = RecordMode.RecordUntilFull;14 options.TracingBufferSize = 10000000;15 options.TracingScreenshot = true;16 options.TracingPath = "trace.json";17 var browser = await Puppeteer.LaunchAsync(options);18 var page = await browser.NewPageAsync();19 await page.ScreenshotAsync("screenshot.png");20 await browser.CloseAsync();21 }22 }23}24using PuppeteerSharp;25using System;26using System.Threading.Tasks;27{28 {29 static async Task Main(string[] args)30 {31 var options = new LaunchOptions();32 options.Headless = false;33 options.Args = new string[] { "--disable-extensions" };34 options.Devtools = true;35 options.TracingCategories = new string[] { "devtools.timeline" };36 options.TracingRecordMode = RecordMode.RecordUntilFull;37 options.TracingBufferSize = 10000000;38 options.TracingScreenshot = true;39 options.TracingPath = "trace.json";40 var browser = await Puppeteer.LaunchAsync(options);41 var page = await browser.NewPageAsync();42 await page.ScreenshotAsync("screenshot.png");43 await browser.CloseAsync();44 }45 }46}
TracingOptions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using PuppeteerSharp;7using PuppeteerSharp.Messaging;8using PuppeteerSharp.Helpers;9using PuppeteerSharp.Input;10using PuppeteerSharp.Media;11using PuppeteerSharp.Contrib.Extensions;12using PuppeteerSharp.Contrib.PageObjects;13using PuppeteerSharp.Contrib.Poco;14using PuppeteerSharp.Contrib.RequestInterception;15{16 {17 static async Task Main(string[] args)18 {19 {20 Categories = new[] { "devtools.timeline" },21 };22 var browser = await Puppeteer.LaunchAsync(new LaunchOptions23 {24 Args = new[] { "--start-maximized" },25 });26 var page = await browser.NewPageAsync();27 await page.Tracing.StartAsync(options);28 await page.WaitForTimeoutAsync(5000);29 await page.Tracing.StopAsync();30 await browser.CloseAsync();31 }32 }33}34using System;35using System.Collections.Generic;36using System.Linq;37using System.Text;38using System.Threading.Tasks;39using PuppeteerSharp;40using PuppeteerSharp.Messaging;41using PuppeteerSharp.Helpers;42using PuppeteerSharp.Input;43using PuppeteerSharp.Media;44using PuppeteerSharp.Contrib.Extensions;45using PuppeteerSharp.Contrib.PageObjects;46using PuppeteerSharp.Contrib.Poco;47using PuppeteerSharp.Contrib.RequestInterception;48{49 {50 static async Task Main(string[] args)51 {52 var browser = await Puppeteer.LaunchAsync(new LaunchOptions53 {54 Args = new[] { "--start-maximized" },55 });56 var page = await browser.NewPageAsync();57 await page.Tracing.StartAsync("trace.json");58 await page.WaitForTimeoutAsync(5000);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!