Skip to content

Commit 21ad44f

Browse files
authored
Implement ElementHandle.contentFrame() (#371)
1 parent 3e1add3 commit 21ad44f

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Threading.Tasks;
2+
using Xunit;
3+
using Xunit.Abstractions;
4+
5+
namespace PuppeteerSharp.Tests.ElementHandleTests
6+
{
7+
[Collection("PuppeteerLoaderFixture collection")]
8+
public class ContentFrameTests : PuppeteerPageBaseTest
9+
{
10+
public ContentFrameTests(ITestOutputHelper output) : base(output)
11+
{
12+
}
13+
14+
[Fact]
15+
public async Task ShouldWork()
16+
{
17+
await Page.GoToAsync(TestConstants.EmptyPage);
18+
await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);
19+
var elementHandle = await Page.QuerySelectorAsync("#frame1");
20+
var frame = await elementHandle.ContentFrameAsync();
21+
Assert.Equal(Page.Frames[1], frame);
22+
}
23+
}
24+
}

lib/PuppeteerSharp/ElementHandle.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@ public class ElementHandle : JSHandle
1919
{
2020
internal Page Page { get; }
2121

22-
internal ElementHandle(ExecutionContext context, CDPSession client, object remoteObject, Page page) :
22+
private readonly FrameManager _frameManager;
23+
24+
internal ElementHandle(
25+
ExecutionContext
26+
context,
27+
CDPSession client,
28+
object remoteObject,
29+
Page page,
30+
FrameManager frameManager) :
2331
base(context, client, remoteObject)
2432
{
2533
Page = page;
34+
_frameManager = frameManager;
2635
}
2736

2837
/// <summary>
@@ -354,5 +363,23 @@ public async Task<BoundingBox> BoundingBoxAsync()
354363

355364
return new BoundingBox(x, y, width, height);
356365
}
366+
367+
/// <summary>
368+
///Content frame for element handles referencing iframe nodes, or null otherwise.
369+
/// </summary>
370+
/// <returns>Resolves to the content frame</returns>
371+
public async Task<Frame> ContentFrameAsync()
372+
{
373+
var nodeInfo = await Client.SendAsync<DomDescribeNodeResponse>("DOM.describeNode", new
374+
{
375+
RemoteObject.objectId
376+
});
377+
378+
if (string.IsNullOrEmpty(nodeInfo.Node.FrameId))
379+
{
380+
return null;
381+
}
382+
return _frameManager.Frames[nodeInfo.Node.FrameId];
383+
}
357384
}
358385
}

lib/PuppeteerSharp/FrameManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal JSHandle CreateJSHandle(int contextId, dynamic remoteObject)
4949

5050
if (remoteObject.subtype == "node")
5151
{
52-
return new ElementHandle(storedContext, _client, remoteObject, _page);
52+
return new ElementHandle(storedContext, _client, remoteObject, _page, this);
5353
}
5454

5555
return new JSHandle(storedContext, _client, remoteObject);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace PuppeteerSharp.Messaging
5+
{
6+
internal class DomDescribeNodeResponse
7+
{
8+
[JsonProperty("node")]
9+
public DomNode Node { get; set; }
10+
11+
internal class DomNode
12+
{
13+
[JsonProperty("frameId")]
14+
public string FrameId { get; set; }
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)