-
-
Notifications
You must be signed in to change notification settings - Fork 484
Expand file tree
/
Copy pathPageScreenshotTests.cs
More file actions
548 lines (490 loc) · 21.5 KB
/
PageScreenshotTests.cs
File metadata and controls
548 lines (490 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using NUnit.Framework;
using PuppeteerSharp.Media;
using PuppeteerSharp.Nunit;
namespace PuppeteerSharp.Tests.ScreenshotTests
{
public class PageScreenshotTests : PuppeteerBrowserContextBaseTest
{
[Test, Ignore("previously not marked as a test")]
public async Task ShouldWorkWithFile()
{
var outputFile = Path.Combine(BaseDirectory, "output.png");
var fileInfo = new FileInfo(outputFile);
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
if (fileInfo.Exists)
{
fileInfo.Delete();
}
await page.ScreenshotAsync(outputFile);
fileInfo = new FileInfo(outputFile);
Assert.That(new FileInfo(outputFile).Length, Is.GreaterThan(0));
Assert.That(ScreenshotHelper.PixelMatch("screenshot-sanity.png", outputFile), Is.True);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
}
[Test, Retry(2)]
public async Task Usage()
{
var outputFile = Path.Combine(BaseDirectory, "Usage.png");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
#region screenshotasync_example
var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();
await using var browser = await Puppeteer.LaunchAsync(
new LaunchOptions { Headless = true });
await using var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.ScreenshotAsync(outputFile);
#endregion
Assert.That(File.Exists(outputFile), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should work")]
public async Task ShouldWork()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotDataAsync();
Assert.That(ScreenshotHelper.PixelMatch("screenshot-sanity.png", screenshot), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should clip rect")]
public async Task ShouldClipRect()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
Clip = new Clip
{
X = 50,
Y = 100,
Width = 150,
Height = 100
}
});
Assert.That(ScreenshotHelper.PixelMatch("screenshot-clip-rect.png", screenshot), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Cdp", "should use scale for clip")]
public async Task ShouldUseScaleForClip()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
Clip = new Clip
{
X = 50,
Y = 100,
Width = 150,
Height = 100,
Scale = 2,
}
});
Assert.That(ScreenshotHelper.PixelMatch("screenshot-clip-rect-scale2.png", screenshot), Is.True);
}
[Test, Ignore("previously not marked as a test")]
public async Task ShouldClipScale()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
Clip = new Clip
{
X = 50,
Y = 100,
Width = 150,
Height = 100,
Scale = 2
}
});
Assert.That(ScreenshotHelper.PixelMatch("screenshot-clip-rect-scale.png", screenshot), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should get screenshot bigger than the viewport")]
public async Task ShouldClipElementsToTheViewport()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions { Width = 50, Height = 50 });
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
Clip = new Clip
{
X = 25,
Y = 25,
Width = 100,
Height = 100
}
});
Assert.That(ScreenshotHelper.PixelMatch("screenshot-offscreen-clip.png", screenshot), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should clip clip bigger than the viewport without \"captureBeyondViewport\"")]
public async Task ShouldClipClipBiggerThanTheViewportWithoutCaptureBeyondViewport()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions { Width = 50, Height = 50 });
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
CaptureBeyondViewport = false,
Clip = new Clip
{
X = 25,
Y = 25,
Width = 100,
Height = 100
}
});
Assert.That(ScreenshotHelper.PixelMatch("screenshot-offscreen-clip-2.png", screenshot), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should run in parallel")]
public async Task ShouldRunInParallel()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var tasks = new List<Task<byte[]>>();
for (var i = 0; i < 3; ++i)
{
tasks.Add(page.ScreenshotDataAsync(new ScreenshotOptions
{
Clip = new Clip
{
X = 50 * i,
Y = 0,
Width = 50,
Height = 50
}
}));
}
await Task.WhenAll(tasks);
Assert.That(ScreenshotHelper.PixelMatch("grid-cell-1.png", tasks[0].Result), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should take fullPage screenshots")]
public async Task ShouldTakeFullPageScreenshots()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
FullPage = true
});
Assert.That(ScreenshotHelper.PixelMatch("screenshot-grid-fullpage.png", screenshot), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should take fullPage screenshots without captureBeyondViewport")]
public async Task ShouldTakeFullPageScreenshotsWithoutCaptureBeyondViewport()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
FullPage = true,
CaptureBeyondViewport = false,
});
Assert.That(ScreenshotHelper.PixelMatch("screenshot-grid-fullpage-2.png", screenshot), Is.True);
Assert.That(page.Viewport.Width, Is.EqualTo(500));
Assert.That(page.Viewport.Height, Is.EqualTo(500));
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should take fullPage screenshots when defaultViewport is null")]
public async Task ShouldTakeFullPageScreenshotsWhenDefaultViewportIsNull()
{
var options = TestConstants.DefaultBrowserOptions();
options.DefaultViewport = null;
await using var browser = await Puppeteer.LaunchAsync(options);
await using var page = await browser.NewPageAsync();
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
Assert.That(await page.ScreenshotDataAsync(new ScreenshotOptions { FullPage = true }), Is.Not.Empty);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should restore to original viewport size after taking fullPage screenshots when defaultViewport is null")]
public async Task ShouldRestoreToOriginalViewportSizeAfterTakingFullPageScreenshotsWhenDefaultViewportIsNull()
{
var options = TestConstants.DefaultBrowserOptions();
options.DefaultViewport = null;
await using var browser = await Puppeteer.LaunchAsync(options);
await using var page = await browser.NewPageAsync();
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var originalSize = await page.EvaluateFunctionAsync<ViewPortOptions>(@"() => {
return {width: window.innerWidth, height: window.innerHeight};
}");
await page.GoToAsync(TestConstants.ServerUrl + "/scrollbar.html");
await page.ScreenshotDataAsync(new ScreenshotOptions
{
FullPage = true,
CaptureBeyondViewport = false,
});
var size = await page.EvaluateFunctionAsync<ViewPortOptions>(@"() => {
return {width: window.innerWidth, height: window.innerHeight};
}");
Assert.That(page.Viewport, Is.Null);
Assert.That(size.Width, Is.EqualTo(originalSize.Width));
Assert.That(size.Height, Is.EqualTo(originalSize.Height));
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should run in parallel in multiple pages")]
public async Task ShouldRunInParallelInMultiplePages()
{
const int n = 2;
var pageTasks = new List<Task<IPage>>();
for (var i = 0; i < n; i++)
{
pageTasks.Add(Func());
continue;
async Task<IPage> Func()
{
var page = await Context.NewPageAsync();
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
return page;
}
}
await Task.WhenAll(pageTasks);
var screenshotTasks = new List<Task<byte[]>>();
for (var i = 0; i < n; i++)
{
screenshotTasks.Add(pageTasks[i].Result.ScreenshotDataAsync(new ScreenshotOptions
{
Clip = new Clip
{
X = 50 * i,
Y = 0,
Width = 50,
Height = 50
}
}));
}
await Task.WhenAll(screenshotTasks);
for (var i = 0; i < n; i++)
{
Assert.That(ScreenshotHelper.PixelMatch($"grid-cell-{i}.png", screenshotTasks[i].Result), Is.True);
}
var closeTasks = new List<Task>();
for (var i = 0; i < n; i++)
{
closeTasks.Add(pageTasks[i].Result.CloseAsync());
}
await Task.WhenAll(closeTasks);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should run in parallel in multiple pages")]
public async Task ShouldRunInParallelInMultiplePagesElementHandle()
{
await using var context = await Browser.CreateBrowserContextAsync();
const int n = 2;
var pageTasks = new List<Task<IPage>>();
for (var i = 0; i < n; i++)
{
pageTasks.Add(Func());
continue;
async Task<IPage> Func()
{
var page = await context.NewPageAsync();
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
return page;
}
}
await Task.WhenAll(pageTasks);
var screenshotTasks = new List<Task<byte[]>>();
for (var i = 0; i < n; i++)
{
screenshotTasks.Add(pageTasks[i].Result.ScreenshotDataAsync(new ScreenshotOptions
{
Clip = new Clip
{
X = 50 * i,
Y = 0,
Width = 50,
Height = 50
}
}));
}
await Task.WhenAll(screenshotTasks);
for (var i = 0; i < n; i++)
{
Assert.That(ScreenshotHelper.PixelMatch($"grid-cell-{i}.png", screenshotTasks[i].Result), Is.True);
}
var closeTasks = new List<Task>();
for (var i = 0; i < n; i++)
{
closeTasks.Add(pageTasks[i].Result.CloseAsync());
}
await Task.WhenAll(closeTasks);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should run in parallel with page.close()")]
public async Task ShouldRunInParallelWithPageClose()
{
await using var context = await Browser.CreateBrowserContextAsync();
var page1 = await context.NewPageAsync();
var page2 = await context.NewPageAsync();
var screen1 = page1.ScreenshotDataAsync();
var screen2 = page2.ScreenshotDataAsync();
var close1 = screen1.ContinueWith(_ => page1.CloseAsync()).Unwrap();
var close2 = screen2.ContinueWith(_ => page2.CloseAsync()).Unwrap();
await screen1;
var page3 = await Browser.NewPageAsync();
await page3.ScreenshotDataAsync();
var close3 = page3.CloseAsync();
await Task.WhenAll(close1, close2, close3);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Cdp", "should allow transparency")]
public async Task ShouldAllowTransparency()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 100,
Height = 100
});
await page.GoToAsync(TestConstants.EmptyPage);
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
OmitBackground = true
});
Assert.That(ScreenshotHelper.PixelMatch("transparent.png", screenshot), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Cdp", "should render white background on jpeg file")]
public async Task ShouldRenderWhiteBackgroundOnJpegFile()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions { Width = 100, Height = 100 });
await page.GoToAsync(TestConstants.EmptyPage);
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
OmitBackground = true,
Type = ScreenshotType.Jpeg
});
Assert.That(ScreenshotHelper.PixelMatch("white.jpg", screenshot), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should work with webp")]
public async Task ShouldWorkWithWebp()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions { Width = 100, Height = 100 });
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
Type = ScreenshotType.Webp
});
Assert.That(screenshot, Is.Not.Empty);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should work with odd clip size on Retina displays")]
public async Task ShouldWorkWithOddClipSizeOnRetinaDisplays()
{
await using var page = await Context.NewPageAsync();
// Make sure documentElement height is at least 11px.
await page.SetContentAsync("<div style=\"width: 11px; height: 11px;\"></div>");
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
Clip = new Clip
{
X = 0,
Y = 0,
Width = 11,
Height = 11
}
});
Assert.That(ScreenshotHelper.PixelMatch("screenshot-clip-odd-size.png", screenshot), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Page.screenshot", "should return base64")]
public async Task ShouldReturnBase64()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotBase64Async();
Assert.That(ScreenshotHelper.PixelMatch("screenshot-sanity.png", Convert.FromBase64String(screenshot)), Is.True);
}
[Test, PuppeteerTest("screenshot.spec", "Screenshots Cdp", "should work in \"fromSurface: false\" mode")]
public async Task ShouldWorkInFromSurfacedFalseMode()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotBase64Async(new ScreenshotOptions
{
FromSurface = false,
});
Assert.That(screenshot, Is.Not.Empty);
}
[Test, Ignore("previously not marked as a test")]
public void ShouldInferScreenshotTypeFromName()
{
Assert.That(ScreenshotOptions.GetScreenshotTypeFromFile("Test.jpg"), Is.EqualTo(ScreenshotType.Jpeg));
Assert.That(ScreenshotOptions.GetScreenshotTypeFromFile("Test.jpe"), Is.EqualTo(ScreenshotType.Jpeg));
Assert.That(ScreenshotOptions.GetScreenshotTypeFromFile("Test.jpeg"), Is.EqualTo(ScreenshotType.Jpeg));
Assert.That(ScreenshotOptions.GetScreenshotTypeFromFile("Test.png"), Is.EqualTo(ScreenshotType.Png));
Assert.That(ScreenshotOptions.GetScreenshotTypeFromFile("Test.exe"), Is.Null);
}
[Test, Ignore("previously not marked as a test")]
public async Task ShouldWorkWithQuality()
{
await using var page = await Context.NewPageAsync();
await page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
Type = ScreenshotType.Jpeg,
FullPage = true,
Quality = 100
});
Assert.That(ScreenshotHelper.PixelMatch("screenshot-grid-fullpage.png", screenshot), Is.True);
}
}
}