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(dotnet): unwrap HumanizedLocator for Playwright APIs that down-cast ILocator (#549)
CloakHQ11c76ebe7530
diff --git a/dotnet/examples/CloakBrowser.Examples/Program.cs b/dotnet/examples/CloakBrowser.Examples/Program.csindex e120f07..472f476 100644--- a/dotnet/examples/CloakBrowser.Examples/Program.cs+++ b/dotnet/examples/CloakBrowser.Examples/Program.cs@@ -27,6 +27,7 @@ switch (which)case "webrtctest": await WebRtcTest(); break;case "trusted": await TrustedTest(); break;case "timeout": await TimeoutTest(); break;+ case "repro549": await Repro549(); break;default:Console.Error.WriteLine($"Unknown example: {which}");Console.Error.WriteLine("Available: basic, humanize, context, persistent, proxy-geoip, bottest, behavioral, visual");@@ -568,3 +569,48 @@ static async Task TimeoutTest()? ">>> PASS - timeout budget is shared, not multiplied": ">>> FAIL - timeout multiplied (each step took the full budget)");}++// ---------------------------------------------------------------------------+// Repro for GitHub #549 - AddLocatorHandlerAsync throws under Humanize=true.+// HumanizedLocator (from page.GetByText) is not Playwright's concrete Locator,+// so Playwright's internal `(locator as Locator)._frame` -> null -> NRE.+// Runs the reporter's exact flow with Humanize=true (expected: throw) and a+// Humanize=false control (expected: pass). dotnet run -- repro549+// ---------------------------------------------------------------------------+static async Task Repro549()+{+ await RunAddLocatorHandler(humanize: true);+ await RunAddLocatorHandler(humanize: false);+}++static async Task RunAddLocatorHandler(bool humanize)+{+ Console.WriteLine($"\n=== AddLocatorHandlerAsync with Humanize={humanize} ===");+ await using var browser = await CloakLauncher.LaunchAsync(new LaunchOptions+ {+ Headless = true,+ Humanize = humanize,+ });+ var page = await browser.NewPageAsync();+ await page.GotoAsync("https://example.com/");++ var locator = page.GetByText("Test");+ Console.WriteLine($"page: {page.GetType().FullName}");+ Console.WriteLine($"locator: {locator.GetType().FullName}");++ try+ {+ // Reporter's exact snippet (Func<Task> overload + NoWaitAfter).+ await page.AddLocatorHandlerAsync(page.GetByText("Test"), () =>+ {+ Console.WriteLine("test");+ return Task.CompletedTask;+ }, new PageAddLocatorHandlerOptions() { NoWaitAfter = true });+ Console.WriteLine(">>> OK - handler registered, no exception");+ }+ catch (Exception ex)+ {+ Console.WriteLine($">>> THREW: {ex.GetType().FullName}: {ex.Message}");+ Console.WriteLine(ex.StackTrace?.Split('\n').FirstOrDefault()?.Trim());+ }+}diff --git a/dotnet/src/CloakBrowser/Handles.cs b/dotnet/src/CloakBrowser/Handles.csindex 3b1e9e4..5737e26 100644--- a/dotnet/src/CloakBrowser/Handles.cs+++ b/dotnet/src/CloakBrowser/Handles.cs@@ -197,6 +197,7 @@ internal static class LicenseGuardcase IGuardedProxy g: current = g.GuardTarget; break;case HumanizedPage p: current = p.Original; break;case HumanizedFrame f: current = f.Original; break;+ case HumanizedLocator l: current = l.Original; break;case HumanizedElementHandle e: current = e.Original; break;case HumanizedBrowserContext c: current = c.Original; break;case HumanizedBrowser b: current = b.Original; break;diff --git a/dotnet/src/CloakBrowser/Wrappers/Humanize.cs b/dotnet/src/CloakBrowser/Wrappers/Humanize.csindex f77710c..d51b76d 100644--- a/dotnet/src/CloakBrowser/Wrappers/Humanize.cs+++ b/dotnet/src/CloakBrowser/Wrappers/Humanize.cs@@ -76,6 +76,10 @@ public static class Humanize/// <summary>Recover the raw Playwright <see cref="IElementHandle"/> behind any CloakBrowser wrapper.</summary>public static IElementHandle Unwrap(IElementHandle handle) => (IElementHandle)LicenseGuard.Unwrap(handle);+ /// <summary>Recover the raw Playwright <see cref="ILocator"/> behind a CloakBrowser wrapper+ /// (for APIs that down-cast their locator arg, e.g. <c>page.AddLocatorHandlerAsync</c>).</summary>+ public static ILocator Unwrap(ILocator locator) => (ILocator)LicenseGuard.Unwrap(locator);+// -----------------------------------------------------------------------// Internal re-wrap helpers (shared by the wrappers).// -----------------------------------------------------------------------diff --git a/dotnet/src/CloakBrowser/Wrappers/HumanizedElementHandle.cs b/dotnet/src/CloakBrowser/Wrappers/HumanizedElementHandle.csindex 60723f7..5040da9 100644--- a/dotnet/src/CloakBrowser/Wrappers/HumanizedElementHandle.cs+++ b/dotnet/src/CloakBrowser/Wrappers/HumanizedElementHandle.cs@@ -141,6 +141,15 @@ public sealed partial class HumanizedElementHandle : IElementHandle// -----------------------------------------------------------------------private static IElementHandle Unwrap(IElementHandle h) => h is HumanizedElementHandle w ? w.Original : h;+ private static ILocator Unwrap(ILocator l) => l is HumanizedLocator w ? w.Original : l;++ // #549: unwrap masked locators in place (rebuilding options would drop future fields).+ public Task<byte[]> ScreenshotAsync(ElementHandleScreenshotOptions? options = null)+ {+ if (options?.Mask != null)+ options.Mask = options.Mask.Select(Unwrap).ToList();+ return _inner.ScreenshotAsync(options);+ }private async Task SelectPrologueAsync(ElementHandleSelectOptionOptions? options){diff --git a/dotnet/src/CloakBrowser/Wrappers/HumanizedLocator.cs b/dotnet/src/CloakBrowser/Wrappers/HumanizedLocator.csindex 347bd22..9467a3d 100644--- a/dotnet/src/CloakBrowser/Wrappers/HumanizedLocator.cs+++ b/dotnet/src/CloakBrowser/Wrappers/HumanizedLocator.cs@@ -222,6 +222,14 @@ public sealed partial class HumanizedLocator : ILocatorpublic ILocator And(ILocator locator) =>Wrap(_inner.And(locator is HumanizedLocator h ? h.Original : locator));+ // #549: unwrap Has/HasNot in place (rebuilding options would drop future fields).+ public ILocator Filter(LocatorFilterOptions? options = null)+ {+ if (options?.Has is HumanizedLocator has) options.Has = has.Original;+ if (options?.HasNot is HumanizedLocator hasNot) options.HasNot = hasNot.Original;+ return Wrap(_inner.Filter(options));+ }+public ILocator Locator(string selectorOrLocator, LocatorLocatorOptions? options = null) =>Wrap(_inner.Locator(selectorOrLocator, options));public ILocator Locator(ILocator selectorOrLocator, LocatorLocatorOptions? options = null) =>diff --git a/dotnet/src/CloakBrowser/Wrappers/HumanizedPage.cs b/dotnet/src/CloakBrowser/Wrappers/HumanizedPage.csindex d84b5c2..414620b 100644--- a/dotnet/src/CloakBrowser/Wrappers/HumanizedPage.cs+++ b/dotnet/src/CloakBrowser/Wrappers/HumanizedPage.cs@@ -181,6 +181,26 @@ public sealed partial class HumanizedPage : IPage}private static IElementHandle Unwrap(IElementHandle h) => h is HumanizedElementHandle hh ? hh.Original : h;+ private static ILocator Unwrap(ILocator l) => l is HumanizedLocator hl ? hl.Original : l;++ // #549: Playwright down-casts ILocator args to concrete Locator; unwrap ours first.+ public Task AddLocatorHandlerAsync(ILocator locator, Func<Task> handler, PageAddLocatorHandlerOptions? options = null) =>+ _inner.AddLocatorHandlerAsync(Unwrap(locator), handler, options);++ // Re-wrap the callback locator so the user's handler stays humanized.+ public Task AddLocatorHandlerAsync(ILocator locator, Func<ILocator, Task> handler, PageAddLocatorHandlerOptions? options = null) =>+ _inner.AddLocatorHandlerAsync(Unwrap(locator), l => handler(Wrap(l)), options);++ public Task RemoveLocatorHandlerAsync(ILocator locator) =>+ _inner.RemoveLocatorHandlerAsync(Unwrap(locator));++ // #549: unwrap masked locators in place (rebuilding options would drop future fields).+ public Task<byte[]> ScreenshotAsync(PageScreenshotOptions? options = null)+ {+ if (options?.Mask != null)+ options.Mask = options.Mask.Select(Unwrap).ToList();+ return _inner.ScreenshotAsync(options);+ }// -----------------------------------------------------------------------// Locator-returning members - re-wrap.diff --git a/dotnet/tests/CloakBrowser.Tests/Wrappers/LocatorWrapperTests.cs b/dotnet/tests/CloakBrowser.Tests/Wrappers/LocatorWrapperTests.csindex 7ee9213..d504dd6 100644--- a/dotnet/tests/CloakBrowser.Tests/Wrappers/LocatorWrapperTests.cs+++ b/dotnet/tests/CloakBrowser.Tests/Wrappers/LocatorWrapperTests.cs@@ -392,4 +392,30 @@ public class LocatorWrapperTestsawait Assert.ThrowsAsync<OperationCanceledException>(() => human.ClickAsync());}++ // #549: Filter's Has/HasNot must reach Playwright unwrapped.+ [Fact]+ public void Filter_unwraps_Has_and_HasNot_and_rewraps_result()+ {+ var (page, _, _) = BuildPage();+ var (inner, innerRec) = Fake.Of<ILocator>();+ var (resultLoc, _) = Fake.Of<ILocator>();+ innerRec.On("Filter", resultLoc);++ var cursor = new HumanCursor(page);+ var (rawHas, _) = Fake.Of<ILocator>();+ var (rawHasNot, _) = Fake.Of<ILocator>();+ var human = new HumanizedLocator(inner, cursor, FastConfig());++ var result = human.Filter(new LocatorFilterOptions+ {+ Has = new HumanizedLocator(rawHas, cursor, FastConfig()),+ HasNot = new HumanizedLocator(rawHasNot, cursor, FastConfig()),+ });++ var opts = (LocatorFilterOptions)innerRec.Last("Filter")!.Args[0]!;+ Assert.Same(rawHas, opts.Has); // raw locator reached Playwright+ Assert.Same(rawHasNot, opts.HasNot);+ Assert.IsType<HumanizedLocator>(result); // returned locator stays humanized+ }}diff --git a/dotnet/tests/CloakBrowser.Tests/Wrappers/PageWrapperTests.cs b/dotnet/tests/CloakBrowser.Tests/Wrappers/PageWrapperTests.csindex c86b439..3d1c66b 100644--- a/dotnet/tests/CloakBrowser.Tests/Wrappers/PageWrapperTests.cs+++ b/dotnet/tests/CloakBrowser.Tests/Wrappers/PageWrapperTests.cs@@ -333,4 +333,69 @@ public class PageWrapperTestsawait Assert.ThrowsAsync<PlaywrightException>(() => human.ContentAsync());}++ // #549: locator-arg APIs must hand Playwright the raw locator, not our decorator.+ private static (HumanizedPage human, FakeProxy pageRec) BuildBarePage()+ {+ var (mouse, _) = Fake.Of<IMouse>();+ var (keyboard, _) = Fake.Of<IKeyboard>();+ var (page, pageRec) = Fake.Of<IPage>();+ pageRec.On("Mouse", mouse);+ pageRec.On("Keyboard", keyboard);+ return (new HumanizedPage(page, new HumanCursor(page), FastConfig()), pageRec);+ }++ private static HumanizedLocator WrapRaw(ILocator raw)+ {+ var (page, _) = Fake.Of<IPage>();+ return new HumanizedLocator(raw, new HumanCursor(page), FastConfig());+ }++ [Fact]+ public async Task AddAndRemoveLocatorHandler_unwrap_locator_arg()+ {+ var (human, pageRec) = BuildBarePage();+ var (rawLoc, _) = Fake.Of<ILocator>();+ var arg = WrapRaw(rawLoc);++ await human.AddLocatorHandlerAsync(arg, () => Task.CompletedTask);+ Assert.Same(rawLoc, pageRec.Last("AddLocatorHandlerAsync")!.Args[0]);++ await human.RemoveLocatorHandlerAsync(arg);+ Assert.Same(rawLoc, pageRec.Last("RemoveLocatorHandlerAsync")!.Args[0]);+ }++ [Fact]+ public async Task AddLocatorHandler_rewraps_callback_locator()+ {+ var (mouse, _) = Fake.Of<IMouse>();+ var (keyboard, _) = Fake.Of<IKeyboard>();+ var (page, pageRec) = Fake.Of<IPage>();+ pageRec.On("Mouse", mouse);+ pageRec.On("Keyboard", keyboard);+ var (rawCallbackLoc, _) = Fake.Of<ILocator>();+ // Fake Playwright fires the registered handler with a raw matched locator.+ pageRec.On("AddLocatorHandlerAsync", args => ((Func<ILocator, Task>)args[1]!)(rawCallbackLoc));+ var human = new HumanizedPage(page, new HumanCursor(page), FastConfig());++ var (rawLoc, _) = Fake.Of<ILocator>();+ ILocator? received = null;+ await human.AddLocatorHandlerAsync(WrapRaw(rawLoc), loc => { received = loc; return Task.CompletedTask; });++ Assert.Same(rawLoc, pageRec.Last("AddLocatorHandlerAsync")!.Args[0]); // input unwrapped+ Assert.IsType<HumanizedLocator>(received); // callback re-wrapped+ }++ [Fact]+ public async Task Screenshot_unwraps_masked_locators()+ {+ var (human, pageRec) = BuildBarePage();+ pageRec.On("ScreenshotAsync", Task.FromResult(System.Array.Empty<byte>()));+ var (rawLoc, _) = Fake.Of<ILocator>();++ await human.ScreenshotAsync(new PageScreenshotOptions { Mask = new[] { WrapRaw(rawLoc) } });++ var opts = (PageScreenshotOptions)pageRec.Last("ScreenshotAsync")!.Args[0]!;+ Assert.Same(rawLoc, opts.Mask!.First());+ }}