Skip to content

Commit 60317e0

Browse files
authored
Implement IDisposable on Browser and Page (#86)
1 parent ec46abd commit 60317e0

File tree

6 files changed

+326
-247
lines changed

6 files changed

+326
-247
lines changed

lib/PuppeteerSharp.Tests/Page/PdfTests.cs

Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,126 +21,139 @@ public async Task ShouldBeAbleToSaveFile()
2121
fileInfo.Delete();
2222
}
2323

24-
var page = await Browser.NewPageAsync();
25-
await page.PdfAsync(outputFile);
24+
using (var page = await Browser.NewPageAsync())
25+
{
26+
await page.PdfAsync(outputFile);
2627

27-
fileInfo = new FileInfo(outputFile);
28-
Assert.True(new FileInfo(outputFile).Length > 0);
28+
fileInfo = new FileInfo(outputFile);
29+
Assert.True(new FileInfo(outputFile).Length > 0);
2930

30-
if (fileInfo.Exists)
31-
{
32-
fileInfo.Delete();
31+
if (fileInfo.Exists)
32+
{
33+
fileInfo.Delete();
34+
}
3335
}
3436
}
3537

3638
[Fact]
3739
public async Task ShouldDefaultToPrintInLetterFormat()
3840
{
39-
var page = await Browser.NewPageAsync();
40-
41-
var document = PdfReader.Open(await page.PdfStreamAsync(), PdfDocumentOpenMode.ReadOnly);
41+
using (var page = await Browser.NewPageAsync())
42+
{
43+
var document = PdfReader.Open(await page.PdfStreamAsync(), PdfDocumentOpenMode.ReadOnly);
4244

43-
Assert.Equal(1, document.Pages.Count);
44-
Assert.Equal(8.5, TruncateDouble(document.Pages[0].Width.Inch, 1));
45-
Assert.Equal(11, TruncateDouble(document.Pages[0].Height.Inch, 0));
45+
Assert.Equal(1, document.Pages.Count);
46+
Assert.Equal(8.5, TruncateDouble(document.Pages[0].Width.Inch, 1));
47+
Assert.Equal(11, TruncateDouble(document.Pages[0].Height.Inch, 0));
48+
}
4649
}
4750

4851
[Fact]
4952
public async Task ShouldSupportSettingCustomFormat()
5053
{
51-
var page = await Browser.NewPageAsync();
52-
53-
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
54+
using (var page = await Browser.NewPageAsync())
5455
{
55-
Format = "a4"
56-
}), PdfDocumentOpenMode.ReadOnly);
56+
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
57+
{
58+
Format = "a4"
59+
}), PdfDocumentOpenMode.ReadOnly);
5760

58-
Assert.Equal(1, document.Pages.Count);
59-
Assert.Equal(8.2, TruncateDouble(document.Pages[0].Width.Inch, 1));
60-
Assert.Equal(842, document.Pages[0].Height.Point);
61+
Assert.Equal(1, document.Pages.Count);
62+
Assert.Equal(8.2, TruncateDouble(document.Pages[0].Width.Inch, 1));
63+
Assert.Equal(842, document.Pages[0].Height.Point);
64+
}
6165
}
6266

6367
[Fact]
6468
public async Task ShouldSupportSettingPaperWidthAndHeight()
6569
{
66-
var page = await Browser.NewPageAsync();
67-
68-
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
70+
using (var page = await Browser.NewPageAsync())
6971
{
70-
Width = "10in",
71-
Height = "10in"
72-
}), PdfDocumentOpenMode.ReadOnly);
72+
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
73+
{
74+
Width = "10in",
75+
Height = "10in"
76+
}), PdfDocumentOpenMode.ReadOnly);
7377

74-
Assert.Equal(1, document.Pages.Count);
75-
Assert.Equal(10, TruncateDouble(document.Pages[0].Width.Inch, 0));
76-
Assert.Equal(10, TruncateDouble(document.Pages[0].Height.Inch, 0));
78+
Assert.Equal(1, document.Pages.Count);
79+
Assert.Equal(10, TruncateDouble(document.Pages[0].Width.Inch, 0));
80+
Assert.Equal(10, TruncateDouble(document.Pages[0].Height.Inch, 0));
81+
}
7782
}
7883

