-
-
Notifications
You must be signed in to change notification settings - Fork 460
/
Copy pathElementHandleScreenshotTests.cs
188 lines (176 loc) · 8.77 KB
/
ElementHandleScreenshotTests.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
using System.Text.Json;
using System.Threading.Tasks;
using NUnit.Framework;
using PuppeteerSharp.Nunit;
namespace PuppeteerSharp.Tests.ScreenshotTests
{
public class ElementHandleScreenshotTests : PuppeteerPageBaseTest
{
[Test, Retry(2), PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should work")]
public async Task ShouldWork()
{
#region setviewportasync_example
await Page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
#endregion
await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
await Page.EvaluateExpressionAsync("window.scrollBy(50, 100)");
var elementHandle = await Page.QuerySelectorAsync(".box:nth-of-type(3)");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.That(ScreenshotHelper.PixelMatch("screenshot-element-bounding-box.png", screenshot), Is.True);
}
[Test, Retry(2), PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should take into account padding and border")]
public async Task ShouldTakeIntoAccountPaddingAndBorder()
{
await Page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await Page.SetContentAsync(@"
something above
<style> div {
border: 2px solid blue;
background: green;
width: 50px;
height: 50px;
}
</style>
<div></div>
");
var elementHandle = await Page.QuerySelectorAsync("div");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.That(ScreenshotHelper.PixelMatch("screenshot-element-padding-border.png", screenshot), Is.True);
}
[Test, Retry(2), PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should capture full element when larger than viewport")]
public async Task ShouldCaptureFullElementWhenLargerThanViewport()
{
await Page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await Page.SetContentAsync(@"
something above
<style>
div.to-screenshot {
border: 1px solid blue;
width: 600px;
height: 600px;
margin-left: 50px;
}
::-webkit-scrollbar{
display: none;
}
</style>
<div class='to-screenshot'></div>"
);
var elementHandle = await Page.QuerySelectorAsync("div.to-screenshot");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.That(ScreenshotHelper.PixelMatch("screenshot-element-larger-than-viewport.png", screenshot), Is.True);
var currentSize =
await Page.EvaluateExpressionAsync<JsonElement>("({ w: window.innerWidth, h: window.innerHeight })");
Assert.That(currentSize.GetProperty("w").GetInt32(), Is.EqualTo(500));
Assert.That(currentSize.GetProperty("h").GetInt32(), Is.EqualTo(500));
}
[Test, Retry(2), PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should scroll element into view")]
public async Task ShouldScrollElementIntoView()
{
await Page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await Page.SetContentAsync(@"
something above
<style> div.above {
border: 2px solid blue;
background: red;
height: 1500px;
}
div.to-screenshot {
border: 2px solid blue;
background: green;
width: 50px;
height: 50px;
}
</style>
<div class='above'></div>
<div class='to-screenshot'></div>
");
var elementHandle = await Page.QuerySelectorAsync("div.to-screenshot");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.That(ScreenshotHelper.PixelMatch("screenshot-element-scrolled-into-view.png", screenshot), Is.True);
}
[Test, Retry(2), PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should work with a rotated element")]
public async Task ShouldWorkWithARotatedElement()
{
await Page.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await Page.SetContentAsync(@"
<div style='position: absolute;
top: 100px;
left: 100px;
width: 100px;
height: 100px;
background: green;
transform: rotateZ(200deg); '> </div>
");
var elementHandle = await Page.QuerySelectorAsync("div");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.That(ScreenshotHelper.PixelMatch("screenshot-element-rotate.png", screenshot), Is.True);
}
[Test, Retry(2), PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should fail to screenshot a detached element")]
public async Task ShouldFailToScreenshotADetachedElement()
{
await Page.SetContentAsync("<h1>remove this</h1>");
var elementHandle = await Page.QuerySelectorAsync("h1");
await Page.EvaluateFunctionAsync("element => element.remove()", elementHandle);
var exception = Assert.ThrowsAsync<PuppeteerException>(elementHandle.ScreenshotStreamAsync);
Assert.That(exception!.Message, Is.EqualTo("Node is either not visible or not an HTMLElement"));
}
[Test, Retry(2), PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should not hang with zero width/height element")]
public async Task ShouldNotHangWithZeroWidthHeightElement()
{
await Page.SetContentAsync(@"<div style='width: 50px; height: 0'></div>");
var elementHandle = await Page.QuerySelectorAsync("div");
var exception = Assert.ThrowsAsync<PuppeteerException>(elementHandle.ScreenshotDataAsync);
Assert.That(exception!.Message, Is.EqualTo("Node has 0 height."));
}
[Test, Retry(2), PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should work for an element with fractional dimensions")]
public async Task ShouldWorkForAnElementWithFractionalDimensions()
{
await Page.SetContentAsync("<div style=\"width:48.51px;height:19.8px;border:1px solid black;\"></div>");
var elementHandle = await Page.QuerySelectorAsync("div");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.That(ScreenshotHelper.PixelMatch("screenshot-element-fractional.png", screenshot), Is.True);
}
[Test, Retry(2), PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should work for an element with an offset")]
public async Task ShouldWorkForAnElementWithAnOffset()
{
await Page.SetContentAsync("<div style=\"position:absolute; top: 10.3px; left: 20.4px;width:50.3px;height:20.2px;border:1px solid black;\"></div>");
var elementHandle = await Page.QuerySelectorAsync("div");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.That(ScreenshotHelper.PixelMatch("screenshot-element-fractional-offset.png", screenshot), Is.True);
}
[Test, Retry(2), PuppeteerTest("screenshot.spec", "Screenshots ElementHandle.screenshot", "should work with a null viewport")]
public async Task ShouldWorkWithANullViewport()
{
var options = TestConstants.DefaultBrowserOptions();
options.DefaultViewport = null;
await using var browser = await Puppeteer.LaunchAsync(options);
var page = await browser.NewPageAsync();
await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
await page.EvaluateExpressionAsync("window.scrollBy(50, 100)");
var elementHandle = await page.QuerySelectorAsync(".box:nth-of-type(3)");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.That(screenshot, Is.Not.Empty);
}
}
}