From 42127300fe04bd2863c0a3e0765372100bb3d2f2 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 16 Nov 2025 16:51:16 +0300 Subject: [PATCH 1/4] [dotnet] [bidi] Fix events subscription possibilities --- dotnet/src/webdriver/BiDi/Broker.cs | 42 ++-------- .../BiDi/BrowsingContext/BrowsingContext.cs | 80 +++++++++---------- .../BrowsingContextLogModule.cs | 10 +-- .../BrowsingContext/BrowsingContextModule.cs | 56 ++++++------- .../BrowsingContextNetworkModule.cs | 50 ++++++------ .../BiDi/Session/SubscribeCommand.cs | 2 + dotnet/src/webdriver/BiDi/Subscription.cs | 20 +++-- 7 files changed, 122 insertions(+), 138 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Broker.cs b/dotnet/src/webdriver/BiDi/Broker.cs index 81726b76631be..6546dba693da6 100644 --- a/dotnet/src/webdriver/BiDi/Broker.cs +++ b/dotnet/src/webdriver/BiDi/Broker.cs @@ -162,26 +162,13 @@ public async Task SubscribeAsync(string eventName, Act var handlers = _eventHandlers.GetOrAdd(eventName, (a) => []); - if (options is BrowsingContextsSubscriptionOptions browsingContextsOptions) - { - var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = browsingContextsOptions.Contexts }).ConfigureAwait(false); - - var eventHandler = new SyncEventHandler(eventName, action, browsingContextsOptions?.Contexts); + var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = options?.Contexts, UserContexts = options?.UserContexts }).ConfigureAwait(false); - handlers.Add(eventHandler); + var eventHandler = new SyncEventHandler(eventName, action); - return new Subscription(subscribeResult.Subscription, this, eventHandler); - } - else - { - var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName]).ConfigureAwait(false); + handlers.Add(eventHandler); - var eventHandler = new SyncEventHandler(eventName, action); - - handlers.Add(eventHandler); - - return new Subscription(subscribeResult.Subscription, this, eventHandler); - } + return new Subscription(subscribeResult.Subscription, this, eventHandler); } public async Task SubscribeAsync(string eventName, Func func, SubscriptionOptions? options, JsonTypeInfo jsonTypeInfo) @@ -191,26 +178,13 @@ public async Task SubscribeAsync(string eventName, Fun var handlers = _eventHandlers.GetOrAdd(eventName, (a) => []); - if (options is BrowsingContextsSubscriptionOptions browsingContextsOptions) - { - var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = browsingContextsOptions.Contexts }).ConfigureAwait(false); - - var eventHandler = new AsyncEventHandler(eventName, func, browsingContextsOptions.Contexts); + var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = options?.Contexts, UserContexts = options?.UserContexts }).ConfigureAwait(false); - handlers.Add(eventHandler); + var eventHandler = new AsyncEventHandler(eventName, func); - return new Subscription(subscribeResult.Subscription, this, eventHandler); - } - else - { - var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName]).ConfigureAwait(false); + handlers.Add(eventHandler); - var eventHandler = new AsyncEventHandler(eventName, func); - - handlers.Add(eventHandler); - - return new Subscription(subscribeResult.Subscription, this, eventHandler); - } + return new Subscription(subscribeResult.Subscription, this, eventHandler); } public async Task UnsubscribeAsync(Subscription subscription) diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs index 46a703f9533db..ffd3cf3acaa66 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs @@ -116,104 +116,104 @@ public Task GetTreeAsync(BrowsingContextGetTreeOptions? options = return BiDi.BrowsingContext.GetTreeAsync(getTreeOptions); } - public Task OnNavigationStartedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnNavigationStartedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationStartedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnNavigationStartedAsync(handler, options.WithContext(this)); } - public Task OnNavigationStartedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnNavigationStartedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationStartedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnNavigationStartedAsync(handler, options.WithContext(this)); } - public Task OnFragmentNavigatedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnFragmentNavigatedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnFragmentNavigatedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnFragmentNavigatedAsync(handler, options.WithContext(this)); } - public Task OnFragmentNavigatedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnFragmentNavigatedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnFragmentNavigatedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnFragmentNavigatedAsync(handler, options.WithContext(this)); } - public Task OnHistoryUpdatedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnHistoryUpdatedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnHistoryUpdatedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnHistoryUpdatedAsync(handler, options.WithContext(this)); } - public Task OnHistoryUpdatedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnHistoryUpdatedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnHistoryUpdatedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnHistoryUpdatedAsync(handler, options.WithContext(this)); } - public Task OnDomContentLoadedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnDomContentLoadedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, options.WithContext(this)); } - public Task OnDomContentLoadedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnDomContentLoadedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnDomContentLoadedAsync(handler, options.WithContext(this)); } - public Task OnLoadAsync(Action handler, SubscriptionOptions? options = null) + public Task OnLoadAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnLoadAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnLoadAsync(handler, options.WithContext(this)); } - public Task OnLoadAsync(Func handler, SubscriptionOptions? options = null) + public Task OnLoadAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnLoadAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnLoadAsync(handler, options.WithContext(this)); } - public Task OnDownloadWillBeginAsync(Action handler, SubscriptionOptions? options = null) + public Task OnDownloadWillBeginAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDownloadWillBeginAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnDownloadWillBeginAsync(handler, options.WithContext(this)); } - public Task OnDownloadWillBeginAsync(Func handler, SubscriptionOptions? options = null) + public Task OnDownloadWillBeginAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDownloadWillBeginAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnDownloadWillBeginAsync(handler, options.WithContext(this)); } - public Task OnDownloadEndAsync(Action handler, SubscriptionOptions? options = null) + public Task OnDownloadEndAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDownloadEndAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnDownloadEndAsync(handler, options.WithContext(this)); } - public Task OnDownloadEndAsync(Func handler, SubscriptionOptions? options = null) + public Task OnDownloadEndAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnDownloadEndAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnDownloadEndAsync(handler, options.WithContext(this)); } - public Task OnNavigationAbortedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnNavigationAbortedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationAbortedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnNavigationAbortedAsync(handler, options.WithContext(this)); } - public Task OnNavigationAbortedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnNavigationAbortedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationAbortedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnNavigationAbortedAsync(handler, options.WithContext(this)); } - public Task OnNavigationFailedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnNavigationFailedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationFailedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnNavigationFailedAsync(handler, options.WithContext(this)); } - public Task OnNavigationFailedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnNavigationFailedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationFailedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnNavigationFailedAsync(handler, options.WithContext(this)); } - public Task OnNavigationCommittedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnNavigationCommittedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationCommittedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnNavigationCommittedAsync(handler, options.WithContext(this)); } - public Task OnNavigationCommittedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnNavigationCommittedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return BiDi.BrowsingContext.OnNavigationCommittedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [this] }); + return BiDi.BrowsingContext.OnNavigationCommittedAsync(handler, options.WithContext(this)); } public override bool Equals(object? obj) diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs index d98fe04948631..1014993f33023 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs @@ -18,14 +18,14 @@ // using OpenQA.Selenium.BiDi.Log; -using System.Threading.Tasks; using System; +using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.BrowsingContext; public sealed class BrowsingContextLogModule(BrowsingContext context, LogModule logModule) { - public Task OnEntryAddedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnEntryAddedAsync(Func handler, ContextSubscriptionOptions? options = null) { return logModule.OnEntryAddedAsync(async args => { @@ -33,10 +33,10 @@ public Task OnEntryAddedAsync(Func handler, Su { await handler(args).ConfigureAwait(false); } - }, options); + }, options.WithContext(context)); } - public Task OnEntryAddedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnEntryAddedAsync(Action handler, ContextSubscriptionOptions? options = null) { return logModule.OnEntryAddedAsync(args => { @@ -44,6 +44,6 @@ public Task OnEntryAddedAsync(Action handler, Subscr { handler(args); } - }, options); + }, options.WithContext(context)); } } diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs index 72f2d5d995d0d..636634bb0b1d2 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs @@ -108,142 +108,142 @@ public async Task HandleUserPromptAsync(BrowsingContext return await Broker.ExecuteCommandAsync(new HandleUserPromptCommand(@params), options, JsonContext.HandleUserPromptCommand, JsonContext.HandleUserPromptResult).ConfigureAwait(false); } - public async Task OnNavigationStartedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnNavigationStartedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.navigationStarted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnNavigationStartedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnNavigationStartedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.navigationStarted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnFragmentNavigatedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnFragmentNavigatedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.fragmentNavigated", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnFragmentNavigatedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnFragmentNavigatedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.fragmentNavigated", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnHistoryUpdatedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnHistoryUpdatedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.historyUpdated", handler, options, JsonContext.HistoryUpdatedEventArgs).ConfigureAwait(false); } - public async Task OnHistoryUpdatedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnHistoryUpdatedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.historyUpdated", handler, options, JsonContext.HistoryUpdatedEventArgs).ConfigureAwait(false); } - public async Task OnDomContentLoadedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnDomContentLoadedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.domContentLoaded", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnDomContentLoadedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnDomContentLoadedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.domContentLoaded", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnLoadAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnLoadAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.load", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnLoadAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnLoadAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.load", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnDownloadWillBeginAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnDownloadWillBeginAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.downloadWillBegin", handler, options, JsonContext.DownloadWillBeginEventArgs).ConfigureAwait(false); } - public async Task OnDownloadWillBeginAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnDownloadWillBeginAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.downloadWillBegin", handler, options, JsonContext.DownloadWillBeginEventArgs).ConfigureAwait(false); } - public async Task OnDownloadEndAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnDownloadEndAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.downloadEnd", handler, options, JsonContext.DownloadEndEventArgs).ConfigureAwait(false); } - public async Task OnDownloadEndAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnDownloadEndAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.downloadEnd", handler, options, JsonContext.DownloadEndEventArgs).ConfigureAwait(false); } - public async Task OnNavigationAbortedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnNavigationAbortedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.navigationAborted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnNavigationAbortedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnNavigationAbortedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.navigationAborted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnNavigationFailedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnNavigationFailedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.navigationFailed", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnNavigationFailedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnNavigationFailedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.navigationFailed", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnNavigationCommittedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnNavigationCommittedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.navigationCommitted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnNavigationCommittedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnNavigationCommittedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.navigationCommitted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } - public async Task OnContextCreatedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnContextCreatedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.contextCreated", handler, options, JsonContext.BrowsingContextInfo).ConfigureAwait(false); } - public async Task OnContextCreatedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnContextCreatedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.contextCreated", handler, options, JsonContext.BrowsingContextInfo).ConfigureAwait(false); } - public async Task OnContextDestroyedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnContextDestroyedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.contextDestroyed", handler, options, JsonContext.BrowsingContextInfo).ConfigureAwait(false); } - public async Task OnContextDestroyedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnContextDestroyedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.contextDestroyed", handler, options, JsonContext.BrowsingContextInfo).ConfigureAwait(false); } - public async Task OnUserPromptOpenedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnUserPromptOpenedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.userPromptOpened", handler, options, JsonContext.UserPromptOpenedEventArgs).ConfigureAwait(false); } - public async Task OnUserPromptOpenedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnUserPromptOpenedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.userPromptOpened", handler, options, JsonContext.UserPromptOpenedEventArgs).ConfigureAwait(false); } - public async Task OnUserPromptClosedAsync(Func handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnUserPromptClosedAsync(Func handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.userPromptClosed", handler, options, JsonContext.UserPromptClosedEventArgs).ConfigureAwait(false); } - public async Task OnUserPromptClosedAsync(Action handler, BrowsingContextsSubscriptionOptions? options = null) + public async Task OnUserPromptClosedAsync(Action handler, SubscriptionOptions? options = null) { return await Broker.SubscribeAsync("browsingContext.userPromptClosed", handler, options, JsonContext.UserPromptClosedEventArgs).ConfigureAwait(false); } diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs index 039a6644f03dc..2caf06632cc9c 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs @@ -17,9 +17,9 @@ // under the License. // -using System.Threading.Tasks; -using System; using OpenQA.Selenium.BiDi.Network; +using System; +using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.BrowsingContext; @@ -36,7 +36,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)), - new BrowsingContextsSubscriptionOptions(null) { Contexts = [context] }).ConfigureAwait(false); + new() { Contexts = [context] }).ConfigureAwait(false); return intercept; } @@ -52,7 +52,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)), - new BrowsingContextsSubscriptionOptions(null) { Contexts = [context] }).ConfigureAwait(false); + new() { Contexts = [context] }).ConfigureAwait(false); return intercept; } @@ -68,7 +68,7 @@ public async Task InterceptAuthAsync(Func hand await intercept.OnAuthRequiredAsync( async auth => await handler(new(auth.BiDi, auth.Context, auth.IsBlocked, auth.Navigation, auth.RedirectCount, auth.Request, auth.Timestamp, auth.Response, auth.Intercepts)), - new BrowsingContextsSubscriptionOptions(null) { Contexts = [context] }).ConfigureAwait(false); + new() { Contexts = [context] }).ConfigureAwait(false); return intercept; } @@ -83,54 +83,54 @@ public Task SetCacheBehaviorAsync(CacheBehavior behavior return networkModule.SetCacheBehaviorAsync(behavior, setCacheBehaviorOptions); } - public Task OnBeforeRequestSentAsync(Func handler, SubscriptionOptions? options = null) + public Task OnBeforeRequestSentAsync(Func handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnBeforeRequestSentAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }); + return networkModule.OnBeforeRequestSentAsync(handler, options.WithContext(context)); } - public Task OnBeforeRequestSentAsync(Action handler, SubscriptionOptions? options = null) + public Task OnBeforeRequestSentAsync(Action handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnBeforeRequestSentAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }); + return networkModule.OnBeforeRequestSentAsync(handler, options.WithContext(context)); } - public Task OnResponseStartedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnResponseStartedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnResponseStartedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }); + return networkModule.OnResponseStartedAsync(handler, options.WithContext(context)); } - public Task OnResponseStartedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnResponseStartedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnResponseStartedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }); + return networkModule.OnResponseStartedAsync(handler, options.WithContext(context)); } - public Task OnResponseCompletedAsync(Func handler, SubscriptionOptions? options = null) + public Task OnResponseCompletedAsync(Func handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnResponseCompletedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }); + return networkModule.OnResponseCompletedAsync(handler, options.WithContext(context)); } - public Task OnResponseCompletedAsync(Action handler, SubscriptionOptions? options = null) + public Task OnResponseCompletedAsync(Action handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnResponseCompletedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }); + return networkModule.OnResponseCompletedAsync(handler, options.WithContext(context)); } - public Task OnFetchErrorAsync(Func handler, SubscriptionOptions? options = null) + public Task OnFetchErrorAsync(Func handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnFetchErrorAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }); + return networkModule.OnFetchErrorAsync(handler, options.WithContext(context)); } - public Task OnFetchErrorAsync(Action handler, SubscriptionOptions? options = null) + public Task OnFetchErrorAsync(Action handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnFetchErrorAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }); + return networkModule.OnFetchErrorAsync(handler, options.WithContext(context)); } - public Task OnAuthRequiredAsync(Func handler, SubscriptionOptions? options = null) + public Task OnAuthRequiredAsync(Func handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnAuthRequiredAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }); + return networkModule.OnAuthRequiredAsync(handler, options.WithContext(context)); } - public Task OnAuthRequiredAsync(Action handler, SubscriptionOptions? options = null) + public Task OnAuthRequiredAsync(Action handler, ContextSubscriptionOptions? options = null) { - return networkModule.OnAuthRequiredAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }); + return networkModule.OnAuthRequiredAsync(handler, options.WithContext(context)); } } diff --git a/dotnet/src/webdriver/BiDi/Session/SubscribeCommand.cs b/dotnet/src/webdriver/BiDi/Session/SubscribeCommand.cs index 8336fab962053..ffb413be32e9c 100644 --- a/dotnet/src/webdriver/BiDi/Session/SubscribeCommand.cs +++ b/dotnet/src/webdriver/BiDi/Session/SubscribeCommand.cs @@ -29,6 +29,8 @@ internal sealed record SubscribeParameters(IEnumerable Events, IEnumerab public sealed class SubscribeOptions : CommandOptions { public IEnumerable? Contexts { get; set; } + + public IEnumerable? UserContexts { get; set; } } internal sealed record SubscribeResult(Subscription Subscription) : EmptyResult; diff --git a/dotnet/src/webdriver/BiDi/Subscription.cs b/dotnet/src/webdriver/BiDi/Subscription.cs index 80fa4b8e03050..0266aa08e35aa 100644 --- a/dotnet/src/webdriver/BiDi/Subscription.cs +++ b/dotnet/src/webdriver/BiDi/Subscription.cs @@ -51,15 +51,23 @@ public async ValueTask DisposeAsync() public class SubscriptionOptions { + public IEnumerable? Contexts { get; set; } + + public IEnumerable? UserContexts { get; set; } + public TimeSpan? Timeout { get; set; } } -public class BrowsingContextsSubscriptionOptions : SubscriptionOptions +public class ContextSubscriptionOptions { - public BrowsingContextsSubscriptionOptions(SubscriptionOptions? options) - { - Timeout = options?.Timeout; - } + public TimeSpan? Timeout { get; set; } +} - public IEnumerable? Contexts { get; set; } +internal static class ContextSubscriptionOptionsExtensions +{ + public static SubscriptionOptions WithContext(this ContextSubscriptionOptions? options, BrowsingContext.BrowsingContext context) => new() + { + Contexts = [context], + Timeout = options?.Timeout + }; } From aa9777b3f32dcd6bfd0644b50146d4ca0232a838 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 16 Nov 2025 17:02:19 +0300 Subject: [PATCH 2/4] Scoped internal event handler --- dotnet/src/webdriver/BiDi/Broker.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Broker.cs b/dotnet/src/webdriver/BiDi/Broker.cs index 6546dba693da6..a4d7a09e99ac1 100644 --- a/dotnet/src/webdriver/BiDi/Broker.cs +++ b/dotnet/src/webdriver/BiDi/Broker.cs @@ -164,7 +164,7 @@ public async Task SubscribeAsync(string eventName, Act var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = options?.Contexts, UserContexts = options?.UserContexts }).ConfigureAwait(false); - var eventHandler = new SyncEventHandler(eventName, action); + var eventHandler = new SyncEventHandler(eventName, action, options?.Contexts); handlers.Add(eventHandler); @@ -180,7 +180,7 @@ public async Task SubscribeAsync(string eventName, Fun var subscribeResult = await _bidi.SessionModule.SubscribeAsync([eventName], new() { Contexts = options?.Contexts, UserContexts = options?.UserContexts }).ConfigureAwait(false); - var eventHandler = new AsyncEventHandler(eventName, func); + var eventHandler = new AsyncEventHandler(eventName, func, options?.Contexts); handlers.Add(eventHandler); From a8499a6323d9ee8ce481a4e98dad0461b53f603c Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 16 Nov 2025 17:50:21 +0300 Subject: [PATCH 3/4] Fix tests --- .../BiDi/BrowsingContext/BrowsingContextLogModule.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs index 1014993f33023..4d51f3dbc99dd 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs @@ -33,7 +33,7 @@ public Task OnEntryAddedAsync(Func handler, Co { await handler(args).ConfigureAwait(false); } - }, options.WithContext(context)); + }, new SubscriptionOptions() { Timeout = options?.Timeout }); // special case, don't scopoe to context, awaiting https://github.com/w3c/webdriver-bidi/issues/1032 } public Task OnEntryAddedAsync(Action handler, ContextSubscriptionOptions? options = null) @@ -44,6 +44,6 @@ public Task OnEntryAddedAsync(Action handler, Contex { handler(args); } - }, options.WithContext(context)); + }, new SubscriptionOptions() { Timeout = options?.Timeout }); // special case, don't scopoe to context, awaiting https://github.com/w3c/webdriver-bidi/issues/1032 } } From 4da532f2585f3b86a8f24553c65abbce73ab7bf8 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 16 Nov 2025 17:51:05 +0300 Subject: [PATCH 4/4] typo --- .../BiDi/BrowsingContext/BrowsingContextLogModule.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs index 4d51f3dbc99dd..24954256597b5 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs @@ -33,7 +33,7 @@ public Task OnEntryAddedAsync(Func handler, Co { await handler(args).ConfigureAwait(false); } - }, new SubscriptionOptions() { Timeout = options?.Timeout }); // special case, don't scopoe to context, awaiting https://github.com/w3c/webdriver-bidi/issues/1032 + }, new SubscriptionOptions() { Timeout = options?.Timeout }); // special case, don't scope to context, awaiting https://github.com/w3c/webdriver-bidi/issues/1032 } public Task OnEntryAddedAsync(Action handler, ContextSubscriptionOptions? options = null) @@ -44,6 +44,6 @@ public Task OnEntryAddedAsync(Action handler, Contex { handler(args); } - }, new SubscriptionOptions() { Timeout = options?.Timeout }); // special case, don't scopoe to context, awaiting https://github.com/w3c/webdriver-bidi/issues/1032 + }, new SubscriptionOptions() { Timeout = options?.Timeout }); // special case, don't scope to context, awaiting https://github.com/w3c/webdriver-bidi/issues/1032 } }