Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.ScreenshotAsync
LottoXClient.cs
Source:LottoXClient.cs
...145 }146 private async Task TakeTopOrderScreenshot(string filepath)147 {148 Page page = await GetPage();149 await page.ScreenshotAsync(filepath,150 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 1000, Height = 400, X = 1000, Y = 350 } });151 }152 private async Task TakeOrdersScreenshot(string filepath)153 {154 Page page = await GetPage();155 await page.ScreenshotAsync(filepath,156 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 983, Height = 1440, X = 1017 } });157 }158 private async Task TakeHeaderScreenshot(string filepath)159 {160 Page page = await GetPage();161 await page.ScreenshotAsync(filepath,162 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 1000, Height = 350, X = 250 } });163 }164 private async Task TakeQuantityColumnScreenshot(string filePath)165 {166 Page page = await GetPage();167 await page.ScreenshotAsync(filePath,168 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 65, Height = 350, X = 477, Y = 400 } });169 }170 private async Task TakePortfolioScreenshot(string filePath)171 {172 Page page = await GetPage();173 await page.ScreenshotAsync(filePath,174 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 1000, Height = 1440 } });175 }176 }177}...
SaveWebPage.xaml.cs
Source:SaveWebPage.xaml.cs
...58 {59 fileName = fileName.Replace(".jpg", $"{DateTime.Now.ToString("ffff")}.jpg");60 }6162 await page.ScreenshotAsync(fileName, screenshotOptions);6364 if (System.IO.File.Exists(fileName))65 {66 BitmapImage bi = new BitmapImage();67 bi.BeginInit();68 bi.UriSource = new Uri(fileName, UriKind.Absolute);69 bi.EndInit();70 this.image.Source = bi;71 }72 else73 {74 EMessageBox.Show("ä¿åç½é¡µæªå¾å¤±è´¥");75 }76
...
PuppeteerWrapper.cs
Source:PuppeteerWrapper.cs
...72 case SourceType.URL:73 {74 var page = await browser.NewPageAsync();75 await page.GoToAsync("http://www.google.com");76 await page.ScreenshotAsync(outputPath + "\\" + fileName);77 break;78 }79 case SourceType.HTML_FILE:80 {81 var page = await browser.NewPageAsync();82 await page.GoToAsync(sourceData);83 await page.ScreenshotAsync(outputPath + "\\" + fileName);84 break;85 }86 case SourceType.HTML_STRING:87 {88 using (var page = await browser.NewPageAsync())89 {90 await page.SetContentAsync(sourceData);91 var result = await page.GetContentAsync();92 await page.ScreenshotAsync(outputPath + "\\" + fileName);93 }94 break;95 }96 }97 }98 public async Task GetScreenshots(string fileName, ViewPortOptions viewPortOptions)99 {100 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);101 var browser = await Puppeteer.LaunchAsync(new LaunchOptions102 {103 Headless = true104 });105 switch (sourceType)106 {107 case SourceType.URL:108 {109 var page = await browser.NewPageAsync();110 await page.GoToAsync("http://www.google.com");111 await page.SetViewportAsync(viewPortOptions);112 await page.ScreenshotAsync(outputPath + "\\" + fileName);113 break;114 }115 case SourceType.HTML_FILE:116 {117 var page = await browser.NewPageAsync();118 await page.GoToAsync(sourceData);119 await page.SetViewportAsync(viewPortOptions);120 await page.ScreenshotAsync(outputPath + "\\" + fileName);121 break;122 }123 case SourceType.HTML_STRING:124 {125 using (var page = await browser.NewPageAsync())126 {127 await page.SetContentAsync(sourceData);128 var result = await page.GetContentAsync();129 await page.SetViewportAsync(viewPortOptions);130 await page.ScreenshotAsync(outputPath + "\\" + fileName);131 }132 break;133 }134 }135 }136 }137}
Form1.cs
Source:Form1.cs
...53 screenshotOptions.FullPage = true; //æ¯å¦æªåæ´ä¸ªé¡µé¢54 screenshotOptions.OmitBackground = false;//æ¯å¦ä½¿ç¨éæèæ¯ï¼èä¸æ¯é»è®¤ç½è²èæ¯55 screenshotOptions.Quality = 100; //æªå¾è´¨é 0-100ï¼pngä¸å¯ç¨ï¼56 screenshotOptions.Type = ScreenshotType.Jpeg; //æªå¾æ ¼å¼ 57 await page.ScreenshotAsync(path,screenshotOptions);58 MessageBox.Show($"æªå¾å·²ç»ä¿åè³{path}");59 }60 private async void button3_Click(object sender, EventArgs e)61 {62 //è·åæåçæ ç¾é¡µ63 var page = (await browser.PagesAsync()).Last();64 var path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "screenshot.pdf");65 //设置PDFé项66 PdfOptions pdfOptions = new PdfOptions();67 pdfOptions.DisplayHeaderFooter = false; //æ¯å¦æ¾ç¤ºé¡µç页è68 pdfOptions.FooterTemplate = ""; //页èææ¬69 pdfOptions.Format = new PuppeteerSharp.Media.PaperFormat(8.27m,11.69m); //pdfçº¸å¼ æ ¼å¼ è±å¯¸ä¸ºåä½ 70 pdfOptions.HeaderTemplate = ""; //页çææ¬71 pdfOptions.Landscape = false; //çº¸å¼ æ¹å false-åç´ true-æ°´å¹³ ...
BookingScrapperTest.cs
Source:BookingScrapperTest.cs
...34 await page.SetCookieAsync(cookie);35 }36 }37 await page.GoToAsync("https://admin.booking.com");38 await page.ScreenshotAsync(screenshotFilePath);39 await page.Keyboard.TypeAsync("2453996");40 await page.ClickAsync("form.nw-signin button[type=\"submit\"]");41 var passwordInput = await page.WaitForSelectorAsync("input#password");42 await passwordInput.TypeAsync("booking-johnnyx1A");43 var passwordSubmitButton = await page.WaitForSelectorAsync("form.nw-signin button[type=\"submit\"]");44 navigationTask = page.WaitForNavigationAsync(new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Networkidle0 } });45 await passwordSubmitButton.ClickAsync();46 await navigationTask;47 await page.ScreenshotAsync(screenshotFilePath);48 if(page.Url.Contains("ses="))49 {50 return new CookieParam[2];51 }52 else53 {54 var pulseButton = await page.WaitForSelectorAsync("a.nw-pulse-verification-link");55 await pulseButton.ClickAsync();56 var pulseCodeInput = await page.WaitForSelectorAsync("input#sms_code");57 await pulseCodeInput.TypeAsync("706860");58 await page.ScreenshotAsync(screenshotFilePath);59 var pulseCodeSubmitButton = await page.WaitForSelectorAsync("form.nw-tfa-form button[type=\"submit\"]");60 navigationTask = page.WaitForNavigationAsync(new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Networkidle0 } });61 await pulseCodeSubmitButton.ClickAsync();62 await navigationTask;63 await page.ScreenshotAsync(screenshotFilePath);64 return await page.GetCookiesAsync();65 }66 67 }68 [TestMethod]69 public async Task DownloadReservations()70 {71 var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "reservations.xls");72 var client = new HttpClient();73 client.DefaultRequestHeaders.Host = "admin.booking.com";74 client.DefaultRequestHeaders.Add("Cookie", "esadm=02UmFuZG9tSVYkc2RlIyh9YbxZGyl9Y5%2BPynjCLxCQ5SnKFbrr57DvXZw1sp2yFYUddAZCaighbCs%3D; ecid=WItRz%2BR36BGkGg028uL7CQan; bkng_bfp=aef6b2e1b76f06aacb6f0c2b8f99a513; auth_token=6412390550; ux=e; hauavc=01be565IQWtYpsK1NdHtF%2BjRchlkDi%2FoOolSuOOJywu2bUemY0; bkng_bfp=86f162fcd73527f33dc06228d1d588a0; external_host=account.booking.com; xp=02UmFuZG9tSVYkc2RlIyh9YbxZGyl9Y5%2BPpjNyy1T7WUQFoQgXZsIpfhJITCOuSk2SV8Ljk3HCReM%3D; liteha=%5B%7B%22actions%22%3A%5B%5D%2C%22page%22%3A%22home%22%7D%2C%7B%22actions%22%3A%5B%5D%2C%22page%22%3A%22finance_reservations%22%7D%2C%7B%22actions%22%3A%5B%5D%2C%22page%22%3A%22search_reservations%22%7D%5D; bkng_iam_rt=CAESQlJAKiYcMd1HOoS9iVxT8CQ0oRWVsb4cqGtRVYwapJXGqcQvKA824B18ua8LSvtXsG0V5U3sUlr95FYOhPcqZ_cHCw; he=02UmFuZG9tSVYkc2RlIyh9YbxZGyl9Y5%2BPMzBUOmE1%2BZnRjE6ccsHLcr3XeaLhlmueT7Ji33TyEzQ%3D");75 var data = await client.GetByteArrayAsync(@"https://admin.booking.com/fresa/extranet/reservations/download?date_type=arrival&date_to=2019-01-10&date_from=2019-12-31&lang=xu&hotel_id=2453996&ses=4e1e6fd18bafca6079649931b72438c6");76 File.WriteAllBytes(filePath, data);77 }...
Program.cs
Source:Program.cs
...3536 37 var element = await page.QuerySelectorAsync("#MainContent_ASPxRoundPanel2_WebChartControl1_IMG");38 //var element = await page.EvaluateExpressionAsync<ElementHandle>("document.getElementById('MainContent_ASPxRoundPanel2_WebChartControl1_IMG')");39 await element.ScreenshotAsync("gurka.png");4041 //await page.ScreenshotAsync("test.png", new ScreenshotOptions42 //{43 // Clip = new Clip()44 // {45 // X = 10,46 // Y = 10,47 // Height = 100,48 // Width = 10049 // }50 //});515253 var res= await page.EvaluateExpressionAsync<string>("document.getElementById('MainContent_signalpagedailycommentarytext').textContent");5455 element = await page.QuerySelectorAsync("#MainContent_signalpagedailycommentarytext");
...
PageExtensions.cs
Source:PageExtensions.cs
...40 if (deltaInfo.Exists)41 {42 deltaInfo.Delete();43 }44 await page.ScreenshotAsync(actualPath).ConfigureAwait(false);45 using var actualBitmap = new Bitmap(actualPath);46 TestContext.AddTestAttachment(actualPath);47 var expectedInfo = new FileInfo(expectedPath);48 if (!expectedInfo.Exists)49 {50 // if this is the first time the test has executed then assume the current screenshot is expected51 actualBitmap.Save(expectedPath);52 }53 using var expectedBitmap = new Bitmap(expectedPath);54 var compare = actualBitmap.Compare(expectedBitmap);55 if (compare != null)56 {57 compare.Save(deltaPath);58 TestContext.AddTestAttachment(deltaPath);...
ImageService.cs
Source:ImageService.cs
...17 BrowserWSEndpoint = _config.GetSection(BrowserWSKey).Value18 });19 var page = await browser.NewPageAsync();20 await page.GoToAsync("https://www.google.com");21 await page.ScreenshotAsync("screenshot.png");22 }23 }24}...
ScreenshotAsync
Using AI Code Generation
1using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions2{3 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",4}))5{6 var page = await browser.NewPageAsync();7 await page.ScreenshotAsync("google.png");8}9using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions10{11 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",12}))13{14 var page = await browser.NewPageAsync();15 await page.ScreenshotAsync("google.png");16}17using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions18{19 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",20}))21{22 var page = await browser.NewPageAsync();23 await page.ScreenshotAsync("google.png");24}25using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions26{27 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",28}))29{30 var page = await browser.NewPageAsync();31 await page.ScreenshotAsync("google.png");32}33using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions34{35 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",36}))37{38 var page = await browser.NewPageAsync();39 await page.ScreenshotAsync("google.png");40}
ScreenshotAsync
Using AI Code Generation
1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5{6 {7 static async Task Main(string[] args)8 {9 var options = new LaunchOptions { Headless = true };10 using (var browser = await Puppeteer.LaunchAsync(options))11 {12 using (var page = await browser.NewPageAsync())13 {14 var screenshot = await page.ScreenshotAsync();15 await File.WriteAllBytesAsync("screenshot.png", screenshot);16 }17 }18 }19 }20}21using System;22using System.IO;23using System.Threading.Tasks;24using PuppeteerSharp;25{26 {27 static async Task Main(string[] args)28 {29 var options = new LaunchOptions { Headless = true };30 using (var browser = await Puppeteer.LaunchAsync(options))31 {32 using (var page = await browser.NewPageAsync())33 {
ScreenshotAsync
Using AI Code Generation
1var page = await browser.NewPageAsync();2await page.ScreenshotAsync("1.png");3var page = await browser.NewPageAsync();4await page.ScreenshotAsync("2.png");5var page = await browser.NewPageAsync();6await page.ScreenshotAsync("3.png");7var page = await browser.NewPageAsync();8await page.ScreenshotAsync("4.png");9var page = await browser.NewPageAsync();10await page.ScreenshotAsync("5.png");11var page = await browser.NewPageAsync();12await page.ScreenshotAsync("6.png");13var page = await browser.NewPageAsync();14await page.ScreenshotAsync("7.png");15var page = await browser.NewPageAsync();16await page.ScreenshotAsync("8.png");17var page = await browser.NewPageAsync();18await page.ScreenshotAsync("9.png");19var page = await browser.NewPageAsync();20await page.ScreenshotAsync("10.png");
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!!