forked from ghasctl/puppeteer-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageScreenshotTests.cs
377 lines (343 loc) · 13.6 KB
/
PageScreenshotTests.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using PuppeteerSharp.Media;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace PuppeteerSharp.Tests.ScreenshotTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class PageScreenshotTests : PuppeteerBrowserContextBaseTest
{
public PageScreenshotTests(ITestOutputHelper output) : base(output)
{
}
[SkipBrowserFact(skipFirefox: true)]
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.True(new FileInfo(outputFile).Length > 0);
Assert.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", outputFile));
if (fileInfo.Exists)
{
fileInfo.Delete();
}
}
}
[PuppeteerFact(Timeout = -1)]
public async Task Usage()
{
var outputFile = Path.Combine(BaseDirectory, "Usage.png");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
#region ScreenshotAsync
using 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.True(File.Exists(outputFile));
}
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should work")]
[SkipBrowserFact(skipFirefox: true)]
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.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", screenshot));
}
}
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should clip rect")]
[PuppeteerFact]
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.True(ScreenshotHelper.PixelMatch("screenshot-clip-rect.png", screenshot));
}
}
[PuppeteerFact]
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.True(ScreenshotHelper.PixelMatch("screenshot-clip-rect-scale.png", screenshot));
}
}
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldClipElementsToTheViewport()
{
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 = 600,
Width = 100,
Height = 100
}
});
Assert.True(ScreenshotHelper.PixelMatch("screenshot-offscreen-clip.png", screenshot));
}
}
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should run in parallel")]
[PuppeteerFact]
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.True(ScreenshotHelper.PixelMatch("grid-cell-1.png", tasks[0].Result));
}
}
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should take fullPage screenshots")]
[SkipBrowserFact(skipFirefox: true)]
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.True(ScreenshotHelper.PixelMatch("screenshot-grid-fullpage.png", screenshot));
}
}
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should run in parallel in multiple pages")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldRunInParallelInMultiplePages()
{
var n = 2;
var pageTasks = new List<Task<IPage>>();
for (var i = 0; i < n; i++)
{
async Task<IPage> func()
{
var page = await Context.NewPageAsync();
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
return page;
}
pageTasks.Add(func());
}
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.True(ScreenshotHelper.PixelMatch($"grid-cell-{i}.png", screenshotTasks[i].Result));
}
var closeTasks = new List<Task>();
for (var i = 0; i < n; i++)
{
closeTasks.Add(pageTasks[i].Result.CloseAsync());
}
await Task.WhenAll(closeTasks);
}
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should allow transparency")]
[SkipBrowserFact(skipFirefox: true)]
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.True(ScreenshotHelper.PixelMatch("transparent.png", screenshot));
}
}
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should render white background on jpeg file")]
[SkipBrowserFact(skipFirefox: true)]
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.True(ScreenshotHelper.PixelMatch("white.jpg", screenshot));
}
}
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should work with odd clip size on Retina displays")]
[PuppeteerFact]
public async Task ShouldWorkWithOddClipSizeOnRetinaDisplays()
{
await using (var page = await Context.NewPageAsync())
{
var screenshot = await page.ScreenshotDataAsync(new ScreenshotOptions
{
Clip = new Clip
{
X = 0,
Y = 0,
Width = 11,
Height = 11
}
});
Assert.True(ScreenshotHelper.PixelMatch("screenshot-clip-odd-size.png", screenshot));
}
}
[PuppeteerTest("screenshot.spec.ts", "Page.screenshot", "should return base64")]
[SkipBrowserFact(skipFirefox: true)]
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.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", Convert.FromBase64String(screenshot)));
}
}
[PuppeteerFact]
public void ShouldInferScreenshotTypeFromName()
{
Assert.Equal(ScreenshotType.Jpeg, ScreenshotOptions.GetScreenshotTypeFromFile("Test.jpg"));
Assert.Equal(ScreenshotType.Jpeg, ScreenshotOptions.GetScreenshotTypeFromFile("Test.jpe"));
Assert.Equal(ScreenshotType.Jpeg, ScreenshotOptions.GetScreenshotTypeFromFile("Test.jpeg"));
Assert.Equal(ScreenshotType.Png, ScreenshotOptions.GetScreenshotTypeFromFile("Test.png"));
Assert.Null(ScreenshotOptions.GetScreenshotTypeFromFile("Test.exe"));
}
[SkipBrowserFact(skipFirefox: true)]
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.True(ScreenshotHelper.PixelMatch("screenshot-grid-fullpage.png", screenshot));
}
}
}
}