Skip to content

Commit 8562870

Browse files
kblokMeir017
authored andcommitted
Set JPG background to white when omitBackground option is used (#700)
1 parent 972cc61 commit 8562870

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

lib/PuppeteerSharp.Tests/PageTests/ScreenshotTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ await page.SetViewportAsync(new ViewPortOptions
236236
}
237237
}
238238

239+
[Fact]
240+
public async Task ShouldRenderWhiteBackgroundOnJpegFile()
241+
{
242+
using (var page = await Context.NewPageAsync())
243+
{
244+
await page.SetViewportAsync(new ViewPortOptions { Width = 100, Height = 100 });
245+
await page.GoToAsync(TestConstants.EmptyPage);
246+
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
247+
{
248+
OmitBackground = true,
249+
Type = ScreenshotType.Jpeg
250+
});
251+
Assert.True(ScreenshotHelper.PixelMatch("white.jpg", screenshot));
252+
}
253+
}
254+
239255
[Fact]
240256
public async Task ShouldWorkWithOddClipSizeOnRetinaDisplays()
241257
{
357 Bytes
Loading

lib/PuppeteerSharp/Page.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,7 @@ private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOpti
17291729
}).ConfigureAwait(false);
17301730
}
17311731

1732-
if (options != null && options.OmitBackground)
1732+
if (options?.OmitBackground == true && type == ScreenshotType.Png)
17331733
{
17341734
await Client.SendAsync("Emulation.setDefaultBackgroundColorOverride", new
17351735
{
@@ -1774,7 +1774,7 @@ private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOpti
17741774

17751775
private Task ResetBackgroundColorAndViewport(ScreenshotOptions options)
17761776
{
1777-
var omitBackgroundTask = options?.OmitBackground == true ?
1777+
var omitBackgroundTask = options?.OmitBackground == true && options.Type == ScreenshotType.Png ?
17781778
Client.SendAsync("Emulation.setDefaultBackgroundColorOverride") : Task.CompletedTask;
17791779
var setViewPortTask = (options?.FullPage == true && Viewport != null) ?
17801780
SetViewportAsync(Viewport) : Task.CompletedTask;

0 commit comments

Comments
 (0)