forked from hardkoded/puppeteer-sharp
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ElementHandleScreenshotTests.cs
198 lines (184 loc) · 8.16 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
189
190
191
192
193
194
195
196
197
198
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using CefSharp.Dom;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;
using CefSharp;
namespace PuppeteerSharp.Tests.ScreenshotTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class ElementHandleScreenshotTests : DevToolsContextBaseTest
{
public ElementHandleScreenshotTests(ITestOutputHelper output) : base(output)
{
}
#pragma warning disable IDE0051 // Remove unused private members
async Task Usage(IWebBrowser chromiumWebBrowser)
#pragma warning restore IDE0051 // Remove unused private members
{
#region Screenshot
//Wait for Initial page load
await chromiumWebBrowser.WaitForInitialLoadAsync();
await using var devToolsContext = await chromiumWebBrowser.CreateDevToolsContextAsync();
await devToolsContext.ScreenshotAsync("file.png");
#endregion
}
[PuppeteerTest("screenshot.spec.ts", "ElementHandle.screenshot", "should work")]
[PuppeteerFact]
public async Task ShouldWork()
{
#region SetViewportAsync
// Set Viewport
await DevToolsContext.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
#endregion
await DevToolsContext.GoToAsync(TestConstants.ServerUrl + "/grid.html");
await DevToolsContext.EvaluateExpressionAsync("window.scrollBy(50, 100)");
var elementHandle = await DevToolsContext.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 DevToolsContext.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await DevToolsContext.SetContentAsync(@"
something above
<style> div {
border: 2px solid blue;
background: green;
width: 50px;
height: 50px;
}
</style>
<div></div>
");
var elementHandle = await DevToolsContext.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")]
[PuppeteerFact(Skip = "TODO: CEF")]
public async Task ShouldCaptureFullElementWhenLargerThanViewport()
{
await DevToolsContext.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await DevToolsContext.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 DevToolsContext.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 DevToolsContext.EvaluateExpressionAsync("({ w: window.innerWidth, h: window.innerHeight })"));
}
[PuppeteerFact]
public async Task ShouldScrollElementIntoView()
{
await DevToolsContext.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await DevToolsContext.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 DevToolsContext.QuerySelectorAsync("div.to-screenshot");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-scrolled-into-view.png", screenshot));
}
[PuppeteerFact]
public async Task ShouldWorkWithARotatedElement()
{
await DevToolsContext.SetViewportAsync(new ViewPortOptions
{
Width = 500,
Height = 500
});
await DevToolsContext.SetContentAsync(@"
<div style='position: absolute;
top: 100px;
left: 100px;
width: 100px;
height: 100px;
background: green;
transform: rotateZ(200deg); '> </div>
");
var elementHandle = await DevToolsContext.QuerySelectorAsync("div");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-rotate.png", screenshot));
}
[PuppeteerFact]
public async Task ShouldFailToScreenshotADetachedElement()
{
await DevToolsContext.SetContentAsync("<h1>remove this</h1>");
var elementHandle = await DevToolsContext.QuerySelectorAsync("h1");
await DevToolsContext.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);
}
[PuppeteerFact]
public async Task ShouldNotHangWithZeroWidthHeightElement()
{
await DevToolsContext.SetContentAsync(@"<div style='width: 50px; height: 0'></div>");
var elementHandle = await DevToolsContext.QuerySelectorAsync("div");
var exception = await Assert.ThrowsAsync<PuppeteerException>(elementHandle.ScreenshotDataAsync);
Assert.Equal("Node has 0 height.", exception.Message);
}
[PuppeteerFact]
public async Task ShouldWorkForAnElementWithFractionalDimensions()
{
await DevToolsContext.SetContentAsync("<div style=\"width:48.51px;height:19.8px;border:1px solid black;\"></div>");
var elementHandle = await DevToolsContext.QuerySelectorAsync("div");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional.png", screenshot));
}
[PuppeteerFact]
public async Task ShouldWorkForAnElementWithAnOffset()
{
await DevToolsContext.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 DevToolsContext.QuerySelectorAsync("div");
var screenshot = await elementHandle.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("screenshot-element-fractional-offset.png", screenshot));
}
}
}