Skip to content

Commit 61a6a27

Browse files
authored
Add BrowserContext.Pages() method (#622)
1 parent 3fff9a0 commit 61a6a27

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

lib/PuppeteerSharp.Tests/BrowserContextTests/BrowserContextTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task ShouldCloseAllBelongingTargetsOnceClosingContext()
4545
var context = await Browser.CreateIncognitoBrowserContextAsync();
4646
await context.NewPageAsync();
4747
Assert.Equal(2, (await Browser.PagesAsync()).Length);
48-
48+
Assert.Single((await context.PagesAsync()));
4949
await context.CloseAsync();
5050
Assert.Single((await Browser.PagesAsync()));
5151
}

lib/PuppeteerSharp/Browser.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,21 @@ public BrowserContext[] BrowserContexts()
194194
{
195195
var allContexts = new BrowserContext[_contexts.Count + 1];
196196
allContexts[0] = _defaultContext;
197-
_contexts.Values.CopyTo(allContexts, 0);
197+
_contexts.Values.CopyTo(allContexts, 1);
198198
return allContexts;
199199
}
200200

201201
/// <summary>
202202
/// Returns a Task which resolves to an array of all open pages.
203203
/// Non visible pages, such as <c>"background_page"</c>, will not be listed here. You can find them using <see cref="Target.PageAsync"/>
204204
/// </summary>
205-
/// <returns>Task which resolves to an array of all open pages.</returns>
205+
/// <returns>Task which resolves to an array of all open pages inside the Browser.
206+
/// In case of multiple browser contexts, the method will return an array with all the pages in all browser contexts.
207+
/// </returns>
206208
public async Task<Page[]> PagesAsync()
207209
=> (await Task.WhenAll(
208-
Targets().Where(t => t.Type == TargetType.Page).Select(target => target.PageAsync())
209-
).ConfigureAwait(false)).ToArray();
210+
BrowserContexts().Select(t => t.PagesAsync())).ConfigureAwait(false)
211+
).SelectMany(p => p).ToArray();
210212

211213
/// <summary>
212214
/// Gets the browser's version

lib/PuppeteerSharp/BrowserContext.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Threading.Tasks;
34

45
namespace PuppeteerSharp
@@ -52,6 +53,17 @@ internal BrowserContext(Browser browser, string contextId)
5253
/// <returns>An array of all active targets inside the browser context</returns>
5354
public Target[] Targets() => Array.FindAll(Browser.Targets(), target => target.BrowserContext == this);
5455

56+
/// <summary>
57+
/// An array of all pages inside the browser context.
58+
/// </summary>
59+
/// <returns>Task which resolves to an array of all open pages.
60+
/// Non visible pages, such as <c>"background_page"</c>, will not be listed here.
61+
/// You can find them using <see cref="Target.PageAsync"/>.</returns>
62+
public async Task<Page[]> PagesAsync()
63+
=> (await Task.WhenAll(
64+
Targets().Where(t => t.Type == TargetType.Page).Select(t => t.PageAsync())).ConfigureAwait(false)
65+
).Where(p => p != null).ToArray();
66+
5567
/// <summary>
5668
/// Creates a new page
5769
/// </summary>

0 commit comments

Comments
 (0)