forked from ghasctl/puppeteer-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElementHandleScreenshotTests.cs
187 lines (176 loc) · 8.04 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
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace PuppeteerSharp.Tests.ScreenshotTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class ElementHandleScreenshotTests : PuppeteerPageBaseTest
{
public ElementHandleScreenshotTests(ITestOutputHelper output) : base(output)
{
}
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should work")]
[PuppeteerFact]
public async Task ShouldWork()
{
#region SetViewportAsync
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.True(ScreenshotHelper.PixelMatch("screenshot-element-bounding-box.png", screenshot));
}
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should take into account padding and border")]
[PuppeteerFact]
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.True(ScreenshotHelper.PixelMatch("screenshot-element-padding-border.png", screenshot));
}
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should capture full element when larger than viewport")]
[SkipBrowserFact(skipFirefox: true)]
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.True(ScreenshotHelper.PixelMatch("screenshot-element-larger-than-viewport.png", screenshot));
Assert.Equal(JToken.FromObject(new { w = 500, h = 500 }),
await Page.EvaluateExpressionAsync("({ w: window.innerWidth, h: window.innerHeight })"));
}
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should scroll element into view")]
[PuppeteerFact]
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.True(ScreenshotHelper.PixelMatch("screenshot-element-scrolled-into-view.png", screenshot));
}
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should work with a rotated element")]
[PuppeteerFact]
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.True(ScreenshotHelper.PixelMatch("screenshot-element-rotate.png", screenshot));
}
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should fail to screenshot a detached element")]
[SkipBrowserFact(skipFirefox: true)]
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 = await Assert.ThrowsAsync<PuppeteerException>(elementHandle.ScreenshotStreamAsync);
Assert.Equal("Node is either not visible or not an HTMLElement", exception.Message);
}
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should not hang with zero width/height element")]
[PuppeteerFact]
public async Task ShouldNotHangWithZeroWidthHeightElement()
{
await Page.SetContentAsync(@"<div style='width: 50px; height: 0'></div>");
var elementHandle = await Page.QuerySelectorAsync("div");
var exception = await Assert.ThrowsAsync<PuppeteerException>(elementHandle.ScreenshotDataAsync);
Assert.Equal("Node has 0 height.", exception.Message);
}
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should work for an element with fractional dimensions")]
[PuppeteerFact]
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.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional.png", screenshot));
}
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should work for an element with an offset")]
[SkipBrowserFact(skipFirefox: true)]
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.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional-offset.png", screenshot));
}
}
}