Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.ScreenshotHelper
PageScreenshotTests.cs
Source:PageScreenshotTests.cs
...34 }35 await page.ScreenshotAsync(outputFile);36 fileInfo = new FileInfo(outputFile);37 Assert.True(new FileInfo(outputFile).Length > 0);38 Assert.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", outputFile));39 if (fileInfo.Exists)40 {41 fileInfo.Delete();42 }43 }44 }45 [PuppeteerFact(Timeout = -1)]46 public async Task Usage()47 {48 var outputFile = Path.Combine(BaseDirectory, "Usage.png");49 var fileInfo = new FileInfo(outputFile);50 if (fileInfo.Exists)51 {52 fileInfo.Delete();53 }54 #region ScreenshotAsync55 using var browserFetcher = new BrowserFetcher();56 await browserFetcher.DownloadAsync();57 await using var browser = await Puppeteer.LaunchAsync(58 new LaunchOptions { Headless = true });59 await using var page = await browser.NewPageAsync();60 await page.GoToAsync("http://www.google.com");61 await page.ScreenshotAsync(outputFile);62 #endregion63 Assert.True(File.Exists(outputFile));64 }65 [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should work")]66 [SkipBrowserFact(skipFirefox: true)]67 public async Task ShouldWork()68 {69 await using (var page = await Context.NewPageAsync())70 {71 await page.SetViewportAsync(new ViewPortOptions72 {73 Width = 500,74 Height = 50075 });76 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");77 var screenshot = await page.ScreenshotDataAsync();78 Assert.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", screenshot));79 }80 }81 [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should clip rect")]82 [PuppeteerFact]83 public async Task ShouldClipRect()84 {85 await using (var page = await Context.NewPageAsync())86 {87 await page.SetViewportAsync(new ViewPortOptions88 {89 Width = 500,90 Height = 50091 });92 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");93 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions94 {95 Clip = new Clip96 {97 X = 50,98 Y = 100,99 Width = 150,100 Height = 100101 }102 });103 Assert.True(ScreenshotHelper.PixelMatch("screenshot-clip-rect.png", screenshot));104 }105 }106 [PuppeteerFact]107 public async Task ShouldClipScale()108 {109 await using (var page = await Context.NewPageAsync())110 {111 await page.SetViewportAsync(new ViewPortOptions112 {113 Width = 500,114 Height = 500115 });116 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");117 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions118 {119 Clip = new Clip120 {121 X = 50,122 Y = 100,123 Width = 150,124 Height = 100,125 Scale = 2126 }127 });128 Assert.True(ScreenshotHelper.PixelMatch("screenshot-clip-rect-scale.png", screenshot));129 }130 }131 [SkipBrowserFact(skipFirefox: true)]132 public async Task ShouldClipElementsToTheViewport()133 {134 await using (var page = await Context.NewPageAsync())135 {136 await page.SetViewportAsync(new ViewPortOptions { Width = 500, Height = 500 });137 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");138 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions139 {140 Clip = new Clip141 {142 X = 50,143 Y = 600,144 Width = 100,145 Height = 100146 }147 });148 Assert.True(ScreenshotHelper.PixelMatch("screenshot-offscreen-clip.png", screenshot));149 }150 }151 [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should run in parallel")]152 [PuppeteerFact]153 public async Task ShouldRunInParallel()154 {155 await using (var page = await Context.NewPageAsync())156 {157 await page.SetViewportAsync(new ViewPortOptions158 {159 Width = 500,160 Height = 500161 });162 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");163 var tasks = new List<Task<byte[]>>();164 for (var i = 0; i < 3; ++i)165 {166 tasks.Add(page.ScreenshotDataAsync(new ScreenshotOptions167 {168 Clip = new Clip169 {170 X = 50 * i,171 Y = 0,172 Width = 50,173 Height = 50174 }175 }));176 }177 await Task.WhenAll(tasks);178 Assert.True(ScreenshotHelper.PixelMatch("grid-cell-1.png", tasks[0].Result));179 }180 }181 [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should take fullPage screenshots")]182 [SkipBrowserFact(skipFirefox: true)]183 public async Task ShouldTakeFullPageScreenshots()184 {185 await using (var page = await Context.NewPageAsync())186 {187 await page.SetViewportAsync(new ViewPortOptions188 {189 Width = 500,190 Height = 500191 });192 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");193 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions194 {195 FullPage = true196 });197 Assert.True(ScreenshotHelper.PixelMatch("screenshot-grid-fullpage.png", screenshot));198 }199 }200 [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should run in parallel in multiple pages")]201 [SkipBrowserFact(skipFirefox: true)]202 public async Task ShouldRunInParallelInMultiplePages()203 {204 var n = 2;205 var pageTasks = new List<Task<Page>>();206 for (var i = 0; i < n; i++)207 {208 async Task<Page> func()209 {210 var page = await Context.NewPageAsync();211 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");212 return page;213 }214 pageTasks.Add(func());215 }216 await Task.WhenAll(pageTasks);217 var screenshotTasks = new List<Task<byte[]>>();218 for (var i = 0; i < n; i++)219 {220 screenshotTasks.Add(pageTasks[i].Result.ScreenshotDataAsync(new ScreenshotOptions221 {222 Clip = new Clip223 {224 X = 50 * i,225 Y = 0,226 Width = 50,227 Height = 50228 }229 }));230 }231 await Task.WhenAll(screenshotTasks);232 for (var i = 0; i < n; i++)233 {234 Assert.True(ScreenshotHelper.PixelMatch($"grid-cell-{i}.png", screenshotTasks[i].Result));235 }236 var closeTasks = new List<Task>();237 for (var i = 0; i < n; i++)238 {239 closeTasks.Add(pageTasks[i].Result.CloseAsync());240 }241 await Task.WhenAll(closeTasks);242 }243 [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should allow transparency")]244 [SkipBrowserFact(skipFirefox: true)]245 public async Task ShouldAllowTransparency()246 {247 await using (var page = await Context.NewPageAsync())248 {249 await page.SetViewportAsync(new ViewPortOptions250 {251 Width = 100,252 Height = 100253 });254 await page.GoToAsync(TestConstants.EmptyPage);255 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions256 {257 OmitBackground = true258 });259 Assert.True(ScreenshotHelper.PixelMatch("transparent.png", screenshot));260 }261 }262 [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should render white background on jpeg file")]263 [SkipBrowserFact(skipFirefox: true)]264 public async Task ShouldRenderWhiteBackgroundOnJpegFile()265 {266 await using (var page = await Context.NewPageAsync())267 {268 await page.SetViewportAsync(new ViewPortOptions { Width = 100, Height = 100 });269 await page.GoToAsync(TestConstants.EmptyPage);270 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions271 {272 OmitBackground = true,273 Type = ScreenshotType.Jpeg274 });275 Assert.True(ScreenshotHelper.PixelMatch("white.jpg", screenshot));276 }277 }278 [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should work with odd clip size on Retina displays")]279 [PuppeteerFact]280 public async Task ShouldWorkWithOddClipSizeOnRetinaDisplays()281 {282 await using (var page = await Context.NewPageAsync())283 {284 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions285 {286 Clip = new Clip287 {288 X = 0,289 Y = 0,290 Width = 11,291 Height = 11292 }293 });294 Assert.True(ScreenshotHelper.PixelMatch("screenshot-clip-odd-size.png", screenshot));295 }296 }297 [PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should return base64")]298 [SkipBrowserFact(skipFirefox: true)]299 public async Task ShouldReturnBase64()300 {301 await using (var page = await Context.NewPageAsync())302 {303 await page.SetViewportAsync(new ViewPortOptions304 {305 Width = 500,306 Height = 500307 });308 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");309 var screenshot = await page.ScreenshotBase64Async();310 Assert.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", Convert.FromBase64String(screenshot)));311 }312 }313 [PuppeteerFact]314 public void ShouldInferScreenshotTypeFromName()315 {316 Assert.Equal(ScreenshotType.Jpeg, ScreenshotOptions.GetScreenshotTypeFromFile("Test.jpg"));317 Assert.Equal(ScreenshotType.Jpeg, ScreenshotOptions.GetScreenshotTypeFromFile("Test.jpe"));318 Assert.Equal(ScreenshotType.Jpeg, ScreenshotOptions.GetScreenshotTypeFromFile("Test.jpeg"));319 Assert.Equal(ScreenshotType.Png, ScreenshotOptions.GetScreenshotTypeFromFile("Test.png"));320 Assert.Null(ScreenshotOptions.GetScreenshotTypeFromFile("Test.exe"));321 }322 [SkipBrowserFact(skipFirefox: true)]323 public async Task ShouldWorkWithQuality()324 {325 await using (var page = await Context.NewPageAsync())326 {327 await page.SetViewportAsync(new ViewPortOptions328 {329 Width = 500,330 Height = 500331 });332 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");333 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions334 {335 Type = ScreenshotType.Jpeg,336 FullPage = true,337 Quality = 100338 });339 Assert.True(ScreenshotHelper.PixelMatch("screenshot-grid-fullpage.png", screenshot));340 }341 }342 }343}...
ScreenshotTests.cs
Source:ScreenshotTests.cs
...32 }33 await page.ScreenshotAsync(outputFile);34 fileInfo = new FileInfo(outputFile);35 Assert.True(new FileInfo(outputFile).Length > 0);36 Assert.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", outputFile));37 if (fileInfo.Exists)38 {39 fileInfo.Delete();40 }41 }42 }43 [Fact]44 public async Task ShouldWork()45 {46 using (var page = await Browser.NewPageAsync())47 {48 await page.SetViewportAsync(new ViewPortOptions49 {50 Width = 500,51 Height = 50052 });53 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");54 var screenshot = await page.ScreenshotDataAsync();55 Assert.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", screenshot));56 }57 }58 [Fact]59 public async Task ShouldClipRect()60 {61 using (var page = await Browser.NewPageAsync())62 {63 await page.SetViewportAsync(new ViewPortOptions64 {65 Width = 500,66 Height = 50067 });68 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");69 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions70 {71 Clip = new Clip72 {73 X = 50,74 Y = 100,75 Width = 150,76 Height = 10077 }78 });79 Assert.True(ScreenshotHelper.PixelMatch("screenshot-clip-rect.png", screenshot));80 }81 }82 [Fact]83 public async Task ShouldWorkForOffscreenClip()84 {85 using (var page = await Browser.NewPageAsync())86 {87 await page.SetViewportAsync(new ViewPortOptions88 {89 Width = 500,90 Height = 50091 });92 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");93 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions94 {95 Clip = new Clip96 {97 X = 50,98 Y = 600,99 Width = 100,100 Height = 100101 }102 });103 Assert.True(ScreenshotHelper.PixelMatch("screenshot-offscreen-clip.png", screenshot));104 }105 }106 [Fact]107 public async Task ShouldRunInParallel()108 {109 using (var page = await Browser.NewPageAsync())110 {111 await page.SetViewportAsync(new ViewPortOptions112 {113 Width = 500,114 Height = 500115 });116 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");117 var tasks = new List<Task<byte[]>>();118 for (var i = 0; i < 3; ++i)119 {120 tasks.Add(page.ScreenshotDataAsync(new ScreenshotOptions121 {122 Clip = new Clip123 {124 X = 50 * i,125 Y = 0,126 Width = 50,127 Height = 50128 }129 }));130 }131 await Task.WhenAll(tasks);132 Assert.True(ScreenshotHelper.PixelMatch("grid-cell-1.png", tasks[0].Result));133 }134 }135 [Fact]136 public async Task ShouldTakeFullPageScreenshots()137 {138 using (var page = await Browser.NewPageAsync())139 {140 await page.SetViewportAsync(new ViewPortOptions141 {142 Width = 500,143 Height = 500144 });145 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");146 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions147 {148 FullPage = true149 });150 Assert.True(ScreenshotHelper.PixelMatch("screenshot-grid-fullpage.png", screenshot));151 }152 }153 [Fact]154 public async Task ShouldRunInParallelInMultiplePages()155 {156 var n = 2;157 var pageTasks = new List<Task<Page>>();158 for (var i = 0; i < n; i++)159 {160 Func<Task<Page>> func = async () =>161 {162 var page = await Browser.NewPageAsync();163 await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");164 return page;165 };166 pageTasks.Add(func());167 }168 await Task.WhenAll(pageTasks);169 var screenshotTasks = new List<Task<byte[]>>();170 for (var i = 0; i < n; i++)171 {172 screenshotTasks.Add(pageTasks[i].Result.ScreenshotDataAsync(new ScreenshotOptions173 {174 Clip = new Clip175 {176 X = 50 * i,177 Y = 0,178 Width = 50,179 Height = 50180 }181 }));182 }183 await Task.WhenAll(screenshotTasks);184 for (var i = 0; i < n; i++)185 {186 Assert.True(ScreenshotHelper.PixelMatch($"grid-cell-{i}.png", screenshotTasks[i].Result));187 }188 var closeTasks = new List<Task>();189 for (var i = 0; i < n; i++)190 {191 closeTasks.Add(pageTasks[i].Result.CloseAsync());192 }193 await Task.WhenAll(closeTasks);194 }195 [Fact]196 public async Task ShouldAllowTransparency()197 {198 using (var page = await Browser.NewPageAsync())199 {200 await page.SetViewportAsync(new ViewPortOptions201 {202 Width = 100,203 Height = 100204 });205 await page.GoToAsync(TestConstants.EmptyPage);206 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions207 {208 OmitBackground = true209 });210 Assert.True(ScreenshotHelper.PixelMatch("transparent.png", screenshot));211 }212 }213 [Fact]214 public async Task ShouldWorkWithOddClipSizeOnRetinaDisplays()215 {216 using (var page = await Browser.NewPageAsync())217 {218 var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions219 {220 Clip = new Clip221 {222 X = 0,223 Y = 0,224 Width = 11,225 Height = 11226 }227 });228 Assert.True(ScreenshotHelper.PixelMatch("screenshot-clip-odd-size.png", screenshot));229 }230 }231 }232}...
RequestRespondTests.cs
Source:RequestRespondTests.cs
...106 document.body.appendChild(img);107 return new Promise(fulfill => img.onload = fulfill);108 }", TestConstants.ServerUrl);109 var img = await Page.QuerySelectorAsync("img");110 Assert.True(ScreenshotHelper.PixelMatch("mock-binary-response.png", await img.ScreenshotDataAsync()));111 }112 [PuppeteerTest("requestinterception.spec.ts", "Request.respond", "should stringify intercepted request response headers")]113 [SkipBrowserFact(skipFirefox: true)]114 public async Task ShouldStringifyInterceptedRequestResponseHeaders()115 {116 await Page.SetRequestInterceptionAsync(true);117 Page.Request += async (_, e) =>118 {119 await e.Request.RespondAsync(new ResponseData120 {121 Status = HttpStatusCode.OK,122 Headers = new Dictionary<string, object>123 {124 ["foo"] = true...
ElementHandleScreenshotTests.cs
Source:ElementHandleScreenshotTests.cs
...26 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}...
PageEmulateVisionDeficiencyTests.cs
Source:PageEmulateVisionDeficiencyTests.cs
...18 await Page.SetViewportAsync(new ViewPortOptions { Width = 500, Height = 500 });19 await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");20 await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.None);21 var screenshot = await Page.ScreenshotDataAsync();22 Assert.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", screenshot));23 await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.Achromatopsia);24 screenshot = await Page.ScreenshotDataAsync();25 Assert.True(ScreenshotHelper.PixelMatch("vision-deficiency-achromatopsia.png", screenshot));26 await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.BlurredVision);27 screenshot = await Page.ScreenshotDataAsync();28 Assert.True(ScreenshotHelper.PixelMatch("vision-deficiency-blurredVision.png", screenshot));29 await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.Deuteranopia);30 screenshot = await Page.ScreenshotDataAsync();31 Assert.True(ScreenshotHelper.PixelMatch("vision-deficiency-deuteranopia.png", screenshot));32 await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.Protanopia);33 screenshot = await Page.ScreenshotDataAsync();34 Assert.True(ScreenshotHelper.PixelMatch("vision-deficiency-protanopia.png", screenshot));35 await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.Tritanopia);36 screenshot = await Page.ScreenshotDataAsync();37 Assert.True(ScreenshotHelper.PixelMatch("vision-deficiency-tritanopia.png", screenshot));38 await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.None);39 screenshot = await Page.ScreenshotDataAsync();40 Assert.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", screenshot));41 }42 }43}...
RespondTests.cs
Source:RespondTests.cs
...57 document.body.appendChild(img);58 return new Promise(fulfill => img.onload = fulfill);59 }", TestConstants.ServerUrl);60 var img = await Page.QuerySelectorAsync("img");61 Assert.True(ScreenshotHelper.PixelMatch("mock-binary-response.png", await img.ScreenshotDataAsync()));62 }63 }64}...
ScreenshotHelper.cs
Source:ScreenshotHelper.cs
...3using SixLabors.ImageSharp;4using SixLabors.ImageSharp.PixelFormats;5namespace PuppeteerSharp.Tests6{7 public class ScreenshotHelper8 {9 public static bool PixelMatch(string screenShotFile, string fileName)10 => PixelMatch(screenShotFile, File.ReadAllBytes(fileName));11 public static bool PixelMatch(string screenShotFile, byte[] screenshot)12 {13 const int pixelThreshold = 10;14 const decimal totalTolerance = 0.05m;15 var baseImage = Image.Load(Path.Combine(TestUtils.FindParentDirectory("Screenshots"), screenShotFile));16 var compareImage = Image.Load(screenshot);17 //Just for debugging purpose18 compareImage.Save(Path.Combine(TestUtils.FindParentDirectory("Screenshots"), "test.png"));19 if (baseImage.Width != compareImage.Width || baseImage.Height != compareImage.Height)20 {21 return false;...
Issue0764.cs
Source:Issue0764.cs
...23 };24 await Task.WhenAll(25 Page.GoToAsync(TestConstants.ServerUrl + "/grid.html"),26 tcs.Task);27 Assert.True(ScreenshotHelper.PixelMatch("0.png", await tcs.Task));28 }29 }30}...
ScreenshotHelper
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4using PuppeteerSharp.Tests;5using PuppeteerSharp.Helpers;6{7 {8 static async Task Main(string[] args)9 {10 var browser = await Puppeteer.LaunchAsync(new LaunchOptions11 {12 });13 var page = await browser.NewPageAsync();14 await page.ScreenshotAsync("google.png");15 await ScreenshotHelper.ScreenshotElementAsync(page, "#hplogo", "googlelogo.png");16 await browser.CloseAsync();17 }18 }19}20using System;21using System.Threading.Tasks;22using PuppeteerSharp;23using PuppeteerSharp.Helpers;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.ScreenshotAsync("google.png");33 await ScreenshotHelper.ScreenshotElementAsync(page, "#hplogo", "googlelogo.png");34 await browser.CloseAsync();35 }36 }37}38using System;39using System.Threading.Tasks;40using PuppeteerSharp;41{42 {43 static async Task Main(string[] args)44 {45 var browser = await Puppeteer.LaunchAsync(new LaunchOptions46 {47 });48 var page = await browser.NewPageAsync();49 await page.ScreenshotAsync("google.png");50 await browser.CloseAsync();51 }52 }53}54using System;55using System.Threading.Tasks;56using PuppeteerSharp;57using PuppeteerSharp.Tests;58{59 {60 static async Task Main(string[] args)61 {62 var browser = await Puppeteer.LaunchAsync(new LaunchOptions63 {
ScreenshotHelper
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });10 var page = await browser.NewPageAsync();11 await page.ScreenshotAsync("google.png");12 await browser.CloseAsync();13 }14 }15}16using PuppeteerSharp;17using System;18using System.Threading.Tasks;19{20 {21 static async Task Main(string[] args)22 {23 Console.WriteLine("Hello World!");24 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });25 var page = await browser.NewPageAsync();26 await page.ScreenshotAsync("google.png");27 await browser.CloseAsync();28 }29 }30}31using PuppeteerSharp;32using System;33using System.Threading.Tasks;34{35 {36 static async Task Main(string[] args)37 {38 Console.WriteLine("Hello World!");39 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });40 var page = await browser.NewPageAsync();41 await page.ScreenshotAsync("google.png");42 await browser.CloseAsync();43 }44 }45}46Error CS0234 The type or namespace name 'PuppeteerSharp' does not exist in the namespace 'PuppeteerSharp.Tests' (are you missing an assembly reference?) 2.cs
ScreenshotHelper
Using AI Code Generation
1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5using PuppeteerSharp.Tests;6using System.Drawing.Imaging;7using System.Drawing;8using System.Linq;9{10 {11 static async Task Main(string[] args)12 {13 Console.WriteLine("Hello World!");14 {15 Args = new string[] { "--no-sandbox", "--disable-setuid-sandbox" }16 };17 using (var browser = await Puppeteer.LaunchAsync(options))18 {19 using (var page = await browser.NewPageAsync())20 {21 await page.ScreenshotAsync("screenshot.png", new ScreenshotOptions22 {23 });24 }25 }26 }27 }28}
ScreenshotHelper
Using AI Code Generation
1using PuppeteerSharp.Tests;2var screenshotHelper = new ScreenshotHelper();3await screenshotHelper.InitAsync();4await screenshotHelper.TakeScreenshotAsync("google.com", "google.png", new ViewPortOptions { Width = 1920, Height = 1080 });5await screenshotHelper.CloseAsync();6using PuppeteerSharp.Tests;7var screenshotHelper = new ScreenshotHelper();8await screenshotHelper.InitAsync();9await screenshotHelper.TakeScreenshotAsync("google.com", "google.png", new ViewPortOptions { Width = 1920, Height = 1080 });10await screenshotHelper.CloseAsync();
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!!