From a68fd81d5a79891a9251743b0f130d5996054822 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Fri, 23 Jan 2026 03:14:36 +0300 Subject: [PATCH] [dotnet] [bidi] Refactor BiDi module initialization to pass BiDi explicitly --- .../src/webdriver/BiDi/Browser/BrowserModule.cs | 4 ++-- .../BrowsingContext/BrowsingContextModule.cs | 10 +++++----- .../BrowsingContextNetworkModule.cs | 6 +++--- .../webdriver/BiDi/Emulation/EmulationModule.cs | 6 +++--- dotnet/src/webdriver/BiDi/Input/InputModule.cs | 6 +++--- dotnet/src/webdriver/BiDi/Log/LogModule.cs | 10 +++++----- dotnet/src/webdriver/BiDi/Module.cs | 7 ++----- .../BiDi/Network/NetworkModule.HighLevel.cs | 16 ++++++++-------- .../src/webdriver/BiDi/Network/NetworkModule.cs | 10 +++++----- .../BiDi/Permissions/PermissionsModule.cs | 4 ++-- dotnet/src/webdriver/BiDi/Script/ScriptModule.cs | 12 ++++++------ .../src/webdriver/BiDi/Session/SessionModule.cs | 6 +++--- .../src/webdriver/BiDi/Storage/StorageModule.cs | 6 +++--- .../BiDi/WebExtension/WebExtensionModule.cs | 4 ++-- 14 files changed, 52 insertions(+), 55 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs b/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs index a160d4312a990..a579349410376 100644 --- a/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs +++ b/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs @@ -79,9 +79,9 @@ public async Task SetDownloadBehaviorDeniedAsync(SetD return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult).ConfigureAwait(false); } - protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) + protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions) { - jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(bidi)); _jsonContext = new BrowserJsonSerializerContext(jsonSerializerOptions); } diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs index 160c306154ae8..6eda61956e385 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs @@ -253,12 +253,12 @@ public async Task OnUserPromptClosedAsync(Action InterceptRequestAsync(Func await handler(new(req.BiDi, req.Context, req.IsBlocked, req.Navigation, req.RedirectCount, req.Request, req.Timestamp, req.Initiator, req.Intercepts)), @@ -53,7 +53,7 @@ public async Task InterceptResponseAsync(Func await handler(new(res.BiDi, res.Context, res.IsBlocked, res.Navigation, res.RedirectCount, res.Request, res.Timestamp, res.Response, res.Intercepts)), @@ -71,7 +71,7 @@ public async Task InterceptAuthAsync(Func h var interceptResult = await networkModule.AddInterceptAsync([InterceptPhase.AuthRequired], addInterceptOptions).ConfigureAwait(false); - Interception interception = new(context.BiDi, interceptResult.Intercept); + Interception interception = new(networkModule, interceptResult.Intercept); await interception.OnAuthRequiredAsync( async auth => await handler(new(auth.BiDi, auth.Context, auth.IsBlocked, auth.Navigation, auth.RedirectCount, auth.Request, auth.Timestamp, auth.Response, auth.Intercepts)), diff --git a/dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs b/dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs index 0c3e44db81e0b..ed6972de3230b 100644 --- a/dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs +++ b/dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs @@ -100,10 +100,10 @@ public async Task SetGeolocationPositionErrorOverr return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, _jsonContext.SetGeolocationOverrideCommand, _jsonContext.SetGeolocationOverrideResult).ConfigureAwait(false); } - protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) + protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions) { - jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(BiDi)); - jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(bidi)); + jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(bidi)); _jsonContext = new EmulationJsonSerializerContext(jsonSerializerOptions); } diff --git a/dotnet/src/webdriver/BiDi/Input/InputModule.cs b/dotnet/src/webdriver/BiDi/Input/InputModule.cs index e625f32cf6d3f..e18e9d7d44c37 100644 --- a/dotnet/src/webdriver/BiDi/Input/InputModule.cs +++ b/dotnet/src/webdriver/BiDi/Input/InputModule.cs @@ -61,10 +61,10 @@ public async Task OnFileDialogOpenedAsync(Action h return await Broker.SubscribeAsync("input.fileDialogOpened", handler, options, _jsonContext.FileDialogInfo).ConfigureAwait(false); } - protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) + protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions) { - jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(BiDi)); - jsonSerializerOptions.Converters.Add(new HandleConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(bidi)); + jsonSerializerOptions.Converters.Add(new HandleConverter(bidi)); _jsonContext = new InputJsonSerializerContext(jsonSerializerOptions); } diff --git a/dotnet/src/webdriver/BiDi/Log/LogModule.cs b/dotnet/src/webdriver/BiDi/Log/LogModule.cs index 34da57d0330ee..9f944cf17e525 100644 --- a/dotnet/src/webdriver/BiDi/Log/LogModule.cs +++ b/dotnet/src/webdriver/BiDi/Log/LogModule.cs @@ -39,12 +39,12 @@ public async Task OnEntryAddedAsync(Action handler, Subs return await Broker.SubscribeAsync("log.entryAdded", handler, options, _jsonContext.LogEntry).ConfigureAwait(false); } - protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) + protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions) { - jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(BiDi)); - jsonSerializerOptions.Converters.Add(new RealmConverter(BiDi)); - jsonSerializerOptions.Converters.Add(new InternalIdConverter(BiDi)); - jsonSerializerOptions.Converters.Add(new HandleConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(bidi)); + jsonSerializerOptions.Converters.Add(new RealmConverter(bidi)); + jsonSerializerOptions.Converters.Add(new InternalIdConverter(bidi)); + jsonSerializerOptions.Converters.Add(new HandleConverter(bidi)); _jsonContext = new LogJsonSerializerContext(jsonSerializerOptions); } diff --git a/dotnet/src/webdriver/BiDi/Module.cs b/dotnet/src/webdriver/BiDi/Module.cs index 0bbba4773cf61..b4fe5a2a0e3f1 100644 --- a/dotnet/src/webdriver/BiDi/Module.cs +++ b/dotnet/src/webdriver/BiDi/Module.cs @@ -23,22 +23,19 @@ namespace OpenQA.Selenium.BiDi; public abstract class Module { - protected BiDi BiDi { get; private set; } = null!; - protected Broker Broker { get; private set; } = null!; - protected abstract void Initialize(JsonSerializerOptions jsonSerializerOptions); + protected abstract void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions); internal static TModule Create(BiDi bidi, Broker broker, JsonSerializerOptions jsonSerializerOptions) where TModule : Module, new() { TModule module = new() { - BiDi = bidi, Broker = broker }; - module.Initialize(jsonSerializerOptions); + module.Initialize(bidi, jsonSerializerOptions); return module; } diff --git a/dotnet/src/webdriver/BiDi/Network/NetworkModule.HighLevel.cs b/dotnet/src/webdriver/BiDi/Network/NetworkModule.HighLevel.cs index 54324e11cf8f2..e95f121cbb8db 100644 --- a/dotnet/src/webdriver/BiDi/Network/NetworkModule.HighLevel.cs +++ b/dotnet/src/webdriver/BiDi/Network/NetworkModule.HighLevel.cs @@ -30,7 +30,7 @@ public async Task InterceptRequestAsync(Func await handler(new(req.BiDi, req.Context, req.IsBlocked, req.Navigation, req.RedirectCount, req.Request, req.Timestamp, req.Initiator, req.Intercepts))).ConfigureAwait(false); @@ -41,7 +41,7 @@ public async Task InterceptResponseAsync(Func await handler(new(res.BiDi, res.Context, res.IsBlocked, res.Navigation, res.RedirectCount, res.Request, res.Timestamp, res.Response, res.Intercepts))).ConfigureAwait(false); @@ -52,7 +52,7 @@ public async Task InterceptAuthAsync(Func h { var interceptResult = await AddInterceptAsync([InterceptPhase.AuthRequired], options).ConfigureAwait(false); - Interception interception = new(BiDi, interceptResult.Intercept); + Interception interception = new(this, interceptResult.Intercept); await interception.OnAuthRequiredAsync(async auth => await handler(new(auth.BiDi, auth.Context, auth.IsBlocked, auth.Navigation, auth.RedirectCount, auth.Request, auth.Timestamp, auth.Response, auth.Intercepts))).ConfigureAwait(false); @@ -128,7 +128,7 @@ public Task ContinueAsync(ContinueWithAuthCancelCredentialsOptions? options = nu } } -public sealed record Interception(BiDi BiDi, Intercept Intercept) : IAsyncDisposable +public sealed record Interception(NetworkModule Network, Intercept Intercept) : IAsyncDisposable { IList OnBeforeRequestSentSubscriptions { get; } = []; IList OnResponseStartedSubscriptions { get; } = []; @@ -136,7 +136,7 @@ public sealed record Interception(BiDi BiDi, Intercept Intercept) : IAsyncDispos public async Task RemoveAsync() { - await BiDi.Network.RemoveInterceptAsync(Intercept).ConfigureAwait(false); + await Network.RemoveInterceptAsync(Intercept).ConfigureAwait(false); foreach (var subscription in OnBeforeRequestSentSubscriptions) { @@ -156,21 +156,21 @@ public async Task RemoveAsync() public async Task OnBeforeRequestSentAsync(Func handler, SubscriptionOptions? options = null) { - var subscription = await BiDi.Network.OnBeforeRequestSentAsync(async args => await Filter(args, handler), options).ConfigureAwait(false); + var subscription = await Network.OnBeforeRequestSentAsync(async args => await Filter(args, handler), options).ConfigureAwait(false); OnBeforeRequestSentSubscriptions.Add(subscription); } public async Task OnResponseStartedAsync(Func handler, SubscriptionOptions? options = null) { - var subscription = await BiDi.Network.OnResponseStartedAsync(async args => await Filter(args, handler), options).ConfigureAwait(false); + var subscription = await Network.OnResponseStartedAsync(async args => await Filter(args, handler), options).ConfigureAwait(false); OnResponseStartedSubscriptions.Add(subscription); } public async Task OnAuthRequiredAsync(Func handler, SubscriptionOptions? options = null) { - var subscription = await BiDi.Network.OnAuthRequiredAsync(async args => await Filter(args, handler), options).ConfigureAwait(false); + var subscription = await Network.OnAuthRequiredAsync(async args => await Filter(args, handler), options).ConfigureAwait(false); OnAuthRequiredSubscriptions.Add(subscription); } diff --git a/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs b/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs index e9408335ce0a5..ed1fd55da7e4e 100644 --- a/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs +++ b/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs @@ -174,12 +174,12 @@ public async Task OnAuthRequiredAsync(Action SetPermissionAsync(PermissionDescriptor d return await Broker.ExecuteCommandAsync(new SetPermissionCommand(@params), options, _jsonContext.SetPermissionCommand, _jsonContext.SetPermissionResult).ConfigureAwait(false); } - protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) + protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions) { - jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(bidi)); _jsonContext = new PermissionsJsonSerializerContext(jsonSerializerOptions); } diff --git a/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs index ca935daa2a29c..fb86fa4affb76 100644 --- a/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs @@ -117,13 +117,13 @@ public async Task OnRealmDestroyedAsync(Action EndAsync(EndOptions? options = null) return await Broker.ExecuteCommandAsync(new EndCommand(), options, _jsonContext.EndCommand, _jsonContext.EndResult).ConfigureAwait(false); } - protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) + protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions) { - jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(BiDi)); - jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(bidi)); + jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(bidi)); _jsonContext = new SessionJsonSerializerContext(jsonSerializerOptions); } diff --git a/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs b/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs index ed53de3ef7d51..bf4f24b05de34 100644 --- a/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs +++ b/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs @@ -49,10 +49,10 @@ public async Task SetCookieAsync(PartialCookie cookie, SetCooki return await Broker.ExecuteCommandAsync(new SetCookieCommand(@params), options, _jsonContext.SetCookieCommand, _jsonContext.SetCookieResult).ConfigureAwait(false); } - protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) + protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions) { - jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(BiDi)); - jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(bidi)); + jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(bidi)); _jsonContext = new StorageJsonSerializerContext(jsonSerializerOptions); } diff --git a/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs b/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs index 5555a7db4ea2a..571b93742c054 100644 --- a/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs +++ b/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs @@ -42,9 +42,9 @@ public async Task UninstallAsync(Extension extension, Uninstall return await Broker.ExecuteCommandAsync(new UninstallCommand(@params), options, _jsonContext.UninstallCommand, _jsonContext.UninstallResult).ConfigureAwait(false); } - protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) + protected override void Initialize(BiDi bidi, JsonSerializerOptions jsonSerializerOptions) { - jsonSerializerOptions.Converters.Add(new WebExtensionConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new WebExtensionConverter(bidi)); _jsonContext = new WebExtensionJsonSerializerContext(jsonSerializerOptions); }