Skip to content

Commit 3e9669b

Browse files
Meir017kblok
authored andcommitted
Implement ElementHandle.QuerySelectorAll (a.k.a $$) (#219)
1 parent 4c106f3 commit 3e9669b

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace PuppeteerSharp.Tests.ElementHandleTests
6+
{
7+
[Collection("PuppeteerLoaderFixture collection")]
8+
public class QuerySelectorAllTests : PuppeteerPageBaseTest
9+
{
10+
[Fact]
11+
public async Task ShouldQueryExistingElements()
12+
{
13+
await Page.SetContentAsync("<html><body><div>A</div><br/><div>B</div></body></html>");
14+
var html = await Page.QuerySelectorAsync("html");
15+
var elements = await html.QuerySelectorAllAsync("div");
16+
Assert.Equal(2, elements.Length);
17+
var tasks = elements.Select(element => Page.EvaluateFunctionAsync<string>("e => e.textContent", element));
18+
Assert.Equal(new[] { "A", "B" }, await Task.WhenAll(tasks));
19+
}
20+
21+
[Fact]
22+
public async Task ShouldReturnEmptyArrayForNonExistingElements()
23+
{
24+
await Page.SetContentAsync("<html><body><span>A</span><br/><span>B</span></body></html>");
25+
var html = await Page.QuerySelectorAsync("html");
26+
var elements = await html.QuerySelectorAllAsync("div");
27+
Assert.Empty(elements);
28+
}
29+
}
30+
}

lib/PuppeteerSharp.Tests/PuppeteerSharp.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
</ItemGroup>
1414
<ItemGroup>
1515
<Folder Include="Issues\" />
16-
<Folder Include="ElementHandleTests\" />
1716
</ItemGroup>
1817
<PropertyGroup Condition="'$(TargetFramework)'=='net471'">
1918
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>

lib/PuppeteerSharp/ElementHandle.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,12 @@ public async Task<ElementHandle> QuerySelectorAsync(string selector)
197197
return null;
198198
}
199199

200-
internal async Task<ElementHandle[]> QuerySelectorAllAsync(string selector)
200+
/// <summary>
201+
/// Runs <c>element.querySelectorAll</c> within the page. If no elements match the selector, the return value resolve to <see cref="Array.Empty{T}"/>.
202+
/// </summary>
203+
/// <param name="selector">A selector to query element for</param>
204+
/// <returns>Task which resolves to ElementHandles pointing to the frame elements</returns>
205+
public async Task<ElementHandle[]> QuerySelectorAllAsync(string selector)
201206
{
202207
var arrayHandle = await ExecutionContext.EvaluateFunctionHandleAsync(
203208
"(element, selector) => element.querySelectorAll(selector)",

0 commit comments

Comments
 (0)