Skip to content

Commit 0286563

Browse files
kblokMeir017
authored andcommitted
Extension improvements (#637)
EvaluateFunctionAsync extensions now works with resolved tasks
1 parent 6a0bdf1 commit 0286563

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

lib/PuppeteerSharp.Tests/PageTests/EvalManyTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,14 @@ public async Task ShouldWork()
1818
var divsCount = await Page.QuerySelectorAllHandleAsync("div").EvaluateFunctionAsync<int>("divs => divs.length");
1919
Assert.Equal(3, divsCount);
2020
}
21+
22+
[Fact]
23+
public async Task ShouldWorkWithAwaitedElements()
24+
{
25+
await Page.SetContentAsync("<div>hello</div><div>beautiful</div><div>world!</div>");
26+
var divs = await Page.QuerySelectorAllHandleAsync("div");
27+
var divsCount = await divs.EvaluateFunctionAsync<int>("divs => divs.length");
28+
Assert.Equal(3, divsCount);
29+
}
2130
}
2231
}

lib/PuppeteerSharp.Tests/PageTests/EvalTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ public async Task ShouldWork()
1919
Assert.Equal("testAttribute", idAttribute);
2020
}
2121

22+
[Fact]
23+
public async Task ShouldWorkWithAwaitedElements()
24+
{
25+
await Page.SetContentAsync("<section id='testAttribute'>43543</section>");
26+
var section = await Page.QuerySelectorAsync("section");
27+
var idAttribute = await section.EvaluateFunctionAsync<string>("e => e.id");
28+
Assert.Equal("testAttribute", idAttribute);
29+
}
30+
2231
[Fact]
2332
public async Task ShouldAcceptArguments()
2433
{

lib/PuppeteerSharp/Extensions.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ public static async Task<T> EvaluateFunctionAsync<T>(this Task<ElementHandle> el
2424
throw new SelectorException("Error: failed to find element matching selector");
2525
}
2626

27+
return await elementHandle.EvaluateFunctionAsync<T>(pageFunction, args);
28+
}
29+
30+
/// <summary>
31+
/// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome the <paramref name="elementHandle"/> as the first argument
32+
/// </summary>
33+
/// <typeparam name="T">The type of the response</typeparam>
34+
/// <param name="elementHandle">An <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>
35+
/// <param name="pageFunction">Function to be evaluated in browser context</param>
36+
/// <param name="args">Arguments to pass to <c>pageFunction</c></param>
37+
/// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>
38+
/// <exception cref="SelectorException">If <paramref name="elementHandle"/> is <c>null</c></exception>
39+
public static async Task<T> EvaluateFunctionAsync<T>(this ElementHandle elementHandle, string pageFunction, params object[] args)
40+
{
41+
if (elementHandle == null)
42+
{
43+
throw new SelectorException("Error: failed to find element matching selector");
44+
}
45+
2746
var newArgs = new object[args.Length + 1];
2847
newArgs[0] = elementHandle;
2948
args.CopyTo(newArgs, 1);
@@ -41,8 +60,18 @@ public static async Task<T> EvaluateFunctionAsync<T>(this Task<ElementHandle> el
4160
/// <param name="args">Arguments to pass to <c>pageFunction</c></param>
4261
/// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>
4362
public static async Task<T> EvaluateFunctionAsync<T>(this Task<JSHandle> arrayHandleTask, string pageFunction, params object[] args)
63+
=> await (await arrayHandleTask.ConfigureAwait(false)).EvaluateFunctionAsync<T>(pageFunction, args);
64+
65+
/// <summary>
66+
/// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="arrayHandle"/> as the first argument. Use only after <see cref="Page.QuerySelectorAllHandleAsync(string)"/>
67+
/// </summary>
68+
/// <typeparam name="T"></typeparam>
69+
/// <param name="arrayHandle">An <see cref="JSHandle"/> that represents an array of <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>
70+
/// <param name="pageFunction">Function to be evaluated in browser context</param>
71+
/// <param name="args">Arguments to pass to <c>pageFunction</c></param>
72+
/// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>
73+
public static async Task<T> EvaluateFunctionAsync<T>(this JSHandle arrayHandle, string pageFunction, params object[] args)
4474
{
45-
var arrayHandle = await arrayHandleTask.ConfigureAwait(false);
4675
var response = await arrayHandle.JsonValueAsync<object[]>().ConfigureAwait(false);
4776

4877
var newArgs = new object[args.Length + 1];

0 commit comments

Comments
 (0)