-
-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathPdfTests.cs
More file actions
213 lines (187 loc) · 8.08 KB
/
Copy pathPdfTests.cs
File metadata and controls
213 lines (187 loc) · 8.08 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
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using NUnit.Framework;
using PuppeteerSharp.Cdp;
using PuppeteerSharp.Media;
using PuppeteerSharp.Nunit;
namespace PuppeteerSharp.Tests.PageTests
{
public class PdfTests : PuppeteerPageBaseTest
{
[Test]
public async Task Usage()
{
var outputFile = Path.Combine(BaseDirectory, "Usage.pdf");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
#region pdfasync_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"); // In case of fonts being loaded from a CDN, use WaitUntilNavigation.Networkidle0 as a second param.
await page.EvaluateExpressionHandleAsync("document.fonts.ready"); // Wait for fonts to be loaded. Omitting this might result in no text rendered in pdf.
await page.PdfAsync(outputFile);
#endregion
Assert.That(File.Exists(outputFile), Is.True);
}
[Test, PuppeteerTest("pdf.spec", "Page.pdf", "can print to PDF and save to file")]
public async Task ShouldBeAbleToSaveFile()
{
var outputFile = Path.Combine(BaseDirectory, "output.pdf");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
await Page.PdfAsync(outputFile);
fileInfo = new FileInfo(outputFile);
Assert.That(new FileInfo(outputFile).Length, Is.GreaterThan(0));
if (fileInfo.Exists)
{
fileInfo.Delete();
}
}
[Test, PuppeteerTest("pdf.spec", "Page.pdf", "can print to PDF and stream the result")]
public async Task CanPrintToPDFAndStreamTheResult()
{
// We test this differently compared to puppeteer.
// We will compare that we can get to the same file using both PDF methods
var outputFile = Path.Combine(BaseDirectory, "output.pdf");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
await Page.PdfAsync(outputFile);
var stream = await Page.PdfStreamAsync();
// Firefox in Linux might generate and of by one result here.
// If the difference is less than 2 bytes is good
Assert.That(Math.Abs(new FileInfo(outputFile).Length - stream.Length), Is.LessThan(2));
}
[Test, PuppeteerTest("pdf.spec", "Page.pdf", "can print to PDF with accessible")]
public async Task CanPrintToPdfWithAccessible()
{
// We test this differently compared to puppeteer.
// We will compare that we can get to the same file using both PDF methods
var outputFile = Path.Combine(BaseDirectory, "output.pdf");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
var accessibleOutputFile = Path.Combine(BaseDirectory, "output-accessible.pdf");
fileInfo = new FileInfo(accessibleOutputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
await Page.GoToAsync(TestConstants.ServerUrl + "/pdf.html");
await Page.PdfAsync(outputFile, new PdfOptions { Tagged = false });
await Page.PdfAsync(accessibleOutputFile, new PdfOptions { Tagged = true });
Assert.That(new FileInfo(accessibleOutputFile).Length, Is.GreaterThan(new FileInfo(outputFile).Length));
}
[Test, PuppeteerTest("pdf.spec", "Page.pdf", "can print to PDF with outline")]
public async Task CanPrintToPdfWithOutline()
{
var outputFile = Path.Combine(BaseDirectory, "output.pdf");
var outputFileOutlined = Path.Combine(BaseDirectory, "output-outlined.pdf");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
fileInfo = new FileInfo(outputFileOutlined);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
await Page.GoToAsync(TestConstants.ServerUrl + "/pdf.html");
await Page.PdfAsync(outputFile, new PdfOptions { Tagged = false });
await Page.PdfAsync(outputFileOutlined, new PdfOptions { Tagged = true });
Assert.That(new FileInfo(outputFileOutlined).Length, Is.GreaterThan(new FileInfo(outputFile).Length));
}
[Test]
public void PdfOptionsShouldBeSerializable()
{
var pdfOptions = new PdfOptions
{
Width = 100,
Height = 100,
Format = PaperFormat.A4,
DisplayHeaderFooter = true,
MarginOptions = new MarginOptions
{
Top = "20px",
Right = "20px",
Bottom = "40px",
Left = "20px"
},
FooterTemplate = "<div id=\"footer-template\" style=\"font-size:10px !important; color:#808080; padding-left:10px\">- <span class=\"pageNumber\"></span> - </div>"
};
var serialized = JsonSerializer.Serialize(pdfOptions);
var newPdfOptions = JsonSerializer.Deserialize<PdfOptions>(serialized);
Assert.That(newPdfOptions, Is.EqualTo(pdfOptions));
}
[Test, PuppeteerTest("page.spec", "Page.pdf", "can print to PDF and save to file")]
public async Task CanPrintToPdfAndSaveToFile()
{
var outputFile = Path.Combine(BaseDirectory, "output.pdf");
var fileInfo = new FileInfo(outputFile);
if (fileInfo.Exists)
{
fileInfo.Delete();
}
await Page.GoToAsync(TestConstants.ServerUrl + "/pdf.html");
await Page.PdfAsync(outputFile);
try
{
Assert.That(new FileInfo(outputFile).Length, Is.GreaterThan(0));
}
finally
{
if (File.Exists(outputFile))
{
File.Delete(outputFile);
}
}
}
[Test, PuppeteerTest("page.spec", "Page.pdf", "can print to PDF and stream the result")]
public async Task CanPrintToPdfAndStreamTheResult()
{
var stream = await Page.PdfStreamAsync();
var size = 0L;
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
size += bytesRead;
}
Assert.That(size, Is.GreaterThan(0));
}
[Test, PuppeteerTest("page.spec", "Page.pdf", "should respect timeout")]
public async Task ShouldRespectTimeout()
{
await Page.GoToAsync(TestConstants.ServerUrl + "/pdf.html");
var exception = Assert.ThrowsAsync<TimeoutException>(async () =>
{
await Page.PdfDataAsync(new PdfOptions { Timeout = 1 });
});
Assert.That(exception, Is.Not.Null);
}
[Test]
public void ConvertPrintParameterToInchesTests()
{
Assert.That(CdpPage.ConvertPrintParameterToInches("10"), Is.EqualTo(10m / 96));
Assert.That(CdpPage.ConvertPrintParameterToInches("10px"), Is.EqualTo(10m / 96));
Assert.That(CdpPage.ConvertPrintParameterToInches("0"), Is.EqualTo(0));
Assert.That(CdpPage.ConvertPrintParameterToInches("0px"), Is.EqualTo(0));
Assert.That(CdpPage.ConvertPrintParameterToInches("10in"), Is.EqualTo(10));
}
}
}