How to use Dispose method of PuppeteerSharp.BrowserFetcher class

Best Puppeteer-sharp code snippet using PuppeteerSharp.BrowserFetcher.Dispose

SaveWebPage.xaml.cs

Source:SaveWebPage.xaml.cs Github

copy

Full Screen

...75 }7677 //在最后记得关闭浏览器及释放资源78 browser.Disconnect();79 browser.Dispose();80 }81 catch(Exception ex)82 {83 EMessageBox.Show(ex.Message);84 }85 }8687 private async void btn_SaveAsPDF_Click(object sender, RoutedEventArgs e)88 {89 //打开网页的操作跟上面是一样的90 try91 {92 await new PuppeteerSharp.BrowserFetcher().DownloadAsync(PuppeteerSharp.BrowserFetcher.DefaultRevision);93 var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new PuppeteerSharp.LaunchOptions94 {95 Headless = true96 });97 var page = await browser.NewPageAsync(); //打开一个新标签98 await page.GoToAsync(this.tbox_Url.Text); //访问页面99100 //设置PDF选项101 PuppeteerSharp.PdfOptions pdfOptions = new PuppeteerSharp.PdfOptions();102 pdfOptions.DisplayHeaderFooter = false; //是否显示页眉页脚103 pdfOptions.FooterTemplate = ""; //页脚文本104105 var width = await page.EvaluateFunctionAsync<int>("function getWidth(){return document.body.scrollWidth}");106 var height = await page.EvaluateFunctionAsync<int>("function getHeight(){return document.body.scrollHeight}");107108 pdfOptions.Width = $"{width}px";109 pdfOptions.Height = $"{height}px";110111 pdfOptions.HeaderTemplate = ""; //页眉文本112 pdfOptions.Landscape = false; //纸张方向 false-垂直 true-水平113 pdfOptions.MarginOptions = new PuppeteerSharp.Media.MarginOptions() { Bottom = "0px", Left = "0px", Right = "0px", Top = "0px" }; //纸张边距,需要设置带单位的值,默认值是None114 pdfOptions.Scale = 1m; //PDF缩放,从0-1115 pdfOptions.PrintBackground = true;116117 var fileName = Environment.CurrentDirectory + $"\\download\\{await page.GetTitleAsync()}.pdf";118119 if (System.IO.File.Exists(fileName))120 {121 fileName = fileName.Replace(".pdf", $"{DateTime.Now.ToString("ffff")}.pdf");122 }123124 //保存PDF125 await page.PdfAsync(fileName, pdfOptions);126 EMessageBox.Show($"{fileName}保存成功");127128 //在最后记得关闭浏览器及释放资源129 browser.Disconnect();130 browser.Dispose();131 }132 catch(Exception ex)133 {134 EMessageBox.Show(ex.Message);135 }136 }137 }138} ...

Full Screen

Full Screen

Form1.cs

Source:Form1.cs Github

copy

Full Screen

...79 //dispose browser80 if (browser != null)81 {82 await browser.CloseAsync();83 browser.Dispose();84 }85 }86 }87}...

Full Screen

Full Screen

Program.cs

Source:Program.cs Github

copy

Full Screen

...133 AnsiConsole.WriteException(e);134}135finally136{137 page?.Dispose();138 browser?.Dispose();139 browserFetcher?.Dispose();140}141// dark mode colors142// ff538d4e C143// ff3a3a3c A144// ffb59f3b P145// ffb59f3b P146// ff3a3a3c A147// non-dark mode colors148// ff6aaa64 C149// ff787c7e A150// ffc9b458 P151// ffc9b458 P152// ff787c7e A...

Full Screen

Full Screen

PuppeteerBrowser.cs

Source:PuppeteerBrowser.cs Github

copy

Full Screen

...70 {71 logger.Info($"{this.Name}: Recreating PuppeteerBrowser");72 lock (browserLock)73 {74 this.page.Dispose();75 this.browser.CloseAsync().Wait();76 }77 System.Threading.Thread.Sleep(500);78 Init();79 }80 #region IDisposable Support81 private bool disposedValue = false; // To detect redundant calls82 protected virtual void Dispose(bool disposing)83 {84 logger.Info($"{this.Name}: Disposing PuppeteerBrowser Instance");85 if (!disposedValue)86 {87 if (disposing)88 {89 // TODO: dispose managed state (managed objects).90 }91 logger.Debug($"{this.Name}: Closing PuppeteerBrowser");92 this.browser.CloseAsync().Wait();93 logger.Debug($"{this.Name}: Closed PuppeteerBrowser");94 // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.95 // TODO: set large fields to null.96 disposedValue = true;97 }98 }99 // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.100 ~PuppeteerBrowser()101 {102 // Do not change this code. Put cleanup code in Dispose(bool disposing) above.103 Dispose(false);104 }105 // This code added to correctly implement the disposable pattern.106 public void Dispose()107 {108 // Do not change this code. Put cleanup code in Dispose(bool disposing) above.109 Dispose(true);110 // TODO: uncomment the following line if the finalizer is overridden above.111 GC.SuppressFinalize(this);112 }113 #endregion114 }115}...