7984
[Fact]
8085
public async Task ShouldPrintMultiplePages()
8186
{
82-
var page = await Browser.NewPageAsync();
83-
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
84-
// Define width and height in CSS pixels.
85-
var width = 50 * 5 + 1;
86-
var height = 50 * 5 + 1;
87-
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
87+
using (var page = await Browser.NewPageAsync())
8888
{
89-
Width = width,
90-
Height = height
91-
}));
89+
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
90+
// Define width and height in CSS pixels.
91+
var width = 50 * 5 + 1;
92+
var height = 50 * 5 + 1;
93+
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
94+
{
95+
Width = width,
96+
Height = height
97+
}));
9298

93-
Assert.Equal(8, document.Pages.Count);
94-
Assert.Equal(CssPixelsToInches(width), TruncateDouble(document.Pages[0].Width.Inch, 0));
95-
Assert.Equal(CssPixelsToInches(height), TruncateDouble(document.Pages[0].Height.Inch, 0));
99+
Assert.Equal(8, document.Pages.Count);
100+
Assert.Equal(CssPixelsToInches(width), TruncateDouble(document.Pages[0].Width.Inch, 0));
101+
Assert.Equal(CssPixelsToInches(height), TruncateDouble(document.Pages[0].Height.Inch, 0));
102+
}
96103
}
97104

98105
[Fact]
99106
public async Task ShouldSupportPageRanges()
100107
{
101-
var page = await Browser.NewPageAsync();
102-
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
103-
// Define width and height in CSS pixels.
104-
var width = 50 * 5 + 1;
105-
var height = 50 * 5 + 1;
106-
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
108+
using (var page = await Browser.NewPageAsync())
107109
{
108-
Width = width,
109-
Height = height,
110-
PageRanges = "1,4-7"
111-
}));
110+
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
111+
// Define width and height in CSS pixels.
112+
var width = 50 * 5 + 1;
113+
var height = 50 * 5 + 1;
114+
var document = PdfReader.Open(await page.PdfStreamAsync(new PdfOptions
115+
{
116+
Width = width,
117+
Height = height,
118+
PageRanges = "1,4-7"
119+
}));
112120

113-
Assert.Equal(5, document.Pages.Count);
121+
Assert.Equal(5, document.Pages.Count);
122+
}
114123
}
115124

116125
[Fact]
117126
public async Task ShowThrowFormatIsUnknown()
118127
{
119-
var page = await Browser.NewPageAsync();
120-
var exception = await Assert.ThrowsAsync<ArgumentException>(async () =>
128+
using (var page = await Browser.NewPageAsync())
121129
{
122-
await page.PdfStreamAsync(new PdfOptions
130+
var exception = await Assert.ThrowsAsync<ArgumentException>(async () =>
123131
{
124-
Format = "something"
132+
await page.PdfStreamAsync(new PdfOptions
133+
{
134+
Format = "something"
135+
});
125136
});
126-
});
127137

128-
Assert.Equal("Unknown paper format", exception.Message);
138+
Assert.Equal("Unknown paper format", exception.Message);
139+
}
129140
}
130141

131142
[Fact]
132143
public async Task ShouldThrowIfUnitsAreUnknown()
133144
{
134-
var page = await Browser.NewPageAsync();
135-
var exception = await Assert.ThrowsAsync<ArgumentException>(async () =>
145+
using (var page = await Browser.NewPageAsync())
136146
{
137-
await page.PdfStreamAsync(new PdfOptions
147+
var exception = await Assert.ThrowsAsync<ArgumentException>(async () =>
138148
{
139-
Width = "10em"
149+
await page.PdfStreamAsync(new PdfOptions
150+
{
151+
Width = "10em"
152+
});
140153
});
141-
});
142154

143-
Assert.Contains("Failed to parse parameter value", exception.Message);
155+
Assert.Contains("Failed to parse parameter value", exception.Message);
156+
}
144157
}
145158

146159
private double TruncateDouble(double value, int precision)

0 commit comments

Comments
 (0)