Skip to content

Commit

Permalink
Implement IAsyncDisposable / IDisposable on IBrowserContext (#2786)
Browse files Browse the repository at this point in the history
* Implement IAsyncDisposable / IDisposable on IBrowserContext

* Use IBrowserContext.DisposeAsync in tests
  • Loading branch information
campersau authored Sep 30, 2024
1 parent e14f59d commit e7f2578
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ await Page.EvaluateFunctionAsync(@"() => {
public async Task ShouldIsolatePermissionsBetweenBrowserContexts()
{
await Page.GoToAsync(TestConstants.EmptyPage);
var otherContext = await Browser.CreateBrowserContextAsync();
await using var otherContext = await Browser.CreateBrowserContextAsync();
var otherPage = await otherContext.NewPageAsync();
await otherPage.GoToAsync(TestConstants.EmptyPage);
Assert.That(await GetPermissionAsync(Page, "geolocation"), Is.EqualTo("prompt"));
Expand All @@ -101,8 +101,6 @@ public async Task ShouldIsolatePermissionsBetweenBrowserContexts()
await Context.ClearPermissionOverridesAsync();
Assert.That(await GetPermissionAsync(Page, "geolocation"), Is.EqualTo("prompt"));
Assert.That(await GetPermissionAsync(otherPage, "geolocation"), Is.EqualTo("granted"));

await otherContext.CloseAsync();
}

[Test, Ignore("Fails on Firefox")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task ShouldCloseAllBelongingTargetsOnceClosingContext()
[Test, Retry(2), PuppeteerTest("browsercontext.spec", "BrowserContext", "window.open should use parent tab context")]
public async Task WindowOpenShouldUseParentTabContext()
{
var context = await Browser.CreateBrowserContextAsync();
await using var context = await Browser.CreateBrowserContextAsync();
var page = await context.NewPageAsync();
await page.GoToAsync(TestConstants.EmptyPage);
var popupTargetCompletion = new TaskCompletionSource<ITarget>();
Expand All @@ -60,13 +60,12 @@ await Task.WhenAll(

var popupTarget = await popupTargetCompletion.Task;
Assert.That(popupTarget.BrowserContext, Is.SameAs(context));
await context.CloseAsync();
}

[Test, Retry(2), PuppeteerTest("browsercontext.spec", "BrowserContext", "should fire target events")]
public async Task ShouldFireTargetEvents()
{
var context = await Browser.CreateBrowserContextAsync();
await using var context = await Browser.CreateBrowserContextAsync();
var events = new List<string>();
context.TargetCreated += (_, e) => events.Add("CREATED: " + e.Target.Url);
context.TargetChanged += (_, e) => events.Add("CHANGED: " + e.Target.Url);
Expand All @@ -82,7 +81,6 @@ public async Task ShouldFireTargetEvents()
$"CHANGED: {TestConstants.EmptyPage}",
$"DESTROYED: {TestConstants.EmptyPage}"
}));
await context.CloseAsync();
}

[Test, Retry(2), PuppeteerTest("browsercontext.spec", "BrowserContext", "should isolate localStorage and cookies")]
Expand Down Expand Up @@ -135,7 +133,7 @@ await page2.EvaluateExpressionAsync(@"{
public async Task ShouldWorkAcrossSessions()
{
Assert.That(Browser.BrowserContexts(), Has.Exactly(1).Items);
var context = await Browser.CreateBrowserContextAsync();
await using var context = await Browser.CreateBrowserContextAsync();
Assert.That(Browser.BrowserContexts(), Has.Length.EqualTo(2));

var remoteBrowser = await Puppeteer.ConnectAsync(new ConnectOptions
Expand All @@ -145,7 +143,6 @@ public async Task ShouldWorkAcrossSessions()
var contexts = remoteBrowser.BrowserContexts();
Assert.That(contexts, Has.Length.EqualTo(2));
remoteBrowser.Disconnect();
await context.CloseAsync();
}

[Test, Retry(2), PuppeteerTest("browsercontext.spec", "BrowserContext", "should provide a context id")]
Expand All @@ -154,32 +151,29 @@ public async Task ShouldProvideAContextId()
Assert.That(Browser.BrowserContexts(), Has.Exactly(1).Items);
Assert.That(Browser.BrowserContexts()[0].Id, Is.Null);

var context = await Browser.CreateBrowserContextAsync();
await using var context = await Browser.CreateBrowserContextAsync();
Assert.That(Browser.BrowserContexts(), Has.Length.EqualTo(2));
Assert.That(Browser.BrowserContexts()[1].Id, Is.Not.Null);
await context.CloseAsync();
}

[Test, Retry(2), PuppeteerTest("browsercontext.spec", "BrowserContext", "should wait for a target")]
public async Task ShouldWaitForTarget()
{
var context = await Browser.CreateBrowserContextAsync();
await using var context = await Browser.CreateBrowserContextAsync();
var targetPromise = context.WaitForTargetAsync((target) => target.Url == TestConstants.EmptyPage);
var page = await context.NewPageAsync();
await page.GoToAsync(TestConstants.EmptyPage);
var promiseTarget = await targetPromise;
var targetPage = await promiseTarget.PageAsync();
Assert.That(page, Is.EqualTo(targetPage));
await context.CloseAsync();
}

[Test, Retry(2), PuppeteerTest("browsercontext.spec", "BrowserContext", "should timeout waiting for a non-existent target")]
public async Task ShouldTimeoutWaitingForNonExistentTarget()
{
var context = await Browser.CreateBrowserContextAsync();
await using var context = await Browser.CreateBrowserContextAsync();
Assert.ThrowsAsync<TimeoutException>(()
=> context.WaitForTargetAsync((target) => target.Url == TestConstants.EmptyPage, new WaitForOptions(1)));
await context.CloseAsync();
}
}
}
4 changes: 1 addition & 3 deletions lib/PuppeteerSharp.Tests/CookiesTests/SetCookiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ await Page.SetCookieAsync(new CookieParam
[Test, Retry(2), PuppeteerTest("cookies.spec", "Cookie specs Page.setCookie", "should isolate cookies in browser contexts")]
public async Task ShouldIsolateCookiesInBrowserContexts()
{
var anotherContext = await Browser.CreateBrowserContextAsync();
await using var anotherContext = await Browser.CreateBrowserContextAsync();
var anotherPage = await anotherContext.NewPageAsync();

await Page.GoToAsync(TestConstants.EmptyPage);
Expand All @@ -53,8 +53,6 @@ await anotherPage.SetCookieAsync(new CookieParam
Assert.That(cookies1[0].Value, Is.EqualTo("page1value"));
Assert.That(cookies2[0].Name, Is.EqualTo("page2cookie"));
Assert.That(cookies2[0].Value, Is.EqualTo("page2value"));

await anotherContext.CloseAsync();
}

[Test, Retry(2), PuppeteerTest("cookies.spec", "Cookie specs Page.setCookie", "should set multiple cookies")]
Expand Down
23 changes: 23 additions & 0 deletions lib/PuppeteerSharp/BrowserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,33 @@ public Task<ITarget> WaitForTargetAsync(Func<ITarget, bool> predicate, WaitForOp
/// <inheritdoc/>
public abstract Task ClearPermissionOverridesAsync();

/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Closes the browser context. All the targets that belong to the browser context will be closed.
/// </summary>
/// <returns>ValueTask.</returns>
public async ValueTask DisposeAsync()
{
await CloseAsync().ConfigureAwait(false);
GC.SuppressFinalize(this);
}

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

internal void OnTargetDestroyed(Browser browser, TargetChangedArgs args) => TargetDestroyed?.Invoke(browser, args);

internal void OnTargetChanged(Browser browser, TargetChangedArgs args) => TargetChanged?.Invoke(browser, args);

/// <summary>
/// Closes the browser context. All the targets that belong to the browser context will be closed.
/// </summary>
/// <param name="disposing">Indicates whether disposal was initiated by <see cref="Dispose()"/> operation.</param>
protected virtual void Dispose(bool disposing) => _ = CloseAsync();
}
}
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/IBrowserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace PuppeteerSharp
/// BrowserContexts provide a way to operate multiple independent browser sessions. When a browser is launched, it has
/// a single <see cref="IBrowserContext"/> used by default. The method <see cref="IBrowser.NewPageAsync"/> creates a <see cref="IPage"/> in the default <see cref="IBrowserContext"/>.
/// </summary>
public interface IBrowserContext
public interface IBrowserContext : IDisposable, IAsyncDisposable
{
/// <summary>
/// Raised when the url of a target changes
Expand Down

0 comments on commit e7f2578

Please sign in to comment.