Skip to content

Commit 362bbee

Browse files
authored
Implement Frame.Context (#168)
1 parent 9bebc1f commit 362bbee

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Threading.Tasks;
2+
using Xunit;
3+
4+
namespace PuppeteerSharp.Tests.Frame
5+
{
6+
[Collection("PuppeteerLoaderFixture collection")]
7+
public class ContextTests : PuppeteerPageBaseTest
8+
{
9+
[Fact]
10+
public async Task ShouldWork()
11+
{
12+
await Page.GoToAsync(TestConstants.EmptyPage);
13+
await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);
14+
Assert.Equal(2, Page.Frames.Length);
15+
16+
var context1 = await Page.Frames[0].GetExecutionContextAsync();
17+
var context2 = await Page.Frames[1].GetExecutionContextAsync();
18+
Assert.NotNull(context1);
19+
Assert.NotNull(context2);
20+
Assert.NotEqual(context1, context2);
21+
22+
await Task.WhenAll(
23+
context1.EvaluateExpressionAsync("window.a = 1"),
24+
context2.EvaluateExpressionAsync("window.a = 2")
25+
);
26+
27+
var a1 = context1.EvaluateExpressionAsync<int>("window.a");
28+
var a2 = context2.EvaluateExpressionAsync<int>("window.a");
29+
30+
await Task.WhenAll(a1, a2);
31+
32+
Assert.Equal(1, a1.Result);
33+
Assert.Equal(2, a2.Result);
34+
}
35+
}
36+
}

lib/PuppeteerSharp/Frame.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public async Task<T> EvaluateFunctionAsync<T>(string script, params object[] arg
7474
return await context.EvaluateFunctionAsync<T>(script, args);
7575
}
7676

77+
/// <summary>
78+
/// Gets the <see cref="ExecutionContext"/> associated with the frame.
79+
/// </summary>
80+
/// <returns><see cref="ExecutionContext"/> associated with the frame.</returns>
7781
public Task<ExecutionContext> GetExecutionContextAsync() => ContextResolveTaskWrapper.Task;
7882

7983
internal async Task<ElementHandle> GetElementAsync(string selector)

0 commit comments

Comments
 (0)