Skip to content

Commit 256b284

Browse files
authoredMay 11, 2018
Implement ElementHandle.BoundingBoxAsync (#213)
1 parent 3e9669b commit 256b284

File tree

3 files changed

+119
-28
lines changed

3 files changed

+119
-28
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Threading.Tasks;
2+
using Xunit;
3+
4+
namespace PuppeteerSharp.Tests.ElementHandleTests
5+
{
6+
[Collection("PuppeteerLoaderFixture collection")]
7+
public class BoundingBoxTests : PuppeteerPageBaseTest
8+
{
9+
[Fact]
10+
public async Task ShouldWork()
11+
{
12+
await Page.SetViewportAsync(new ViewPortOptions
13+
{
14+
Width = 500,
15+
Height = 500
16+
});
17+
await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
18+
var elementHandle = await Page.QuerySelectorAsync(".box:nth-of-type(13)");
19+
var box = await elementHandle.BoundingBoxAsync();
20+
Assert.Equal(new BoundingBox(100, 50, 50, 50), box);
21+
}
22+
23+
[Fact]
24+
public async Task ShouldHandleNestedFrames()
25+
{
26+
await Page.SetViewportAsync(new ViewPortOptions
27+
{
28+
Width = 500,
29+
Height = 500
30+
});
31+
await Page.GoToAsync(TestConstants.ServerUrl + "/frames/nested-frames.html");
32+
var nestedFrame = Page.Frames[1].ChildFrames[1];
33+
var elementHandle = await nestedFrame.QuerySelectorAsync("div");
34+
var box = await elementHandle.BoundingBoxAsync();
35+
Assert.Equal(new BoundingBox(28, 260, 264, 18), box);
36+
}
37+
38+
[Fact]
39+
public async Task ShouldReturnNullForInvisibleElements()
40+
{
41+
await Page.SetContentAsync("<div style='display:none'>hi</div>");
42+
var elementHandle = await Page.QuerySelectorAsync("div");
43+
Assert.Null(await elementHandle.BoundingBoxAsync());
44+
}
45+
}
46+
}

‎lib/PuppeteerSharp/BoundingBox.cs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
namespace PuppeteerSharp
3+
{
4+
public class BoundingBox
5+
{
6+
/// <summary>
7+
/// The x coordinate of the element in pixels.
8+
/// </summary>
9+
/// <value>The x.</value>
10+
public decimal X { get; set; }
11+
/// <summary>
12+
/// The y coordinate of the element in pixels.
13+
/// </summary>
14+
/// <value>The y.</value>
15+
public decimal Y { get; set; }
16+
/// <summary>
17+
/// The width of the element in pixels.
18+
/// </summary>
19+
/// <value>The width.</value>
20+
public decimal Width { get; set; }
21+
/// <summary>
22+
/// The height of the element in pixels.
23+
/// </summary>
24+
/// <value>The height.</value>
25+
public decimal Height { get; set; }
26+
27+
public BoundingBox(decimal x, decimal y, decimal width, decimal height)
28+
{
29+
X = x;
30+
Y = y;
31+
Width = width;
32+
Height = height;
33+
}
34+
35+
internal Clip ToClip()
36+
{
37+
return new Clip
38+
{
39+
X = X,
40+
Y = Y,
41+
Width = Width,
42+
Height = Height
43+
};
44+
}
45+
46+
public override bool Equals(object obj)
47+
{
48+
if (obj == null && GetType() != obj.GetType())
49+
{
50+
return false;
51+
}
52+
53+
var boundingBox = obj as BoundingBox;
54+
55+
return boundingBox.X == X &&
56+
boundingBox.Y == Y &&
57+
boundingBox.Height == Height &&
58+
boundingBox.Width == Width;
59+
}
60+
61+
public override int GetHashCode()
62+
=> X.GetHashCode() * 397
63+
^ Y.GetHashCode() * 397
64+
^ Width.GetHashCode() * 397
65+
^ Height.GetHashCode() * 397;
66+
}
67+
}

‎lib/PuppeteerSharp/ElementHandle.cs

+6-28
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,12 @@ private async Task ScrollIntoViewIfNeededAsync()
271271
}
272272
}
273273

274-
private async Task<BoundingBox> BoundingBoxAsync()
274+
/// <summary>
275+
/// This method returns the bounding box of the element (relative to the main frame),
276+
/// or null if the element is not visible.
277+
/// </summary>
278+
/// <returns>The BoundingBox task.</returns>
279+
public async Task<BoundingBox> BoundingBoxAsync()
275280
{
276281
dynamic result = null;
277282

@@ -301,32 +306,5 @@ private async Task<BoundingBox> BoundingBoxAsync()
301306

302307
return new BoundingBox(x, y, width, height);
303308
}
304-
305-
private class BoundingBox
306-
{
307-
public decimal X { get; set; }
308-
public decimal Y { get; set; }
309-
public decimal Width { get; }
310-
public decimal Height { get; }
311-
312-
public BoundingBox(decimal x, decimal y, decimal width, decimal height)
313-
{
314-
X = x;
315-
Y = y;
316-
Width = width;
317-
Height = height;
318-
}
319-
320-
internal Clip ToClip()
321-
{
322-
return new Clip
323-
{
324-
X = X,
325-
Y = Y,
326-
Width = Width,
327-
Height = Height
328-
};
329-
}
330-
}
331309
}
332310
}

0 commit comments

Comments
 (0)
Please sign in to comment.