Skip to content

Commit ff099b8

Browse files
authored
Update screenshot options (#2285)
* Update screenshot options * fix conditions * FromSurface null by default * Skip fromSurface on windows * it's an or
1 parent 0d03855 commit ff099b8

File tree

7 files changed

+145
-43
lines changed

7 files changed

+145
-43
lines changed

lib/PuppeteerSharp.Tests/ScreenshotTests/ElementHandleScreenshotTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,20 @@ public async Task ShouldWorkForAnElementWithAnOffset()
181181
var screenshot = await elementHandle.ScreenshotDataAsync();
182182
Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional-offset.png", screenshot));
183183
}
184+
185+
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should work with a null viewport")]
186+
[Skip(SkipAttribute.Targets.Firefox)]
187+
public async Task ShouldWorkWithANullViewport()
188+
{
189+
var options = TestConstants.DefaultBrowserOptions();
190+
options.DefaultViewport = null;
191+
await using var browser = await Puppeteer.LaunchAsync(options);
192+
var page = await browser.NewPageAsync();
193+
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
194+
await page.EvaluateExpressionAsync("window.scrollBy(50, 100)");
195+
var elementHandle = await page.QuerySelectorAsync(".box:nth-of-type(3)");
196+
var screenshot = await elementHandle.ScreenshotDataAsync();
197+
Assert.Greater(screenshot.Length, 0);
198+
}
184199
}
185200
}

lib/PuppeteerSharp.Tests/ScreenshotTests/PageScreenshotTests.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ await page.SetViewportAsync(new ViewPortOptions
113113
}
114114
}
115115

116+
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should use scale for clip")]
117+
[Skip(SkipAttribute.Targets.Firefox)]
118+
public async Task ShouldUseScaleForClip()
119+
{
120+
await using (var page = await Context.NewPageAsync())
121+
{
122+
await page.SetViewportAsync(new ViewPortOptions
123+
{
124+
Width = 500,
125+
Height = 500
126+
});
127+
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
128+
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
129+
{
130+
Clip = new Clip
131+
{
132+
X = 50,
133+
Y = 100,
134+
Width = 150,
135+
Height = 100,
136+
Scale = 2,
137+
}
138+
});
139+
Assert.True(ScreenshotHelper.PixelMatch("screenshot-clip-rect-scale2.png", screenshot));
140+
}
141+
}
142+
116143
[PuppeteerTimeout]
117144
public async Task ShouldClipScale()
118145
{
@@ -139,6 +166,7 @@ await page.SetViewportAsync(new ViewPortOptions
139166
}
140167
}
141168

169+
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should get screenshot bigger than the viewport")]
142170
[Skip(SkipAttribute.Targets.Firefox)]
143171
public async Task ShouldClipElementsToTheViewport()
144172
{
@@ -302,6 +330,22 @@ public async Task ShouldRenderWhiteBackgroundOnJpegFile()
302330
}
303331
}
304332

333+
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should work with webp")]
334+
[Skip(SkipAttribute.Targets.Firefox)]
335+
public async Task ShouldWorkWithWebp()
336+
{
337+
await using (var page = await Context.NewPageAsync())
338+
{
339+
await page.SetViewportAsync(new ViewPortOptions { Width = 100, Height = 100 });
340+
await page.GoToAsync(TestConstants.EmptyPage);
341+
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
342+
{
343+
Type = ScreenshotType.Webp
344+
});
345+
Assert.Greater(screenshot.Length, 0);
346+
}
347+
}
348+
305349
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should work with odd clip size on Retina displays")]
306350
[PuppeteerTimeout]
307351
public async Task ShouldWorkWithOddClipSizeOnRetinaDisplays()
@@ -341,6 +385,27 @@ await page.SetViewportAsync(new ViewPortOptions
341385
}
342386
}
343387

388+
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should work in \"fromSurface: false\" mode")]
389+
[Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Windows)]
390+
public async Task ShouldWorkInFromSurfacedFalseMode()
391+
{
392+
await using (var page = await Context.NewPageAsync())
393+
{
394+
await page.SetViewportAsync(new ViewPortOptions
395+
{
396+
Width = 500,
397+
Height = 500
398+
});
399+
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
400+
var screenshot = await page.ScreenshotBase64Async(new ScreenshotOptions
401+
{
402+
FromSurface = false,
403+
});
404+
405+
Assert.Greater(screenshot.Length, 0);
406+
}
407+
}
408+
344409
[PuppeteerTimeout]
345410
public void ShouldInferScreenshotTypeFromName()
346411
{
8.27 KB
Loading

lib/PuppeteerSharp/Messaging/PageCaptureScreenshotRequest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ internal class PageCaptureScreenshotRequest
99
public int Quality { get; set; }
1010

1111
public Clip Clip { get; set; }
12+
13+
public bool CaptureBeyondViewport { get; set; }
14+
15+
public bool? FromSurface { get; set; }
1216
}
1317
}

lib/PuppeteerSharp/Page.cs

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,58 +1268,54 @@ private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOpti
12681268
}
12691269

12701270
var clip = options.Clip != null ? ProcessClip(options.Clip) : null;
1271+
var captureBeyondViewport = options.CaptureBeyondViewport;
12711272

