Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.ScreenshotTests.ElementHandleScreenshotTests.ShouldWork
ElementHandleScreenshotTests.cs
Source:ElementHandleScreenshotTests.cs  
...13        {14        }15        [PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should work")]16        [PuppeteerFact]17        public async Task ShouldWork()18        {19            #region SetViewportAsync20            await Page.SetViewportAsync(new ViewPortOptions21            {22                Width = 500,23                Height = 50024            });25            #endregion26            await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");27            await Page.EvaluateExpressionAsync("window.scrollBy(50, 100)");28            var elementHandle = await Page.QuerySelectorAsync(".box:nth-of-type(3)");29            var screenshot = await elementHandle.ScreenshotDataAsync();30            Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-bounding-box.png", screenshot));31        }32        [PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should take into account padding and border")]33        [PuppeteerFact]34        public async Task ShouldTakeIntoAccountPaddingAndBorder()35        {36            await Page.SetViewportAsync(new ViewPortOptions37            {38                Width = 500,39                Height = 50040            });41            await Page.SetContentAsync(@"42                something above43                <style> div {44                    border: 2px solid blue;45                    background: green;46                    width: 50px;47                    height: 50px;48                }49                </style>50                <div></div>51            ");52            var elementHandle = await Page.QuerySelectorAsync("div");53            var screenshot = await elementHandle.ScreenshotDataAsync();54            Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-padding-border.png", screenshot));55        }56        [PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should capture full element when larger than viewport")]57        [SkipBrowserFact(skipFirefox: true)]58        public async Task ShouldCaptureFullElementWhenLargerThanViewport()59        {60            await Page.SetViewportAsync(new ViewPortOptions61            {62                Width = 500,63                Height = 50064            });65            await Page.SetContentAsync(@"66                something above67                <style>68                div.to-screenshot {69                  border: 1px solid blue;70                  width: 600px;71                  height: 600px;72                  margin-left: 50px;73                }74                ::-webkit-scrollbar{75                  display: none;76                }77                </style>78                <div class='to-screenshot'></div>"79            );80            var elementHandle = await Page.QuerySelectorAsync("div.to-screenshot");81            var screenshot = await elementHandle.ScreenshotDataAsync();82            Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-larger-than-viewport.png", screenshot));83            Assert.Equal(JToken.FromObject(new { w = 500, h = 500 }),84                await Page.EvaluateExpressionAsync("({ w: window.innerWidth, h: window.innerHeight })"));85        }86        [PuppeteerFact]87        public async Task ShouldScrollElementIntoView()88        {89            await Page.SetViewportAsync(new ViewPortOptions90            {91                Width = 500,92                Height = 50093            });94            await Page.SetContentAsync(@"95                something above96                <style> div.above {97                    border: 2px solid blue;98                    background: red;99                    height: 1500px;100                }101                div.to-screenshot {102                    border: 2px solid blue;103                    background: green;104                    width: 50px;105                    height: 50px;106                }107                </style>108                <div class='above'></div>109                <div class='to-screenshot'></div>110            ");111            var elementHandle = await Page.QuerySelectorAsync("div.to-screenshot");112            var screenshot = await elementHandle.ScreenshotDataAsync();113            Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-scrolled-into-view.png", screenshot));114        }115        [PuppeteerFact]116        public async Task ShouldWorkWithARotatedElement()117        {118            await Page.SetViewportAsync(new ViewPortOptions119            {120                Width = 500,121                Height = 500122            });123            await Page.SetContentAsync(@"124                <div style='position: absolute;125                top: 100px;126                left: 100px;127                width: 100px;128                height: 100px;129                background: green;130                transform: rotateZ(200deg); '> </div>131            ");132            var elementHandle = await Page.QuerySelectorAsync("div");133            var screenshot = await elementHandle.ScreenshotDataAsync();134            Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-rotate.png", screenshot));135        }136        [SkipBrowserFact(skipFirefox: true)]137        public async Task ShouldFailToScreenshotADetachedElement()138        {139            await Page.SetContentAsync("<h1>remove this</h1>");140            var elementHandle = await Page.QuerySelectorAsync("h1");141            await Page.EvaluateFunctionAsync("element => element.remove()", elementHandle);142            var exception = await Assert.ThrowsAsync<PuppeteerException>(elementHandle.ScreenshotStreamAsync);143            Assert.Equal("Node is either not visible or not an HTMLElement", exception.Message);144        }145        [PuppeteerFact]146        public async Task ShouldNotHangWithZeroWidthHeightElement()147        {148            await Page.SetContentAsync(@"<div style='width: 50px; height: 0'></div>");149            var elementHandle = await Page.QuerySelectorAsync("div");150            var exception = await Assert.ThrowsAsync<PuppeteerException>(elementHandle.ScreenshotDataAsync);151            Assert.Equal("Node has 0 height.", exception.Message);152        }153        [PuppeteerFact]154        public async Task ShouldWorkForAnElementWithFractionalDimensions()155        {156            await Page.SetContentAsync("<div style=\"width:48.51px;height:19.8px;border:1px solid black;\"></div>");157            var elementHandle = await Page.QuerySelectorAsync("div");158            var screenshot = await elementHandle.ScreenshotDataAsync();159            Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional.png", screenshot));160        }161        [SkipBrowserFact(skipFirefox: true)]162        public async Task ShouldWorkForAnElementWithAnOffset()163        {164            await Page.SetContentAsync("<div style=\"position:absolute; top: 10.3px; left: 20.4px;width:50.3px;height:20.2px;border:1px solid black;\"></div>");165            var elementHandle = await Page.QuerySelectorAsync("div");166            var screenshot = await elementHandle.ScreenshotDataAsync();167            Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional-offset.png", screenshot));168        }169    }170}...ShouldWork
Using AI Code Generation
1{2    [Collection("PuppeteerLoaderFixture collection")]3    {4        public async Task ShouldWork()5        {6            await Page.SetContentAsync(@"7                "#);8            var elementHandle = await Page.QuerySelectorAsync("div");9            var screenshot = await elementHandle.ScreenshotDataAsync();10            Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-bounding-box.png", screenshot) < 0.1);11        }12    }13}14{15    [Collection("PuppeteerLoaderFixture collection")]16    {17        public async Task ShouldWork()18        {19            await Page.SetContentAsync(@"20                <div style=""display: inline-block; width: 14px; height: 14px; background-color: green;""></div>21                <iframe style=""display: inline-block; width: 50px; height: 50px; background-color: red;"" id=""theFrame"" ></iframe>22                <div style=""display: inline-block; width: 14px; height: 14px; background-color: blue;""></div>23                "#);24            var frame = Page.Frames[1];25            var screenshot = await frame.ScreenshotDataAsync();26            Assert.True(ScreenshotHelper.PixelMatch("screenshot-frame.png", screenshot) < 0.1);27        }28    }29}30{31    [Collection("PuppeteerLoaderFixture collection")]32    {33        public async Task ShouldWork()34        {35            await Page.SetViewportAsync(new ViewPortOptions36            {37            });38            await Page.SetContentAsync(@"39                <div style=""width: 100px; height: 100px; background: red""></div>ShouldWork
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6{7    {8        [PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should work")]9        [SkipBrowserFact(skipFirefox: true)]10        public async Task ShouldWork()11        {12            await Page.SetContentAsync("<div style=\"width: 500px; height: 500px; background: red;\"></div>");13            var elementHandle = await Page.QuerySelectorAsync("div");14            var screenshot = await elementHandle.ScreenshotDataAsync();15            Assert.True(ScreenshotHelper.PixelMatch("redbox", screenshot) < 0.1);16        }17    }18}19using System;20using System.Collections.Generic;21using System.Linq;22using System.Text;23using System.Threading.Tasks;24{25    {26        [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should work")]27        [SkipBrowserFact(skipFirefox: true)]28        public async Task ShouldWork()29        {30            await Page.SetViewportAsync(new ViewPortOptions31            {32            });33            await Page.SetContentAsync("<div style=\"width: 50px; height: 50px; background: red;\"></div>");34            var screenshot = await Page.ScreenshotDataAsync();35            Assert.True(ScreenshotHelper.PixelMatch("redbox", screenshot) < 0.1);36        }37    }38}39using System;40using System.Collections.Generic;41using System.Linq;42using System.Text;43using System.Threading.Tasks;44{45    {46        [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should take fullPage screenshots")]47        [SkipBrowserFact(skipFirefox: true)]48        public async Task ShouldTakeFullPageScreenshots()49        {ShouldWork
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Text;4using System.Threading.Tasks;5using PuppeteerSharp.Tests.Attributes;6using PuppeteerSharp.Xunit;7using Xunit;8using Xunit.Abstractions;9{10    [Collection(TestConstants.TestFixtureCollectionName)]11    {12        public ElementHandleScreenshotTests(ITestOutputHelper output) : base(output)13        {14        }15        [PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should work")]16        public async Task ShouldWork()17        {18            await Page.SetContentAsync("<div style=\"width: 500px; height: 500px; background: blue;\"></div>");19            var element = await Page.QuerySelectorAsync("div");20            var screenshot = await element.ScreenshotDataAsync();21            Assert.Equal(500, screenshot.Width);22            Assert.Equal(500, screenshot.Height);23        }24    }25}26using System;27using System.Collections.Generic;28using System.Text;29using System.Threading.Tasks;30using PuppeteerSharp.Tests.Attributes;31using PuppeteerSharp.Xunit;32using Xunit;33using Xunit.Abstractions;34{35    [Collection(TestConstants.TestFixtureCollectionName)]36    {37        public ElementHandleScreenshotTests(ITestOutputHelper output) : base(output)38        {39        }40        [PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should take into account padding and border")]41        public async Task ShouldTakeIntoAccountPaddingAndBorder()42        {43            await Page.SetContentAsync("<div style=\"width: 50px; height: 50px; border: 2px solid red; background: blue;\"></div>");44            var element = await Page.QuerySelectorAsync("div");45            var screenshot = await element.ScreenshotDataAsync();46            Assert.Equal(54, screenshot.Width);47            Assert.Equal(54, screenshot.Height);48        }49    }50}51using System;52using System.Collections.Generic;ShouldWork
Using AI Code Generation
1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5{6    {7        public static async Task ShouldWork()8        {9            var browser = await Puppeteer.LaunchAsync(new LaunchOptions10            {11            });12            var page = await browser.NewPageAsync();13            await page.SetViewportAsync(new ViewPortOptions14            {15            });16            await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");17            var elementHandle = await page.QuerySelectorAsync(".box:nth-of-type(13)");18            var screenshot = await elementHandle.ScreenshotDataAsync();19            await File.WriteAllBytesAsync("screenshot.png", screenshot);20            await browser.CloseAsync();21        }22    }23}24using System;25using System.IO;26using System.Threading.Tasks;27using PuppeteerSharp;28{29    {30        public static async Task ShouldWork()31        {32            var browser = await Puppeteer.LaunchAsync(new LaunchOptions33            {34            });35            var page = await browser.NewPageAsync();36            await page.SetViewportAsync(new ViewPortOptions37            {38            });39            await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");40            var frame = page.Frames[1];41            var screenshot = await frame.ScreenshotDataAsync();42            await File.WriteAllBytesAsync("screenshot.png", screenshot);43            await browser.CloseAsync();44        }45    }46}47using System;48using System.IO;49using System.Threading.Tasks;50using PuppeteerSharp;51{52    {53        public static async Task ShouldWork()54        {55            var browser = await Puppeteer.LaunchAsync(new LaunchOptions56            {57            });58            var page = await browser.NewPageAsync();59            await page.SetViewportAsync(new ViewPortOptions60            {ShouldWork
Using AI Code Generation
1var screenshotTests = new PuppeteerSharp.Tests.ScreenshotTests.ElementHandleScreenshotTests();2screenshotTests.ShouldWork();3var screenshotTests = new PuppeteerSharp.Tests.ScreenshotTests.FrameScreenshotTests();4screenshotTests.ShouldWork();5var screenshotTests = new PuppeteerSharp.Tests.ScreenshotTests.PageScreenshotTests();6screenshotTests.ShouldWork();7var screenshotTests = new PuppeteerSharp.Tests.ScreenshotTests.PageScreenshotTests();8screenshotTests.ShouldWork();9var screenshotTests = new PuppeteerSharp.Tests.ScreenshotTests.PageScreenshotTests();10screenshotTests.ShouldWork();11var screenshotTests = new PuppeteerSharp.Tests.ScreenshotTests.PageScreenshotTests();12screenshotTests.ShouldWork();13var screenshotTests = new PuppeteerSharp.Tests.ScreenshotTests.PageScreenshotTests();14screenshotTests.ShouldWork();15var screenshotTests = new PuppeteerSharp.Tests.ScreenshotTests.PageScreenshotTests();16screenshotTests.ShouldWork();17var screenshotTests = new PuppeteerSharp.Tests.ScreenshotTests.PageScreenshotTests();18screenshotTests.ShouldWork();19var screenshotTests = new PuppeteerSharp.Tests.ScreenshotTests.PageScreenshotTests();20screenshotTests.ShouldWork();ShouldWork
Using AI Code Generation
1var  puppeteer  =  new  PuppeteerSharp.Puppeteer();2 var  browser  =  await  puppeteer.LaunchAsync(new  PuppeteerSharp.LaunchOptions() { Headless =  false  });3 var  page  =  await  browser.NewPageAsync();4 var  elementHandle  =  await  page.QuerySelectorAsync( "input[name=q]" );5 var  screenshot  =  await  elementHandle.ScreenshotDataAsync();6 await  page.CloseAsync();7 await  browser.CloseAsync();8 var  puppeteer  =  new  PuppeteerSharp.Puppeteer();9 var  browser  =  await  puppeteer.LaunchAsync(new  PuppeteerSharp.LaunchOptions() { Headless =  false  });10 var  page  =  await  browser.NewPageAsync();11 var  elementHandle  =  await  page.QuerySelectorAsync( "input[name=q]" );12 var  screenshot  =  await  elementHandle.ScreenshotStreamAsync();13 await  page.CloseAsync();14 await  browser.CloseAsync();15 var  puppeteer  =  new  PuppeteerSharp.Puppeteer();16 var  browser  =  await  puppeteer.LaunchAsync(new  PuppeteerSharp.LaunchOptions() { Headless =  false  });17 var  page  =  await  browser.NewPageAsync();18 var  elementHandle  =  await  page.QuerySelectorAsync( "input[name=q]" );19 var  screenshot  =  await  elementHandle.ScreenshotFileAsync( "screenshot.png" );20 await  page.CloseAsync();21 await  browser.CloseAsync();22 var  puppeteer  =  new  PuppeteerSharp.Puppeteer();23 var  browser  =  await  puppeteer.LaunchAsync(new  PuppeteerSharp.LaunchOptions() { Headless =  falseShouldWork
Using AI Code Generation
1public   void  ShouldWork()2{3   {4     {5        $"--window-size={1024},{768}" ,6        $"--window-position={0},{0}" 7     }8   };9    using  ( var  browser =  await  Puppeteer.LaunchAsync(options))10   {11      var  page =  await  browser.NewPageAsync();12      await  page.SetViewportAsync( new  ViewPortOptions13     {14     });15      await  page.EvaluateExpressionAsync( "document.body.style.backgroundColor = 'red'" );16      var  elementHandle =  await  page.QuerySelectorAsync( "body" );17      var  screenshot =  await  elementHandle.ScreenshotDataAsync();18      File.WriteAllBytes( "5.png" , screenshot);19   }20}21public   void  ShouldWork()22{23   {24     {25        $"--window-size={1024},{768}" ,26        $"--window-position={0},{0}" 27     }28   };29    using  ( var  browser =  await  Puppeteer.LaunchAsync(options))30   {31      var  page =  await  browser.NewPageAsync();32      await  page.SetViewportAsync( new  ViewPortOptions33     {34     });35      await  page.EvaluateExpressionAsync( "document.body.style.backgroundColor = 'red'" );36      var  frame =  await  page.QuerySelectorAsync( "iframe" );37      var  screenshot =  await  frame.ScreenshotDataAsync();38      File.WriteAllBytes( "6.png" , screenshot);39   }40}ShouldWork
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Threading.Tasks;5using PuppeteerSharp.Tests.ScreenshotTests;6{7    {8        [PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should work")]9        public async Task ShouldWork()10        {11            await Page.SetContentAsync(@"12            ");13            var element = await Page.QuerySelectorAsync("div");14            var screenshot = await element.ScreenshotDataAsync();15            Assert.True(ScreenshotHelper.PixelMatch(@"screenshot-element-bounds.png", screenshot));16        }17    }18}19using System;20using System.Collections.Generic;21using System.Linq;22using System.Threading.Tasks;23using PuppeteerSharp.Tests.ScreenshotTests;24{25    {26        [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should work")]27        public async Task ShouldWork()28        {29            await Page.SetContentAsync(@"30            ");31            var screenshot = await Page.ScreenshotDataAsync();32            Assert.True(ScreenshotHelper.PixelMatch(@"screenshot.png", screenshot));33        }34    }35}36using System;37using System.Collections.Generic;38using System.Linq;39using System.Threading.Tasks;40using PuppeteerSharp.Tests.ScreenshotTests;41{42    {43        [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should work")]44        public async Task ShouldWork()45        {46            await Page.SetContentAsync(@"47            ");48            var screenshot = await Page.ScreenshotDataAsync();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!!
