Skip to content

Commit 725c76c

Browse files
authored
Expose browser context Id (#2029)
* Expose browser context Id * do not fail fast
1 parent 4fae4d5 commit 725c76c

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

.github/workflows/dotnet.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
name: build-${{ matrix.browser }}/${{ matrix.os }}
2121
runs-on: ${{ matrix.os }}
2222
strategy:
23+
fail-fast: false
2324
matrix:
2425
os: [ubuntu-latest]
2526
browser: [FIREFOX,CHROME]

lib/PuppeteerSharp.Tests/BrowserContextTests/BrowserContextTests.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
53
using System.Threading.Tasks;
64
using PuppeteerSharp.Tests.Attributes;
75
using PuppeteerSharp.Xunit;
@@ -159,6 +157,19 @@ public async Task ShouldWorkAcrossSessions()
159157
await context.CloseAsync();
160158
}
161159

160+
[PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "should provide a context id")]
161+
[SkipBrowserFact(skipFirefox: true)]
162+
public async Task ShouldProvideAContextId()
163+
{
164+
Assert.Single(Browser.BrowserContexts());
165+
Assert.Null(Browser.BrowserContexts()[0].Id);
166+
167+
var context = await Browser.CreateIncognitoBrowserContextAsync();
168+
Assert.Equal(2, Browser.BrowserContexts().Length);
169+
Assert.NotNull(Browser.BrowserContexts()[1].Id);
170+
await context.CloseAsync();
171+
}
172+
162173
[PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "should wait for a target")]
163174
[SkipBrowserFact(skipFirefox: true)]
164175
public async Task ShouldWaitForTarget()

lib/PuppeteerSharp/BrowserContext.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ namespace PuppeteerSharp
1010
public class BrowserContext : IBrowserContext
1111
{
1212
private readonly Connection _connection;
13-
private readonly string _id;
1413

1514
internal BrowserContext(Connection connection, Browser browser, string contextId)
1615
{
1716
_connection = connection;
1817
Browser = browser;
19-
_id = contextId;
18+
Id = contextId;
2019
}
2120

2221
/// <inheritdoc/>
@@ -29,7 +28,10 @@ internal BrowserContext(Connection connection, Browser browser, string contextId
2928
public event EventHandler<TargetChangedArgs> TargetDestroyed;
3029

3130
/// <inheritdoc/>
32-
public bool IsIncognito => _id != null;
31+
public string Id { get; }
32+
33+
/// <inheritdoc/>
34+
public bool IsIncognito => Id != null;
3335

3436
/// <inheritdoc/>
3537
public Browser Browser { get; }
@@ -51,33 +53,33 @@ public async Task<IPage[]> PagesAsync()
5153
.Where(p => p != null).ToArray();
5254

5355
/// <inheritdoc/>
54-
public Task<IPage> NewPageAsync() => Browser.CreatePageInContextAsync(_id);
56+
public Task<IPage> NewPageAsync() => Browser.CreatePageInContextAsync(Id);
5557

5658
/// <inheritdoc/>
5759
public Task CloseAsync()
5860
{
59-
if (_id == null)
61+
if (Id == null)
6062
{
6163
throw new PuppeteerException("Non-incognito profiles cannot be closed!");
6264
}
6365

64-
return Browser.DisposeContextAsync(_id);
66+
return Browser.DisposeContextAsync(Id);
6567
}
6668

6769
/// <inheritdoc/>
6870
public Task OverridePermissionsAsync(string origin, IEnumerable<OverridePermission> permissions)
6971
=> _connection.SendAsync("Browser.grantPermissions", new BrowserGrantPermissionsRequest
7072
{
7173
Origin = origin,
72-
BrowserContextId = _id,
74+
BrowserContextId = Id,
7375
Permissions = permissions.ToArray(),
7476
});
7577

7678
/// <inheritdoc/>
7779
public Task ClearPermissionOverridesAsync()
7880
=> _connection.SendAsync("Browser.resetPermissions", new BrowserResetPermissionsRequest
7981
{
80-
BrowserContextId = _id,
82+
BrowserContextId = Id,
8183
});
8284

8385
internal void OnTargetCreated(Browser browser, TargetChangedArgs args) => TargetCreated?.Invoke(browser, args);

lib/PuppeteerSharp/IBrowserContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public interface IBrowserContext
2525
/// </summary>
2626
event EventHandler<TargetChangedArgs> TargetDestroyed;
2727

28+
/// <summary>
29+
/// Browser Context Id
30+
/// </summary>
31+
string Id { get; }
32+
2833
/// <summary>
2934
/// Gets the browser this browser context belongs to
3035
/// </summary>

0 commit comments

Comments
 (0)