Stealth Chromium that passes every bot detection test. Drop-in Playwright replacement with source-level fingerprint patches. 50+ tests passed.
Python ai-agentsanti-detectantidetect-browserbot-detectionbrowser-automationcaptcha-bypasschromiumcloudflarecloudflare-bypassfingerprintheadless-browserplaywrightpuppeteerpythonrecaptchaseleniumstealth-browserundetectedweb-scrapingwebscraping
fix(js,dotnet): forward close reason and always close the browser
CloakHQ5d6813f4c05f
diff --git a/dotnet/src/CloakBrowser/Handles.cs b/dotnet/src/CloakBrowser/Handles.csindex 5737e26..6ffac7b 100644--- a/dotnet/src/CloakBrowser/Handles.cs+++ b/dotnet/src/CloakBrowser/Handles.cs@@ -8,7 +8,7 @@ namespace CloakBrowser;/// <summary>/// A launched stealth browser. Owns the underlying Playwright instance and disposes-/// it on <see cref="CloseAsync"/>. Use <see cref="Browser"/> for the raw Playwright API.+/// it on <see cref="CloseAsync()"/>. Use <see cref="Browser"/> for the raw Playwright API./// </summary>public sealed class CloakBrowserHandle : IAsyncDisposable{@@ -106,9 +106,12 @@ public sealed class CloakBrowserHandle : IAsyncDisposablepublic bool HumanizeEnabled => _humanize;/// <summary>Close the browser and stop the underlying Playwright instance.</summary>- public async Task CloseAsync()+ public Task CloseAsync() => CloseAsync(null);++ /// <summary>Close the browser (options such as <c>Reason</c> are forwarded) and stop Playwright.</summary>+ public async Task CloseAsync(BrowserCloseOptions? options){- try { await _rawBrowser.CloseAsync().ConfigureAwait(false); }+ try { await _rawBrowser.CloseAsync(options).ConfigureAwait(false); }finally { _playwright.Dispose(); }}@@ -356,7 +359,7 @@ internal class LicenseGuardProxy<T> : DispatchProxy, IGuardedProxy where T : cla/// <summary>/// A launched stealth browser context. Owns the underlying Playwright instance (and,-/// for non-persistent contexts, the browser) and cleans them up on <see cref="CloseAsync"/>.+/// for non-persistent contexts, the browser) and cleans them up on <see cref="CloseAsync()"/>./// </summary>public sealed class CloakContextHandle : IAsyncDisposable{@@ -415,13 +418,22 @@ public sealed class CloakContextHandle : IAsyncDisposablepublic bool HumanizeEnabled => _humanize;/// <summary>Close the context (and browser, if owned) and stop Playwright.</summary>- public async Task CloseAsync()+ public Task CloseAsync() => CloseAsync(null);++ /// <summary>Close the context (options such as <c>Reason</c> are forwarded), the browser if owned, and stop Playwright.</summary>+ public async Task CloseAsync(BrowserContextCloseOptions? options){try{- await _rawContext.CloseAsync().ConfigureAwait(false);- if (_browser != null)- await _browser.CloseAsync().ConfigureAwait(false);+ try+ {+ await _rawContext.CloseAsync(options).ConfigureAwait(false);+ }+ finally+ {+ if (_browser != null)+ await _browser.CloseAsync().ConfigureAwait(false);+ }}finally{diff --git a/dotnet/tests/CloakBrowser.Tests/Wrappers/HandleCloseTests.cs b/dotnet/tests/CloakBrowser.Tests/Wrappers/HandleCloseTests.csnew file mode 100644index 0000000..e19e254--- /dev/null+++ b/dotnet/tests/CloakBrowser.Tests/Wrappers/HandleCloseTests.cs@@ -0,0 +1,57 @@+using CloakBrowser;+using Microsoft.Playwright;+using Xunit;++namespace CloakBrowser.Tests.Wrappers;++/// <summary>+/// Playwright's own CloseAsync takes options (e.g. Reason); the handles must forward+/// them and still run their cleanup (browser close + Playwright dispose).+/// </summary>+public class HandleCloseTests+{+ [Fact]+ public async Task Context_close_forwards_options_and_closes_browser()+ {+ var (playwright, pwRec) = Fake.Of<IPlaywright>();+ var (browser, browserRec) = Fake.Of<IBrowser>();+ var (ctx, ctxRec) = Fake.Of<IBrowserContext>();+ var handle = new CloakContextHandle(playwright, browser, ctx, humanize: false, null);++ var options = new BrowserContextCloseOptions { Reason = "shutting down" };+ await handle.CloseAsync(options);++ Assert.Same(options, ctxRec.Last("CloseAsync")!.Args[0]);+ Assert.Equal(1, browserRec.CountOf("CloseAsync"));+ Assert.Equal(1, pwRec.CountOf("Dispose"));+ }++ [Fact]+ public async Task Context_close_still_closes_browser_when_context_close_throws()+ {+ var (playwright, pwRec) = Fake.Of<IPlaywright>();+ var (browser, browserRec) = Fake.Of<IBrowser>();+ var (ctx, ctxRec) = Fake.Of<IBrowserContext>();+ ctxRec.On("CloseAsync", _ => Task.FromException(new InvalidOperationException("boom")));+ var handle = new CloakContextHandle(playwright, browser, ctx, humanize: false, null);++ await Assert.ThrowsAsync<InvalidOperationException>(() => handle.CloseAsync());++ Assert.Equal(1, browserRec.CountOf("CloseAsync"));+ Assert.Equal(1, pwRec.CountOf("Dispose"));+ }++ [Fact]+ public async Task Browser_close_forwards_options()+ {+ var (playwright, pwRec) = Fake.Of<IPlaywright>();+ var (browser, browserRec) = Fake.Of<IBrowser>();+ var handle = new CloakBrowserHandle(playwright, browser, humanize: false, null);++ var options = new BrowserCloseOptions { Reason = "shutting down" };+ await handle.CloseAsync(options);++ Assert.Same(options, browserRec.Last("CloseAsync")!.Args[0]);+ Assert.Equal(1, pwRec.CountOf("Dispose"));+ }+}diff --git a/js/src/playwright.ts b/js/src/playwright.tsindex b6562a6..547f6a0 100644--- a/js/src/playwright.ts+++ b/js/src/playwright.ts@@ -299,9 +299,12 @@ export async function launchContext(// Patch close() to also close the browserconst origClose = context.close.bind(context);- context.close = async () => {- await origClose();- await browser.close();+ context.close = async (closeOptions?: { reason?: string }) => {+ try {+ await origClose(closeOptions);+ } finally {+ await browser.close();+ }};// browser.newContext above is already guarded by launch(); also guard thediff --git a/js/tests/launch.test.ts b/js/tests/launch.test.tsindex 84b014b..110af21 100644--- a/js/tests/launch.test.ts+++ b/js/tests/launch.test.ts@@ -393,6 +393,24 @@ describe("launchContext (unit)", () => {expect(mockBrowser.close).toHaveBeenCalledOnce();});+ it("close({ reason }) forwards options to the original close", async () => {+ const { launchContext } = await import("../src/playwright.js");+ const ctx = await launchContext();++ await ctx.close({ reason: "shutting down" });+ expect(mockContext._origClose).toHaveBeenCalledWith({ reason: "shutting down" });+ expect(mockBrowser.close).toHaveBeenCalledOnce();+ });++ it("close() still closes browser when the context close throws", async () => {+ const { launchContext } = await import("../src/playwright.js");+ const ctx = await launchContext();+ mockContext._origClose.mockRejectedValueOnce(new Error("boom"));++ await expect(ctx.close()).rejects.toThrow("boom");+ expect(mockBrowser.close).toHaveBeenCalledOnce();+ });+it("forwards contextOptions to newContext (storageState, etc.)", async () => {const { launchContext } = await import("../src/playwright.js");await launchContext({