Full Screen

Full Screen

PuppeteerBannerGenerator.cs

Source:PuppeteerBannerGenerator.cs Github

copy

Full Screen

...48 using var ms = new MemoryStream();49 await screenshotStream.CopyToAsync(ms, cancellationToken);50 return ms.ToArray().AsMemory();51 }52 public async ValueTask DisposeAsync() {53 if(_browser != null) {54 await _browser.DisposeAsync();55 }56 }57 }58}...

Full Screen

Full Screen

BrowserTests.cs

Source:BrowserTests.cs Github

copy

Full Screen

...24 {25 Headless = true26 });27 }28 public async Task DisposeAsync()29 {30 await Browser?.CloseAsync();31 }32 private async Task PrintPerf(Page page)33 {34 var perf = await page.EvaluateExpressionAsync<long>("window.performance.timing.domContentLoadedEventEnd - window.performance.timing.navigationStart");35 Console.WriteLine($"Load Time: {perf}ms");36 }37 [Fact]38 public async Task CheckRoot()39 {40 bool hasError = false;41 var page = await Browser.NewPageAsync();42 page.Console += Page_Console;...

Full Screen

Full Screen

BrowserService.cs

Source:BrowserService.cs Github

copy

Full Screen

...25 Console.WriteLine("ERROR: reCAPTCHA");26 }27 public async Task<string> GetHtml()28 => await _currentPage.GetContentAsync();29 public void Dispose() 30 => _browser?.Dispose();31 }32}...

Full Screen

Full Screen

PuppeteerDocumentGenerator.cs

Source:PuppeteerDocumentGenerator.cs Github

copy

Full Screen

...26 Top = "1.5cm",27 Bottom = "1.5cm"28 },29 });30 browser.Dispose();31 return pdfData;32 }33 }34 }35}...

Full Screen

Full Screen

Dispose

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5{6 {7 static async Task Main(string[] args)8 {9 var browserFetcher = new BrowserFetcher();10 var revisionInfo = browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);11 string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", "chrome-win", "chrome.exe");12 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { ExecutablePath = path });13 var page = await browser.NewPageAsync();14 await page.ScreenshotAsync("google.png");15 await browser.CloseAsync();16 }17 }18}

Full Screen

Full Screen

Dispose

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browserFetcher = new BrowserFetcher();9 var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);10 Console.WriteLine(revisionInfo.ExecutablePath);11 await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);12 await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);13 browserFetcher.Dispose();14 }15 }16}17public async Task<RevisionInfo> DownloadAsync(int revision, Action<int> progressCallback = null)18using System;19using System.Threading.Tasks;20using PuppeteerSharp;21{22 {23 static async Task Main(string[] args)24 {25 var browserFetcher = new BrowserFetcher();26 await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision, progress =>27 {28 Console.WriteLine(progress);29 });30 }31 }32}

Full Screen

Full Screen

Dispose

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 MainAsync().Wait();9 }10 static async Task MainAsync()11 {12 var browserFetcher = new BrowserFetcher();13 var revisionInfo = browserFetcher.DownloadAsync("533271");14 Console.WriteLine("Revision Info: " + revisionInfo.Result.Revision);15 browserFetcher.Dispose();16 }

Full Screen

Full Screen

Dispose

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browserFetcher = new BrowserFetcher();9 var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);10 var browser = await Puppeteer.LaunchAsync(new LaunchOptions11 {12 });13 var page = await browser.NewPageAsync();14 await page.ScreenshotAsync("google.png");15 await page.PdfAsync("google.pdf");16 await browser.CloseAsync();17 }18 }19}

Full Screen

Full Screen

Dispose

Using AI Code Generation

copy

Full Screen