12721273
if (!_screenshotBurstModeOn)
12731274
{
1274-
if (options != null && options.FullPage)
1275+
if (options?.FullPage == true)
12751276
{
1276-
var metrics = _screenshotBurstModeOn
1277-
? _burstModeMetrics :
1278-
await Client.SendAsync<PageGetLayoutMetricsResponse>("Page.getLayoutMetrics").ConfigureAwait(false);
1277+
// Overwrite clip for full page at all times.
1278+
clip = null;
12791279

1280-
if (options.BurstMode)
1280+
if (!captureBeyondViewport)
12811281
{
1282-
_burstModeMetrics = metrics;
1283-
}
1284-
1285-
var contentSize = metrics.CssContentSize ?? metrics.ContentSize;
1282+
var metrics = _screenshotBurstModeOn
1283+
? _burstModeMetrics :
1284+
await Client.SendAsync<PageGetLayoutMetricsResponse>("Page.getLayoutMetrics").ConfigureAwait(false);
12861285

1287-
var width = Convert.ToInt32(Math.Ceiling(contentSize.Width));
1288-
var height = Convert.ToInt32(Math.Ceiling(contentSize.Height));
1289-
1290-
// Overwrite clip for full page at all times.
1291-
clip = new Clip
1292-
{
1293-
X = 0,
1294-
Y = 0,
1295-
Width = width,
1296-
Height = height,
1297-
Scale = 1,
1298-
};
1299-
1300-
var isMobile = Viewport?.IsMobile ?? false;
1301-
var deviceScaleFactor = Viewport?.DeviceScaleFactor ?? 1;
1302-
var isLandscape = Viewport?.IsLandscape ?? false;
1303-
var screenOrientation = isLandscape
1304-
? new ScreenOrientation
1286+
if (options.BurstMode)
13051287
{
1306-
Angle = 90,
1307-
Type = ScreenOrientationType.LandscapePrimary,
1288+
_burstModeMetrics = metrics;
13081289
}
1309-
: new ScreenOrientation
1310-
{
1311-
Angle = 0,
1312-
Type = ScreenOrientationType.PortraitPrimary,
1313-
};
13141290

1315-
await Client.SendAsync("Emulation.setDeviceMetricsOverride", new EmulationSetDeviceMetricsOverrideRequest
1316-
{
1317-
Mobile = isMobile,
1318-
Width = width,
1319-
Height = height,
1320-
DeviceScaleFactor = deviceScaleFactor,
1321-
ScreenOrientation = screenOrientation,
1322-
}).ConfigureAwait(false);
1291+
var contentSize = metrics.CssContentSize ?? metrics.ContentSize;
1292+
1293+
var width = Convert.ToInt32(Math.Ceiling(contentSize.Width));
1294+
var height = Convert.ToInt32(Math.Ceiling(contentSize.Height));
1295+
var isMobile = Viewport?.IsMobile ?? false;
1296+
var deviceScaleFactor = Viewport?.DeviceScaleFactor ?? 1;
1297+
var isLandscape = Viewport?.IsLandscape ?? false;
1298+
var screenOrientation = isLandscape
1299+
? new ScreenOrientation
1300+
{
1301+
Angle = 90,
1302+
Type = ScreenOrientationType.LandscapePrimary,
1303+
}
1304+
: new ScreenOrientation
1305+
{
1306+
Angle = 0,
1307+
Type = ScreenOrientationType.PortraitPrimary,
1308+
};
1309+
1310+
await Client.SendAsync("Emulation.setDeviceMetricsOverride", new EmulationSetDeviceMetricsOverrideRequest
1311+
{
1312+
Mobile = isMobile,
1313+
Width = width,
1314+
Height = height,
1315+
DeviceScaleFactor = deviceScaleFactor,
1316+
ScreenOrientation = screenOrientation,
1317+
}).ConfigureAwait(false);
1318+
}
13231319
}
13241320

13251321
if (options?.OmitBackground == true && type == ScreenshotType.Png)
@@ -1328,9 +1324,16 @@ private async Task<string> PerformScreenshot(ScreenshotType type, ScreenshotOpti
13281324
}
13291325
}
13301326

1327+
if (options?.FullPage == false && clip == null)
1328+
{
1329+
captureBeyondViewport = false;
1330+
}
1331+
13311332
var screenMessage = new PageCaptureScreenshotRequest
13321333
{
13331334
Format = type.ToString().ToLower(CultureInfo.CurrentCulture),
1335+
CaptureBeyondViewport = captureBeyondViewport,
1336+
FromSurface = options.FromSurface,
13341337
};
13351338

13361339
if (options.Quality.HasValue)

lib/PuppeteerSharp/ScreenshotOptions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class ScreenshotOptions
3838
public bool OmitBackground { get; set; }
3939

4040
/// <summary>
41-
/// Specify screenshot type, can be either jpeg or png. Defaults to 'png'.
41+
/// Specify screenshot type, can be either jpeg, png or webp. Defaults to 'png'.
4242
/// </summary>
4343
/// <value>The type.</value>
4444
public ScreenshotType? Type { get; set; }
@@ -70,6 +70,16 @@ public class ScreenshotOptions
7070
[JsonIgnore]
7171
public bool BurstMode { get; set; } = false;
7272

73+
/// <summary>
74+
/// Capture the screenshot beyond the viewport.
75+
/// </summary>
76+
public bool CaptureBeyondViewport { get; set; } = true;
77+
78+
/// <summary>
79+
/// Capture the screenshot from the surface, rather than the view. Defaults to <c>true</c>.
80+
/// </summary>
81+
public bool? FromSurface { get; set; }
82+
7383
internal static ScreenshotType? GetScreenshotTypeFromFile(string file)
7484
{
7585
var extension = new FileInfo(file).Extension.Replace(".", string.Empty);

lib/PuppeteerSharp/ScreenshotType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@ public enum ScreenshotType
1515
/// PNG type.
1616
/// </summary>
1717
Png,
18+
19+
/// <summary>
20+
/// Webp type.
21+
/// </summary>
22+
Webp,
1823
}
1924
}

0 commit comments

Comments
 (0)