1using System;2using PuppeteerSharp;3{4 {5 static void Main(string[] args)6 {7 var browserFetcher = new BrowserFetcher();8 browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision).GetAwaiter().GetResult();9 browserFetcher.Dispose();10 }11 }12}13using System;14using PuppeteerSharp;15{16 {17 static void Main(string[] args)18 {19 var browserFetcher = new BrowserFetcher();20 browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision).GetAwaiter().GetResult();21 browserFetcher.Dispose();22 }23 }24}25using System;26using PuppeteerSharp;27{28 {29 static void Main(string[] args)30 {31 var browserFetcher = new BrowserFetcher();32 browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision).GetAwaiter().GetResult();33 browserFetcher.Dispose();34 }35 }36}37using System;38using PuppeteerSharp;39{40 {41 static void Main(string[] args)42 {43 var browserFetcher = new BrowserFetcher();44 browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision).GetAwaiter().GetResult();45 browserFetcher.Dispose();46 }47 }48}49using System;50using PuppeteerSharp;51{52 {53 static void Main(string[] args)54 {55 var browserFetcher = new BrowserFetcher();56 browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision).GetAwaiter().GetResult();57 browserFetcher.Dispose();58 }59 }60}61using System;62using PuppeteerSharp;63{64 {65 static void Main(string[] args)66 {67 var browserFetcher = new BrowserFetcher();68 browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision).GetAwaiter

Full Screen

Full Screen

Dispose

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 BrowserFetcher browserFetcher = new BrowserFetcher();9 await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);10 browserFetcher.Dispose();11 }12 }13}14public Task DownloadAsync(string revision)15using PuppeteerSharp;16using System;17using System.Threading.Tasks;18{19 {20 static async Task Main(string[] args)21 {22 BrowserFetcher browserFetcher = new BrowserFetcher();23 await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);24 }25 }26}27public RevisionInfo RevisionInfo(string revision)28using PuppeteerSharp;29using System;30using System.Threading.Tasks;31{32 {33 static async Task Main(string[] args)34 {35 BrowserFetcher browserFetcher = new BrowserFetcher();36 await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);37 var revisionInfo = browserFetcher.RevisionInfo(BrowserFetcher.DefaultRevision);38 Console.WriteLine(revisionInfo.Revision);39 Console.WriteLine(revisionInfo.Local);40 Console.WriteLine(revisionInfo.Url);41 }42 }43}

Full Screen

Full Screen

Dispose

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var fetcher = new BrowserFetcher();9 var revisionInfo = await fetcher.DownloadAsync(BrowserFetcher.DefaultRevision);10 var browser = await Puppeteer.LaunchAsync(new LaunchOptions11 {12 });13 await browser.CloseAsync();14 fetcher.Dispose();15 }16 }17}

Full Screen

Full Screen

Dispose

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 public static async Task Main(string[] args)7 {8 var browserFetcher = new BrowserFetcher();9 var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);10 var browser = await Puppeteer.LaunchAsync(new LaunchOptions11 {12 });13 await browser.CloseAsync();14 browserFetcher.Dispose();15 }16 }17}18using PuppeteerSharp;19using System;20using System.Threading.Tasks;21{22 {23 public static async Task Main(string[] args)24 {25 var browserFetcher = new BrowserFetcher();26 var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);27 var browser = await Puppeteer.LaunchAsync(new LaunchOptions28 {29 });30 await browser.CloseAsync();31 browserFetcher.Dispose();32 browser.Dispose();33 }34 }35}36using PuppeteerSharp;37using System;38using System.Threading.Tasks;39{40 {41 public static async Task Main(string[] args)42 {43 var browserFetcher = new BrowserFetcher();44 var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);45 var browser = await Puppeteer.LaunchAsync(new LaunchOptions46 {47 });48 var page = await browser.NewPageAsync();49 await browser.CloseAsync();50 browserFetcher.Dispose();51 browser.Dispose();52 page.Dispose();53 }54 }55}56using PuppeteerSharp;57using System;58using System.Threading.Tasks;59{60 {61 public static async Task Main(string[] args)62 {63 var browserFetcher = new BrowserFetcher();

Full Screen

Full Screen

Dispose

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browserFetcher = new BrowserFetcher();9 var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);10 Console.WriteLine(revisionInfo.RevisionInfo.Local);11 Console.WriteLine("Press any key to exit...");12 Console.ReadKey();13 }14 }15}16using System;17using System.Threading.Tasks;18using PuppeteerSharp;19{20 {21 static async Task Main(string[] args)22 {23 var browserFetcher = new BrowserFetcher();24 var revisionInfo = await browserFetcher.DownloadAsync(533271);25 Console.WriteLine(revisionInfo.RevisionInfo.Local);26 Console.WriteLine("Press any key to exit...");27 Console.ReadKey();28 }29 }30}31using System;32using System.Threading.Tasks;33using PuppeteerSharp;34{35 {36 static async Task Main(string[] args)37 {

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful