diff --git a/dotnet/src/webdriver/BiDi/BiDi.cs b/dotnet/src/webdriver/BiDi/BiDi.cs index a6593ab29db86..5b40993aa26c8 100644 --- a/dotnet/src/webdriver/BiDi/BiDi.cs +++ b/dotnet/src/webdriver/BiDi/BiDi.cs @@ -17,6 +17,7 @@ // under the License. // +using OpenQA.Selenium.BiDi.Json; using OpenQA.Selenium.BiDi.Json.Converters; using System; using System.Collections.Concurrent; @@ -29,6 +30,8 @@ namespace OpenQA.Selenium.BiDi; public sealed class BiDi : IAsyncDisposable { + private readonly BiDiJsonSerializerContext _jsonContext; + private readonly ConcurrentDictionary _modules = new(); private BiDi(string url) @@ -36,6 +39,8 @@ private BiDi(string url) var uri = new Uri(url); Broker = new Broker(this, uri); + + _jsonContext = new BiDiJsonSerializerContext(GetJsonOptions()); } internal Session.SessionModule SessionModule => AsModule(); @@ -85,7 +90,7 @@ public async ValueTask DisposeAsync() public T AsModule() where T : Module, new() { - return (T)_modules.GetOrAdd(typeof(T), _ => Module.Create(this, Broker, GetJsonOptions())); + return (T)_modules.GetOrAdd(typeof(T), _ => Module.Create(this, Broker, _jsonContext)); } private Broker Broker { get; } diff --git a/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs b/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs index a1427163d6e24..f85392fc548aa 100644 --- a/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs +++ b/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs @@ -17,43 +17,39 @@ // under the License. // -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Browser; public sealed class BrowserModule : Module { - private BrowserJsonSerializerContext _jsonContext = null!; - public async Task CloseAsync(CloseOptions? options = null) { - return await Broker.ExecuteCommandAsync(new CloseCommand(), options, _jsonContext.CloseCommand, _jsonContext.CloseResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new CloseCommand(), options, JsonContext.CloseCommand, JsonContext.CloseResult).ConfigureAwait(false); } public async Task CreateUserContextAsync(CreateUserContextOptions? options = null) { var @params = new CreateUserContextParameters(options?.AcceptInsecureCerts, options?.Proxy, options?.UnhandledPromptBehavior); - return await Broker.ExecuteCommandAsync(new CreateUserContextCommand(@params), options, _jsonContext.CreateUserContextCommand, _jsonContext.CreateUserContextResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new CreateUserContextCommand(@params), options, JsonContext.CreateUserContextCommand, JsonContext.CreateUserContextResult).ConfigureAwait(false); } public async Task GetUserContextsAsync(GetUserContextsOptions? options = null) { - return await Broker.ExecuteCommandAsync(new GetUserContextsCommand(), options, _jsonContext.GetUserContextsCommand, _jsonContext.GetUserContextsResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new GetUserContextsCommand(), options, JsonContext.GetUserContextsCommand, JsonContext.GetUserContextsResult).ConfigureAwait(false); } public async Task RemoveUserContextAsync(UserContext userContext, RemoveUserContextOptions? options = null) { var @params = new RemoveUserContextParameters(userContext); - return await Broker.ExecuteCommandAsync(new RemoveUserContextCommand(@params), options, _jsonContext.RemoveUserContextCommand, _jsonContext.RemoveUserContextResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new RemoveUserContextCommand(@params), options, JsonContext.RemoveUserContextCommand, JsonContext.RemoveUserContextResult).ConfigureAwait(false); } public async Task GetClientWindowsAsync(GetClientWindowsOptions? options = null) { - return await Broker.ExecuteCommandAsync(new(), options, _jsonContext.GetClientWindowsCommand, _jsonContext.GetClientWindowsResult + return await Broker.ExecuteCommandAsync(new(), options, JsonContext.GetClientWindowsCommand, JsonContext.GetClientWindowsResult ).ConfigureAwait(false); } @@ -61,39 +57,20 @@ public async Task SetDownloadBehaviorAllowedAsync(str { var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorAllowed(destinationFolder), options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, JsonContext.SetDownloadBehaviorCommand, JsonContext.SetDownloadBehaviorResult).ConfigureAwait(false); } public async Task SetDownloadBehaviorAllowedAsync(SetDownloadBehaviorOptions? options = null) { var @params = new SetDownloadBehaviorParameters(null, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, JsonContext.SetDownloadBehaviorCommand, JsonContext.SetDownloadBehaviorResult).ConfigureAwait(false); } public async Task SetDownloadBehaviorDeniedAsync(SetDownloadBehaviorOptions? options = null) { var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorDenied(), options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult).ConfigureAwait(false); - } - - protected override void Initialize(JsonSerializerOptions options) - { - _jsonContext = new BrowserJsonSerializerContext(options); + return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, JsonContext.SetDownloadBehaviorCommand, JsonContext.SetDownloadBehaviorResult).ConfigureAwait(false); } } - -[JsonSerializable(typeof(CloseCommand))] -[JsonSerializable(typeof(CloseResult))] -[JsonSerializable(typeof(CreateUserContextCommand))] -[JsonSerializable(typeof(CreateUserContextResult))] -[JsonSerializable(typeof(GetUserContextsCommand))] -[JsonSerializable(typeof(GetUserContextsResult))] -[JsonSerializable(typeof(RemoveUserContextCommand))] -[JsonSerializable(typeof(RemoveUserContextResult))] -[JsonSerializable(typeof(GetClientWindowsCommand))] -[JsonSerializable(typeof(GetClientWindowsResult))] -[JsonSerializable(typeof(SetDownloadBehaviorCommand))] -[JsonSerializable(typeof(SetDownloadBehaviorResult))] -internal partial class BrowserJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/src/webdriver/BiDi/Browser/UserContext.cs b/dotnet/src/webdriver/BiDi/Browser/UserContext.cs index 46066271be9e1..3c0ed98242f62 100644 --- a/dotnet/src/webdriver/BiDi/Browser/UserContext.cs +++ b/dotnet/src/webdriver/BiDi/Browser/UserContext.cs @@ -18,25 +18,26 @@ // using System; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Browser; public sealed class UserContext : IEquatable, IAsyncDisposable { - private readonly BiDi _bidi; - - internal UserContext(BiDi bidi, string id) + internal UserContext(string id) { - _bidi = bidi; Id = id; } internal string Id { get; } + [JsonIgnore] + public BiDi BiDi { get; internal set; } + public Task RemoveAsync() { - return _bidi.Browser.RemoveUserContextAsync(this); + return BiDi.Browser.RemoveUserContextAsync(this); } public async ValueTask DisposeAsync() diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs index 24fdc57b0b1bd..41a36bf27fb8e 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs @@ -26,9 +26,8 @@ namespace OpenQA.Selenium.BiDi.BrowsingContext; public sealed class BrowsingContext { - internal BrowsingContext(BiDi bidi, string id) + internal BrowsingContext(string id) { - BiDi = bidi; Id = id; } @@ -41,7 +40,7 @@ internal BrowsingContext(BiDi bidi, string id) internal string Id { get; } [JsonIgnore] - public BiDi BiDi { get; } + public BiDi BiDi { get; internal set; } [JsonIgnore] public BrowsingContextLogModule Log => _logModule ?? Interlocked.CompareExchange(ref _logModule, new BrowsingContextLogModule(this, BiDi.Log), null) ?? _logModule; diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs index 1e6b1ca4e3d70..636634bb0b1d2 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs @@ -18,278 +18,233 @@ // using System; -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.BrowsingContext; public sealed class BrowsingContextModule : Module { - private BrowsingContextJsonSerializerContext _jsonContext = null!; - public async Task CreateAsync(ContextType type, CreateOptions? options = null) { var @params = new CreateParameters(type, options?.ReferenceContext, options?.Background, options?.UserContext); - return await Broker.ExecuteCommandAsync(new CreateCommand(@params), options, _jsonContext.CreateCommand, _jsonContext.CreateResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new CreateCommand(@params), options, JsonContext.CreateCommand, JsonContext.CreateResult).ConfigureAwait(false); } public async Task NavigateAsync(BrowsingContext context, string url, NavigateOptions? options = null) { var @params = new NavigateParameters(context, url, options?.Wait); - return await Broker.ExecuteCommandAsync(new NavigateCommand(@params), options, _jsonContext.NavigateCommand, _jsonContext.NavigateResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new NavigateCommand(@params), options, JsonContext.NavigateCommand, JsonContext.NavigateResult).ConfigureAwait(false); } public async Task ActivateAsync(BrowsingContext context, ActivateOptions? options = null) { var @params = new ActivateParameters(context); - return await Broker.ExecuteCommandAsync(new ActivateCommand(@params), options, _jsonContext.ActivateCommand, _jsonContext.ActivateResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new ActivateCommand(@params), options, JsonContext.ActivateCommand, JsonContext.ActivateResult).ConfigureAwait(false); } public async Task LocateNodesAsync(BrowsingContext context, Locator locator, LocateNodesOptions? options = null) { var @params = new LocateNodesParameters(context, locator, options?.MaxNodeCount, options?.SerializationOptions, options?.StartNodes); - return await Broker.ExecuteCommandAsync(new LocateNodesCommand(@params), options, _jsonContext.LocateNodesCommand, _jsonContext.LocateNodesResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new LocateNodesCommand(@params), options, JsonContext.LocateNodesCommand, JsonContext.LocateNodesResult).ConfigureAwait(false); } public async Task CaptureScreenshotAsync(BrowsingContext context, CaptureScreenshotOptions? options = null) { var @params = new CaptureScreenshotParameters(context, options?.Origin, options?.Format, options?.Clip); - return await Broker.ExecuteCommandAsync(new CaptureScreenshotCommand(@params), options, _jsonContext.CaptureScreenshotCommand, _jsonContext.CaptureScreenshotResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new CaptureScreenshotCommand(@params), options, JsonContext.CaptureScreenshotCommand, JsonContext.CaptureScreenshotResult).ConfigureAwait(false); } public async Task CloseAsync(BrowsingContext context, CloseOptions? options = null) { var @params = new CloseParameters(context, options?.PromptUnload); - return await Broker.ExecuteCommandAsync(new CloseCommand(@params), options, _jsonContext.CloseCommand, _jsonContext.CloseResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new CloseCommand(@params), options, JsonContext.BrowsingContext_CloseCommand, JsonContext.BrowsingContext_CloseResult).ConfigureAwait(false); } public async Task TraverseHistoryAsync(BrowsingContext context, int delta, TraverseHistoryOptions? options = null) { var @params = new TraverseHistoryParameters(context, delta); - return await Broker.ExecuteCommandAsync(new TraverseHistoryCommand(@params), options, _jsonContext.TraverseHistoryCommand, _jsonContext.TraverseHistoryResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new TraverseHistoryCommand(@params), options, JsonContext.TraverseHistoryCommand, JsonContext.TraverseHistoryResult).ConfigureAwait(false); } public async Task ReloadAsync(BrowsingContext context, ReloadOptions? options = null) { var @params = new ReloadParameters(context, options?.IgnoreCache, options?.Wait); - return await Broker.ExecuteCommandAsync(new ReloadCommand(@params), options, _jsonContext.ReloadCommand, _jsonContext.ReloadResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new ReloadCommand(@params), options, JsonContext.ReloadCommand, JsonContext.ReloadResult).ConfigureAwait(false); } public async Task SetViewportAsync(BrowsingContext context, SetViewportOptions? options = null) { var @params = new SetViewportParameters(context, options?.Viewport, options?.DevicePixelRatio); - return await Broker.ExecuteCommandAsync(new SetViewportCommand(@params), options, _jsonContext.SetViewportCommand, _jsonContext.SetViewportResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetViewportCommand(@params), options, JsonContext.SetViewportCommand, JsonContext.SetViewportResult).ConfigureAwait(false); } public async Task GetTreeAsync(GetTreeOptions? options = null) { var @params = new GetTreeParameters(options?.MaxDepth, options?.Root); - return await Broker.ExecuteCommandAsync(new GetTreeCommand(@params), options, _jsonContext.GetTreeCommand, _jsonContext.GetTreeResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new GetTreeCommand(@params), options, JsonContext.GetTreeCommand, JsonContext.GetTreeResult).ConfigureAwait(false); } public async Task PrintAsync(BrowsingContext context, PrintOptions? options = null) { var @params = new PrintParameters(context, options?.Background, options?.Margin, options?.Orientation, options?.Page, options?.PageRanges, options?.Scale, options?.ShrinkToFit); - return await Broker.ExecuteCommandAsync(new PrintCommand(@params), options, _jsonContext.PrintCommand, _jsonContext.PrintResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new PrintCommand(@params), options, JsonContext.PrintCommand, JsonContext.PrintResult).ConfigureAwait(false); } public async Task HandleUserPromptAsync(BrowsingContext context, HandleUserPromptOptions? options = null) { var @params = new HandleUserPromptParameters(context, options?.Accept, options?.UserText); - return await Broker.ExecuteCommandAsync(new HandleUserPromptCommand(@params), options, _jsonContext.HandleUserPromptCommand, _jsonContext.HandleUserPromptResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new HandleUserPromptCommand(@params), options, JsonContext.HandleUserPromptCommand, JsonContext.HandleUserPromptResult).ConfigureAwait(false); } public async Task OnNavigationStartedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.navigationStarted", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.navigationStarted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnNavigationStartedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.navigationStarted", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.navigationStarted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnFragmentNavigatedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.fragmentNavigated", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.fragmentNavigated", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnFragmentNavigatedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.fragmentNavigated", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.fragmentNavigated", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnHistoryUpdatedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.historyUpdated", handler, options, _jsonContext.HistoryUpdatedEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.historyUpdated", handler, options, JsonContext.HistoryUpdatedEventArgs).ConfigureAwait(false); } public async Task OnHistoryUpdatedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.historyUpdated", handler, options, _jsonContext.HistoryUpdatedEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.historyUpdated", handler, options, JsonContext.HistoryUpdatedEventArgs).ConfigureAwait(false); } public async Task OnDomContentLoadedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.domContentLoaded", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.domContentLoaded", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnDomContentLoadedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.domContentLoaded", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.domContentLoaded", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnLoadAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.load", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.load", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnLoadAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.load", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.load", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnDownloadWillBeginAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.downloadWillBegin", handler, options, _jsonContext.DownloadWillBeginEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.downloadWillBegin", handler, options, JsonContext.DownloadWillBeginEventArgs).ConfigureAwait(false); } public async Task OnDownloadWillBeginAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.downloadWillBegin", handler, options, _jsonContext.DownloadWillBeginEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.downloadWillBegin", handler, options, JsonContext.DownloadWillBeginEventArgs).ConfigureAwait(false); } public async Task OnDownloadEndAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.downloadEnd", handler, options, _jsonContext.DownloadEndEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.downloadEnd", handler, options, JsonContext.DownloadEndEventArgs).ConfigureAwait(false); } public async Task OnDownloadEndAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.downloadEnd", handler, options, _jsonContext.DownloadEndEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.downloadEnd", handler, options, JsonContext.DownloadEndEventArgs).ConfigureAwait(false); } public async Task OnNavigationAbortedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.navigationAborted", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.navigationAborted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnNavigationAbortedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.navigationAborted", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.navigationAborted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnNavigationFailedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.navigationFailed", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.navigationFailed", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnNavigationFailedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.navigationFailed", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.navigationFailed", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnNavigationCommittedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.navigationCommitted", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.navigationCommitted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnNavigationCommittedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.navigationCommitted", handler, options, _jsonContext.NavigationInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.navigationCommitted", handler, options, JsonContext.NavigationInfo).ConfigureAwait(false); } public async Task OnContextCreatedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.contextCreated", handler, options, _jsonContext.BrowsingContextInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.contextCreated", handler, options, JsonContext.BrowsingContextInfo).ConfigureAwait(false); } public async Task OnContextCreatedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.contextCreated", handler, options, _jsonContext.BrowsingContextInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.contextCreated", handler, options, JsonContext.BrowsingContextInfo).ConfigureAwait(false); } public async Task OnContextDestroyedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.contextDestroyed", handler, options, _jsonContext.BrowsingContextInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.contextDestroyed", handler, options, JsonContext.BrowsingContextInfo).ConfigureAwait(false); } public async Task OnContextDestroyedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.contextDestroyed", handler, options, _jsonContext.BrowsingContextInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.contextDestroyed", handler, options, JsonContext.BrowsingContextInfo).ConfigureAwait(false); } public async Task OnUserPromptOpenedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.userPromptOpened", handler, options, _jsonContext.UserPromptOpenedEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.userPromptOpened", handler, options, JsonContext.UserPromptOpenedEventArgs).ConfigureAwait(false); } public async Task OnUserPromptOpenedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.userPromptOpened", handler, options, _jsonContext.UserPromptOpenedEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.userPromptOpened", handler, options, JsonContext.UserPromptOpenedEventArgs).ConfigureAwait(false); } public async Task OnUserPromptClosedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.userPromptClosed", handler, options, _jsonContext.UserPromptClosedEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("browsingContext.userPromptClosed", handler, options, JsonContext.UserPromptClosedEventArgs).ConfigureAwait(false); } public async Task OnUserPromptClosedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("browsingContext.userPromptClosed", handler, options, _jsonContext.UserPromptClosedEventArgs).ConfigureAwait(false); - } - - protected override void Initialize(JsonSerializerOptions options) - { - _jsonContext = new BrowsingContextJsonSerializerContext(options); + return await Broker.SubscribeAsync("browsingContext.userPromptClosed", handler, options, JsonContext.UserPromptClosedEventArgs).ConfigureAwait(false); } } - -[JsonSerializable(typeof(ActivateCommand))] -[JsonSerializable(typeof(ActivateResult))] -[JsonSerializable(typeof(CaptureScreenshotCommand))] -[JsonSerializable(typeof(CaptureScreenshotResult))] -[JsonSerializable(typeof(CloseCommand))] -[JsonSerializable(typeof(CloseResult))] -[JsonSerializable(typeof(CreateCommand))] -[JsonSerializable(typeof(CreateResult))] -[JsonSerializable(typeof(GetTreeCommand))] -[JsonSerializable(typeof(GetTreeResult))] -[JsonSerializable(typeof(HandleUserPromptCommand))] -[JsonSerializable(typeof(HandleUserPromptResult))] -[JsonSerializable(typeof(LocateNodesCommand))] -[JsonSerializable(typeof(LocateNodesResult))] -[JsonSerializable(typeof(NavigateCommand))] -[JsonSerializable(typeof(NavigateResult))] -[JsonSerializable(typeof(PrintCommand))] -[JsonSerializable(typeof(PrintResult))] -[JsonSerializable(typeof(ReloadCommand))] -[JsonSerializable(typeof(ReloadResult))] -[JsonSerializable(typeof(SetViewportCommand))] -[JsonSerializable(typeof(SetViewportResult))] -[JsonSerializable(typeof(TraverseHistoryCommand))] -[JsonSerializable(typeof(TraverseHistoryResult))] - -[JsonSerializable(typeof(BrowsingContextInfo))] -[JsonSerializable(typeof(DownloadWillBeginEventArgs))] -[JsonSerializable(typeof(DownloadEndEventArgs))] -[JsonSerializable(typeof(DownloadCanceledEventArgs))] -[JsonSerializable(typeof(DownloadCompleteEventArgs))] -[JsonSerializable(typeof(HistoryUpdatedEventArgs))] -[JsonSerializable(typeof(NavigationInfo))] -[JsonSerializable(typeof(UserPromptOpenedEventArgs))] -[JsonSerializable(typeof(UserPromptClosedEventArgs))] -internal partial class BrowsingContextJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs b/dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs index e4993e9b6209e..5212f3f7c4222 100644 --- a/dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs +++ b/dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs @@ -17,56 +17,52 @@ // under the License. // -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Emulation; public sealed class EmulationModule : Module { - private EmulationJsonSerializerContext _jsonContext = null!; - public async Task SetTimezoneOverrideAsync(string? timezone, SetTimezoneOverrideOptions? options = null) { var @params = new SetTimezoneOverrideParameters(timezone, options?.Contexts, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetTimezoneOverrideCommand(@params), options, _jsonContext.SetTimezoneOverrideCommand, _jsonContext.SetTimezoneOverrideResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetTimezoneOverrideCommand(@params), options, JsonContext.SetTimezoneOverrideCommand, JsonContext.SetTimezoneOverrideResult).ConfigureAwait(false); } public async Task SetUserAgentOverrideAsync(string? userAgent, SetUserAgentOverrideOptions? options = null) { var @params = new SetUserAgentOverrideParameters(userAgent, options?.Contexts, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetUserAgentOverrideCommand(@params), options, _jsonContext.SetUserAgentOverrideCommand, _jsonContext.SetUserAgentOverrideResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetUserAgentOverrideCommand(@params), options, JsonContext.SetUserAgentOverrideCommand, JsonContext.SetUserAgentOverrideResult).ConfigureAwait(false); } public async Task SetLocaleOverrideAsync(string? locale, SetLocaleOverrideOptions? options = null) { var @params = new SetLocaleOverrideParameters(locale, options?.Contexts, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetLocaleOverrideCommand(@params), options, _jsonContext.SetLocaleOverrideCommand, _jsonContext.SetLocaleOverrideResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetLocaleOverrideCommand(@params), options, JsonContext.SetLocaleOverrideCommand, JsonContext.SetLocaleOverrideResult).ConfigureAwait(false); } public async Task SetForcedColorsModeThemeOverrideAsync(ForcedColorsModeTheme? theme, SetForcedColorsModeThemeOverrideOptions? options = null) { var @params = new SetForcedColorsModeThemeOverrideParameters(theme, options?.Contexts, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetForcedColorsModeThemeOverrideCommand(@params), options, _jsonContext.SetForcedColorsModeThemeOverrideCommand, _jsonContext.SetForcedColorsModeThemeOverrideResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetForcedColorsModeThemeOverrideCommand(@params), options, JsonContext.SetForcedColorsModeThemeOverrideCommand, JsonContext.SetForcedColorsModeThemeOverrideResult).ConfigureAwait(false); } public async Task SetScriptingEnabledAsync(bool? enabled, SetScriptingEnabledOptions? options = null) { var @params = new SetScriptingEnabledParameters(enabled, options?.Contexts, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetScriptingEnabledCommand(@params), options, _jsonContext.SetScriptingEnabledCommand, _jsonContext.SetScriptingEnabledResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetScriptingEnabledCommand(@params), options, JsonContext.SetScriptingEnabledCommand, JsonContext.SetScriptingEnabledResult).ConfigureAwait(false); } public async Task SetScreenOrientationOverrideAsync(ScreenOrientation? screenOrientation, SetScreenOrientationOverrideOptions? options = null) { var @params = new SetScreenOrientationOverrideParameters(screenOrientation, options?.Contexts, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetScreenOrientationOverrideCommand(@params), options, _jsonContext.SetScreenOrientationOverrideCommand, _jsonContext.SetScreenOrientationOverrideResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetScreenOrientationOverrideCommand(@params), options, JsonContext.SetScreenOrientationOverrideCommand, JsonContext.SetScreenOrientationOverrideResult).ConfigureAwait(false); } public async Task SetGeolocationCoordinatesOverrideAsync(double latitude, double longitude, SetGeolocationCoordinatesOverrideOptions? options = null) @@ -75,41 +71,20 @@ public async Task SetGeolocationCoordinatesOverrid var @params = new SetGeolocationOverrideCoordinatesParameters(coordinates, options?.Contexts, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, _jsonContext.SetGeolocationOverrideCommand, _jsonContext.SetGeolocationOverrideResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, JsonContext.SetGeolocationOverrideCommand, JsonContext.SetGeolocationOverrideResult).ConfigureAwait(false); } public async Task SetGeolocationCoordinatesOverrideAsync(SetGeolocationOverrideOptions? options = null) { var @params = new SetGeolocationOverrideCoordinatesParameters(null, options?.Contexts, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, _jsonContext.SetGeolocationOverrideCommand, _jsonContext.SetGeolocationOverrideResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, JsonContext.SetGeolocationOverrideCommand, JsonContext.SetGeolocationOverrideResult).ConfigureAwait(false); } public async Task SetGeolocationPositionErrorOverrideAsync(SetGeolocationPositionErrorOverrideOptions? options = null) { var @params = new SetGeolocationOverridePositionErrorParameters(new GeolocationPositionError(), options?.Contexts, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, _jsonContext.SetGeolocationOverrideCommand, _jsonContext.SetGeolocationOverrideResult).ConfigureAwait(false); - } - - protected override void Initialize(JsonSerializerOptions options) - { - _jsonContext = new EmulationJsonSerializerContext(options); + return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, JsonContext.SetGeolocationOverrideCommand, JsonContext.SetGeolocationOverrideResult).ConfigureAwait(false); } } - -[JsonSerializable(typeof(SetTimezoneOverrideCommand))] -[JsonSerializable(typeof(SetTimezoneOverrideResult))] -[JsonSerializable(typeof(SetUserAgentOverrideCommand))] -[JsonSerializable(typeof(SetUserAgentOverrideResult))] -[JsonSerializable(typeof(SetLocaleOverrideCommand))] -[JsonSerializable(typeof(SetLocaleOverrideResult))] -[JsonSerializable(typeof(SetForcedColorsModeThemeOverrideCommand))] -[JsonSerializable(typeof(SetForcedColorsModeThemeOverrideResult))] -[JsonSerializable(typeof(SetScriptingEnabledCommand))] -[JsonSerializable(typeof(SetScriptingEnabledResult))] -[JsonSerializable(typeof(SetScreenOrientationOverrideCommand))] -[JsonSerializable(typeof(SetScreenOrientationOverrideResult))] -[JsonSerializable(typeof(SetGeolocationOverrideCommand))] -[JsonSerializable(typeof(SetGeolocationOverrideResult))] -internal partial class EmulationJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/src/webdriver/BiDi/Input/InputModule.cs b/dotnet/src/webdriver/BiDi/Input/InputModule.cs index fb6ded16381d4..ccc99be5d5e4a 100644 --- a/dotnet/src/webdriver/BiDi/Input/InputModule.cs +++ b/dotnet/src/webdriver/BiDi/Input/InputModule.cs @@ -18,51 +18,30 @@ // using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Input; public sealed class InputModule : Module { - private InputJsonSerializerContext _jsonContext = null!; - public async Task PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable actions, PerformActionsOptions? options = null) { var @params = new PerformActionsParameters(context, actions); - return await Broker.ExecuteCommandAsync(new PerformActionsCommand(@params), options, _jsonContext.PerformActionsCommand, _jsonContext.PerformActionsResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new PerformActionsCommand(@params), options, JsonContext.PerformActionsCommand, JsonContext.PerformActionsResult).ConfigureAwait(false); } public async Task ReleaseActionsAsync(BrowsingContext.BrowsingContext context, ReleaseActionsOptions? options = null) { var @params = new ReleaseActionsParameters(context); - return await Broker.ExecuteCommandAsync(new ReleaseActionsCommand(@params), options, _jsonContext.ReleaseActionsCommand, _jsonContext.ReleaseActionsResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new ReleaseActionsCommand(@params), options, JsonContext.ReleaseActionsCommand, JsonContext.ReleaseActionsResult).ConfigureAwait(false); } public async Task SetFilesAsync(BrowsingContext.BrowsingContext context, Script.ISharedReference element, IEnumerable files, SetFilesOptions? options = null) { var @params = new SetFilesParameters(context, element, files); - return await Broker.ExecuteCommandAsync(new SetFilesCommand(@params), options, _jsonContext.SetFilesCommand, _jsonContext.SetFilesResult).ConfigureAwait(false); - } - - protected override void Initialize(JsonSerializerOptions options) - { - _jsonContext = new InputJsonSerializerContext(options); + return await Broker.ExecuteCommandAsync(new SetFilesCommand(@params), options, JsonContext.SetFilesCommand, JsonContext.SetFilesResult).ConfigureAwait(false); } } - -[JsonSerializable(typeof(PerformActionsCommand))] -[JsonSerializable(typeof(PerformActionsResult))] -[JsonSerializable(typeof(ReleaseActionsCommand))] -[JsonSerializable(typeof(ReleaseActionsResult))] -[JsonSerializable(typeof(SetFilesCommand))] -[JsonSerializable(typeof(SetFilesResult))] -[JsonSerializable(typeof(IEnumerable))] -[JsonSerializable(typeof(IEnumerable))] -[JsonSerializable(typeof(IEnumerable))] -[JsonSerializable(typeof(IEnumerable))] -internal partial class InputJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/src/webdriver/BiDi/Json/BiDiJsonSerializerContext.cs b/dotnet/src/webdriver/BiDi/Json/BiDiJsonSerializerContext.cs new file mode 100644 index 0000000000000..759ec2353f221 --- /dev/null +++ b/dotnet/src/webdriver/BiDi/Json/BiDiJsonSerializerContext.cs @@ -0,0 +1,218 @@ +// +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace OpenQA.Selenium.BiDi.Json; + +[JsonSerializable(typeof(Browser.CloseCommand))] +[JsonSerializable(typeof(Browser.CloseResult))] +[JsonSerializable(typeof(Browser.CreateUserContextCommand))] +[JsonSerializable(typeof(Browser.CreateUserContextResult))] +[JsonSerializable(typeof(Browser.GetUserContextsCommand))] +[JsonSerializable(typeof(Browser.GetUserContextsResult))] +[JsonSerializable(typeof(Browser.RemoveUserContextCommand))] +[JsonSerializable(typeof(Browser.RemoveUserContextResult))] +[JsonSerializable(typeof(Browser.GetClientWindowsCommand))] +[JsonSerializable(typeof(Browser.GetClientWindowsResult))] +[JsonSerializable(typeof(Browser.SetDownloadBehaviorCommand))] +[JsonSerializable(typeof(Browser.SetDownloadBehaviorResult))] + +[JsonSerializable(typeof(BrowsingContext.ActivateCommand))] +[JsonSerializable(typeof(BrowsingContext.ActivateResult))] +[JsonSerializable(typeof(BrowsingContext.CaptureScreenshotCommand))] +[JsonSerializable(typeof(BrowsingContext.CaptureScreenshotResult))] +[JsonSerializable(typeof(BrowsingContext.CloseCommand), TypeInfoPropertyName = "BrowsingContext_CloseCommand")] +[JsonSerializable(typeof(BrowsingContext.CloseResult), TypeInfoPropertyName = "BrowsingContext_CloseResult")] +[JsonSerializable(typeof(BrowsingContext.CreateCommand))] +[JsonSerializable(typeof(BrowsingContext.CreateResult))] +[JsonSerializable(typeof(BrowsingContext.GetTreeCommand))] +[JsonSerializable(typeof(BrowsingContext.GetTreeResult))] +[JsonSerializable(typeof(BrowsingContext.HandleUserPromptCommand))] +[JsonSerializable(typeof(BrowsingContext.HandleUserPromptResult))] +[JsonSerializable(typeof(BrowsingContext.LocateNodesCommand))] +[JsonSerializable(typeof(BrowsingContext.LocateNodesResult))] +[JsonSerializable(typeof(BrowsingContext.NavigateCommand))] +[JsonSerializable(typeof(BrowsingContext.NavigateResult))] +[JsonSerializable(typeof(BrowsingContext.PrintCommand))] +[JsonSerializable(typeof(BrowsingContext.PrintResult))] +[JsonSerializable(typeof(BrowsingContext.ReloadCommand))] +[JsonSerializable(typeof(BrowsingContext.ReloadResult))] +[JsonSerializable(typeof(BrowsingContext.SetViewportCommand))] +[JsonSerializable(typeof(BrowsingContext.SetViewportResult))] +[JsonSerializable(typeof(BrowsingContext.TraverseHistoryCommand))] +[JsonSerializable(typeof(BrowsingContext.TraverseHistoryResult))] + +[JsonSerializable(typeof(BrowsingContext.BrowsingContextInfo))] +[JsonSerializable(typeof(BrowsingContext.DownloadWillBeginEventArgs))] +[JsonSerializable(typeof(BrowsingContext.DownloadEndEventArgs))] +[JsonSerializable(typeof(BrowsingContext.DownloadCanceledEventArgs))] +[JsonSerializable(typeof(BrowsingContext.DownloadCompleteEventArgs))] +[JsonSerializable(typeof(BrowsingContext.HistoryUpdatedEventArgs))] +[JsonSerializable(typeof(BrowsingContext.NavigationInfo))] +[JsonSerializable(typeof(BrowsingContext.UserPromptOpenedEventArgs))] +[JsonSerializable(typeof(BrowsingContext.UserPromptClosedEventArgs))] + +[JsonSerializable(typeof(Emulation.SetTimezoneOverrideCommand))] +[JsonSerializable(typeof(Emulation.SetTimezoneOverrideResult))] +[JsonSerializable(typeof(Emulation.SetUserAgentOverrideCommand))] +[JsonSerializable(typeof(Emulation.SetUserAgentOverrideResult))] +[JsonSerializable(typeof(Emulation.SetLocaleOverrideCommand))] +[JsonSerializable(typeof(Emulation.SetLocaleOverrideResult))] +[JsonSerializable(typeof(Emulation.SetForcedColorsModeThemeOverrideCommand))] +[JsonSerializable(typeof(Emulation.SetForcedColorsModeThemeOverrideResult))] +[JsonSerializable(typeof(Emulation.SetScriptingEnabledCommand))] +[JsonSerializable(typeof(Emulation.SetScriptingEnabledResult))] +[JsonSerializable(typeof(Emulation.SetScreenOrientationOverrideCommand))] +[JsonSerializable(typeof(Emulation.SetScreenOrientationOverrideResult))] +[JsonSerializable(typeof(Emulation.SetGeolocationOverrideCommand))] +[JsonSerializable(typeof(Emulation.SetGeolocationOverrideResult))] + +[JsonSerializable(typeof(Input.PerformActionsCommand))] +[JsonSerializable(typeof(Input.PerformActionsResult))] +[JsonSerializable(typeof(Input.ReleaseActionsCommand))] +[JsonSerializable(typeof(Input.ReleaseActionsResult))] +[JsonSerializable(typeof(Input.SetFilesCommand))] +[JsonSerializable(typeof(Input.SetFilesResult))] +[JsonSerializable(typeof(IEnumerable))] +[JsonSerializable(typeof(IEnumerable))] +[JsonSerializable(typeof(IEnumerable))] +[JsonSerializable(typeof(IEnumerable))] + +#region https://github.com/dotnet/runtime/issues/72604 +[JsonSerializable(typeof(Log.GenericLogEntry))] +[JsonSerializable(typeof(Log.ConsoleLogEntry))] +[JsonSerializable(typeof(Log.JavascriptLogEntry))] +#endregion + +[JsonSerializable(typeof(Log.LogEntry))] + +[JsonSerializable(typeof(Network.AddDataCollectorCommand))] +[JsonSerializable(typeof(Network.AddDataCollectorResult))] +[JsonSerializable(typeof(Network.AddInterceptCommand))] +[JsonSerializable(typeof(Network.AddInterceptResult))] +[JsonSerializable(typeof(Network.ContinueRequestCommand))] +[JsonSerializable(typeof(Network.ContinueRequestResult))] +[JsonSerializable(typeof(Network.ContinueResponseCommand))] +[JsonSerializable(typeof(Network.ContinueResponseResult))] +[JsonSerializable(typeof(Network.ContinueWithAuthCommand))] +[JsonSerializable(typeof(Network.ContinueWithAuthResult))] +[JsonSerializable(typeof(Network.FailRequestCommand))] +[JsonSerializable(typeof(Network.FailRequestResult))] +[JsonSerializable(typeof(Network.GetDataCommand))] +[JsonSerializable(typeof(Network.GetDataResult))] +[JsonSerializable(typeof(Network.ProvideResponseCommand))] +[JsonSerializable(typeof(Network.ProvideResponseResult))] +[JsonSerializable(typeof(Network.RemoveDataCollectorCommand))] +[JsonSerializable(typeof(Network.RemoveDataCollectorResult))] +[JsonSerializable(typeof(Network.RemoveInterceptCommand))] +[JsonSerializable(typeof(Network.RemoveInterceptResult))] +[JsonSerializable(typeof(Network.SetCacheBehaviorCommand))] +[JsonSerializable(typeof(Network.SetCacheBehaviorResult))] +[JsonSerializable(typeof(Network.SetExtraHeadersCommand))] +[JsonSerializable(typeof(Network.SetExtraHeadersResult))] + +[JsonSerializable(typeof(Network.BeforeRequestSentEventArgs))] +[JsonSerializable(typeof(Network.ResponseStartedEventArgs))] +[JsonSerializable(typeof(Network.ResponseCompletedEventArgs))] +[JsonSerializable(typeof(Network.FetchErrorEventArgs))] +[JsonSerializable(typeof(Network.AuthRequiredEventArgs))] + +#region https://github.com/dotnet/runtime/issues/72604 +[JsonSerializable(typeof(Script.EvaluateResultSuccess))] +[JsonSerializable(typeof(Script.EvaluateResultException))] + +[JsonSerializable(typeof(Script.NumberRemoteValue))] +[JsonSerializable(typeof(Script.BooleanRemoteValue))] +[JsonSerializable(typeof(Script.BigIntRemoteValue))] +[JsonSerializable(typeof(Script.StringRemoteValue))] +[JsonSerializable(typeof(Script.NullRemoteValue))] +[JsonSerializable(typeof(Script.UndefinedRemoteValue))] +[JsonSerializable(typeof(Script.SymbolRemoteValue))] +[JsonSerializable(typeof(Script.ArrayRemoteValue))] +[JsonSerializable(typeof(Script.ObjectRemoteValue))] +[JsonSerializable(typeof(Script.FunctionRemoteValue))] +[JsonSerializable(typeof(Script.RegExpRemoteValue))] +[JsonSerializable(typeof(Script.DateRemoteValue))] +[JsonSerializable(typeof(Script.MapRemoteValue))] +[JsonSerializable(typeof(Script.SetRemoteValue))] +[JsonSerializable(typeof(Script.WeakMapRemoteValue))] +[JsonSerializable(typeof(Script.WeakSetRemoteValue))] +[JsonSerializable(typeof(Script.GeneratorRemoteValue))] +[JsonSerializable(typeof(Script.ErrorRemoteValue))] +[JsonSerializable(typeof(Script.ProxyRemoteValue))] +[JsonSerializable(typeof(Script.PromiseRemoteValue))] +[JsonSerializable(typeof(Script.TypedArrayRemoteValue))] +[JsonSerializable(typeof(Script.ArrayBufferRemoteValue))] +[JsonSerializable(typeof(Script.NodeListRemoteValue))] +[JsonSerializable(typeof(Script.HtmlCollectionRemoteValue))] +[JsonSerializable(typeof(Script.NodeRemoteValue))] +[JsonSerializable(typeof(Script.WindowProxyRemoteValue))] + +[JsonSerializable(typeof(Script.WindowRealmInfo))] +[JsonSerializable(typeof(Script.DedicatedWorkerRealmInfo))] +[JsonSerializable(typeof(Script.SharedWorkerRealmInfo))] +[JsonSerializable(typeof(Script.ServiceWorkerRealmInfo))] +[JsonSerializable(typeof(Script.WorkerRealmInfo))] +[JsonSerializable(typeof(Script.PaintWorkletRealmInfo))] +[JsonSerializable(typeof(Script.AudioWorkletRealmInfo))] +[JsonSerializable(typeof(Script.WorkletRealmInfo))] +#endregion + +[JsonSerializable(typeof(Script.AddPreloadScriptCommand))] +[JsonSerializable(typeof(Script.AddPreloadScriptResult))] +[JsonSerializable(typeof(Script.DisownCommand))] +[JsonSerializable(typeof(Script.DisownResult))] +[JsonSerializable(typeof(Script.CallFunctionCommand))] +[JsonSerializable(typeof(Script.EvaluateResult))] +[JsonSerializable(typeof(Script.EvaluateCommand))] +[JsonSerializable(typeof(Script.EvaluateResult))] +[JsonSerializable(typeof(Script.GetRealmsCommand))] +[JsonSerializable(typeof(Script.GetRealmsResult))] +[JsonSerializable(typeof(Script.RemovePreloadScriptCommand))] +[JsonSerializable(typeof(Script.RemovePreloadScriptResult))] + +[JsonSerializable(typeof(Script.MessageEventArgs))] +[JsonSerializable(typeof(Script.RealmDestroyedEventArgs))] + +[JsonSerializable(typeof(Session.StatusCommand))] +[JsonSerializable(typeof(Session.StatusResult))] +[JsonSerializable(typeof(Session.NewCommand))] +[JsonSerializable(typeof(Session.NewResult))] +[JsonSerializable(typeof(Session.EndCommand))] +[JsonSerializable(typeof(Session.EndResult))] +[JsonSerializable(typeof(Session.SubscribeCommand))] +[JsonSerializable(typeof(Session.SubscribeResult))] +[JsonSerializable(typeof(Session.UnsubscribeByIdCommand))] +[JsonSerializable(typeof(Session.UnsubscribeResult))] + +[JsonSerializable(typeof(Storage.GetCookiesCommand))] +[JsonSerializable(typeof(Storage.GetCookiesResult))] +[JsonSerializable(typeof(Storage.SetCookieCommand))] +[JsonSerializable(typeof(Storage.SetCookieResult))] +[JsonSerializable(typeof(Storage.DeleteCookiesCommand))] +[JsonSerializable(typeof(Storage.DeleteCookiesResult))] + +[JsonSerializable(typeof(WebExtension.InstallCommand))] +[JsonSerializable(typeof(WebExtension.InstallResult))] +[JsonSerializable(typeof(WebExtension.UninstallCommand))] +[JsonSerializable(typeof(WebExtension.UninstallResult))] + +internal partial class BiDiJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/src/webdriver/BiDi/Json/Converters/BrowserUserContextConverter.cs b/dotnet/src/webdriver/BiDi/Json/Converters/BrowserUserContextConverter.cs index e660c9843f086..f8e3b02bef820 100644 --- a/dotnet/src/webdriver/BiDi/Json/Converters/BrowserUserContextConverter.cs +++ b/dotnet/src/webdriver/BiDi/Json/Converters/BrowserUserContextConverter.cs @@ -37,7 +37,7 @@ public BrowserUserContextConverter(BiDi bidi) { var id = reader.GetString(); - return new UserContext(_bidi, id!); + return new UserContext(id!) { BiDi = _bidi }; } public override void Write(Utf8JsonWriter writer, UserContext value, JsonSerializerOptions options) diff --git a/dotnet/src/webdriver/BiDi/Json/Converters/BrowsingContextConverter.cs b/dotnet/src/webdriver/BiDi/Json/Converters/BrowsingContextConverter.cs index 208b2698ad01f..98703b4dc6f83 100644 --- a/dotnet/src/webdriver/BiDi/Json/Converters/BrowsingContextConverter.cs +++ b/dotnet/src/webdriver/BiDi/Json/Converters/BrowsingContextConverter.cs @@ -36,7 +36,7 @@ public BrowsingContextConverter(BiDi bidi) { var id = reader.GetString(); - return new BrowsingContext.BrowsingContext(_bidi, id!); + return new BrowsingContext.BrowsingContext(id!) { BiDi = _bidi }; } public override void Write(Utf8JsonWriter writer, BrowsingContext.BrowsingContext value, JsonSerializerOptions options) diff --git a/dotnet/src/webdriver/BiDi/Json/Converters/CollectorConverter.cs b/dotnet/src/webdriver/BiDi/Json/Converters/CollectorConverter.cs index 9f77d0f03fcbc..aff1a19c348a9 100644 --- a/dotnet/src/webdriver/BiDi/Json/Converters/CollectorConverter.cs +++ b/dotnet/src/webdriver/BiDi/Json/Converters/CollectorConverter.cs @@ -37,7 +37,7 @@ public CollectorConverter(BiDi bidi) { var id = reader.GetString(); - return new Collector(_bidi, id!); + return new Collector(id!) { BiDi = _bidi }; } public override void Write(Utf8JsonWriter writer, Collector value, JsonSerializerOptions options) diff --git a/dotnet/src/webdriver/BiDi/Json/Converters/HandleConverter.cs b/dotnet/src/webdriver/BiDi/Json/Converters/HandleConverter.cs index 852a3d925cc00..4f477b1a92ab4 100644 --- a/dotnet/src/webdriver/BiDi/Json/Converters/HandleConverter.cs +++ b/dotnet/src/webdriver/BiDi/Json/Converters/HandleConverter.cs @@ -37,7 +37,7 @@ public HandleConverter(BiDi bidi) { var id = reader.GetString(); - return new Handle(_bidi, id!); + return new Handle(id!) { BiDi = _bidi }; } public override void Write(Utf8JsonWriter writer, Handle value, JsonSerializerOptions options) diff --git a/dotnet/src/webdriver/BiDi/Json/Converters/InterceptConverter.cs b/dotnet/src/webdriver/BiDi/Json/Converters/InterceptConverter.cs index a72e6cfa96921..4ef50be6d3cf4 100644 --- a/dotnet/src/webdriver/BiDi/Json/Converters/InterceptConverter.cs +++ b/dotnet/src/webdriver/BiDi/Json/Converters/InterceptConverter.cs @@ -37,7 +37,7 @@ public InterceptConverter(BiDi bidi) { var id = reader.GetString(); - return new Intercept(_bidi, id!); + return new Intercept(id!) { BiDi = _bidi }; } public override void Write(Utf8JsonWriter writer, Intercept value, JsonSerializerOptions options) diff --git a/dotnet/src/webdriver/BiDi/Json/Converters/InternalIdConverter.cs b/dotnet/src/webdriver/BiDi/Json/Converters/InternalIdConverter.cs index 6f14fa6571d64..5ed3d01171084 100644 --- a/dotnet/src/webdriver/BiDi/Json/Converters/InternalIdConverter.cs +++ b/dotnet/src/webdriver/BiDi/Json/Converters/InternalIdConverter.cs @@ -37,7 +37,7 @@ public InternalIdConverter(BiDi bidi) { var id = reader.GetString(); - return new InternalId(_bidi, id!); + return new InternalId(id!) { BiDi = _bidi }; } public override void Write(Utf8JsonWriter writer, InternalId value, JsonSerializerOptions options) diff --git a/dotnet/src/webdriver/BiDi/Json/Converters/PreloadScriptConverter.cs b/dotnet/src/webdriver/BiDi/Json/Converters/PreloadScriptConverter.cs index 9a531f60f79ed..a27c41938fdd9 100644 --- a/dotnet/src/webdriver/BiDi/Json/Converters/PreloadScriptConverter.cs +++ b/dotnet/src/webdriver/BiDi/Json/Converters/PreloadScriptConverter.cs @@ -37,7 +37,7 @@ public PreloadScriptConverter(BiDi bidi) { var id = reader.GetString(); - return new PreloadScript(_bidi, id!); + return new PreloadScript(id!) { BiDi = _bidi }; } public override void Write(Utf8JsonWriter writer, PreloadScript value, JsonSerializerOptions options) diff --git a/dotnet/src/webdriver/BiDi/Json/Converters/RealmConverter.cs b/dotnet/src/webdriver/BiDi/Json/Converters/RealmConverter.cs index 235c40b0363f4..388705cf85cf8 100644 --- a/dotnet/src/webdriver/BiDi/Json/Converters/RealmConverter.cs +++ b/dotnet/src/webdriver/BiDi/Json/Converters/RealmConverter.cs @@ -37,7 +37,7 @@ public RealmConverter(BiDi bidi) { var id = reader.GetString(); - return new Realm(_bidi, id!); + return new Realm(id!) { BiDi = _bidi }; } public override void Write(Utf8JsonWriter writer, Realm value, JsonSerializerOptions options) diff --git a/dotnet/src/webdriver/BiDi/Json/Converters/WebExtensionConverter.cs b/dotnet/src/webdriver/BiDi/Json/Converters/WebExtensionConverter.cs index ec3bc5e9cb7dd..e0ab05ae51875 100644 --- a/dotnet/src/webdriver/BiDi/Json/Converters/WebExtensionConverter.cs +++ b/dotnet/src/webdriver/BiDi/Json/Converters/WebExtensionConverter.cs @@ -37,7 +37,7 @@ public WebExtensionConverter(BiDi bidi) { var id = reader.GetString(); - return new Extension(_bidi, id!); + return new Extension(id!) { BiDi = _bidi }; } public override void Write(Utf8JsonWriter writer, Extension value, JsonSerializerOptions options) diff --git a/dotnet/src/webdriver/BiDi/Log/LogModule.cs b/dotnet/src/webdriver/BiDi/Log/LogModule.cs index 34ea0e1d66c4c..28f5fbe5ed20b 100644 --- a/dotnet/src/webdriver/BiDi/Log/LogModule.cs +++ b/dotnet/src/webdriver/BiDi/Log/LogModule.cs @@ -18,66 +18,19 @@ // using System; -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Log; public sealed class LogModule : Module { - private LogJsonSerializerContext _jsonContext = null!; - public async Task OnEntryAddedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("log.entryAdded", handler, options, _jsonContext.LogEntry).ConfigureAwait(false); + return await Broker.SubscribeAsync("log.entryAdded", handler, options, JsonContext.LogEntry).ConfigureAwait(false); } public async Task OnEntryAddedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("log.entryAdded", handler, options, _jsonContext.LogEntry).ConfigureAwait(false); - } - - protected override void Initialize(JsonSerializerOptions options) - { - _jsonContext = new LogJsonSerializerContext(options); + return await Broker.SubscribeAsync("log.entryAdded", handler, options, JsonContext.LogEntry).ConfigureAwait(false); } } - -#region https://github.com/dotnet/runtime/issues/72604 Script.RemoteValue type dependency -[JsonSerializable(typeof(Script.NumberRemoteValue))] -[JsonSerializable(typeof(Script.BooleanRemoteValue))] -[JsonSerializable(typeof(Script.BigIntRemoteValue))] -[JsonSerializable(typeof(Script.StringRemoteValue))] -[JsonSerializable(typeof(Script.NullRemoteValue))] -[JsonSerializable(typeof(Script.UndefinedRemoteValue))] -[JsonSerializable(typeof(Script.SymbolRemoteValue))] -[JsonSerializable(typeof(Script.ArrayRemoteValue))] -[JsonSerializable(typeof(Script.ObjectRemoteValue))] -[JsonSerializable(typeof(Script.FunctionRemoteValue))] -[JsonSerializable(typeof(Script.RegExpRemoteValue))] -[JsonSerializable(typeof(Script.DateRemoteValue))] -[JsonSerializable(typeof(Script.MapRemoteValue))] -[JsonSerializable(typeof(Script.SetRemoteValue))] -[JsonSerializable(typeof(Script.WeakMapRemoteValue))] -[JsonSerializable(typeof(Script.WeakSetRemoteValue))] -[JsonSerializable(typeof(Script.GeneratorRemoteValue))] -[JsonSerializable(typeof(Script.ErrorRemoteValue))] -[JsonSerializable(typeof(Script.ProxyRemoteValue))] -[JsonSerializable(typeof(Script.PromiseRemoteValue))] -[JsonSerializable(typeof(Script.TypedArrayRemoteValue))] -[JsonSerializable(typeof(Script.ArrayBufferRemoteValue))] -[JsonSerializable(typeof(Script.NodeListRemoteValue))] -[JsonSerializable(typeof(Script.HtmlCollectionRemoteValue))] -[JsonSerializable(typeof(Script.NodeRemoteValue))] -[JsonSerializable(typeof(Script.WindowProxyRemoteValue))] -#endregion - -#region https://github.com/dotnet/runtime/issues/72604 -[JsonSerializable(typeof(GenericLogEntry))] -[JsonSerializable(typeof(ConsoleLogEntry))] -[JsonSerializable(typeof(JavascriptLogEntry))] -#endregion - -[JsonSerializable(typeof(LogEntry))] -internal partial class LogJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/src/webdriver/BiDi/Module.cs b/dotnet/src/webdriver/BiDi/Module.cs index 4e90c2db7df67..7f5607506de35 100644 --- a/dotnet/src/webdriver/BiDi/Module.cs +++ b/dotnet/src/webdriver/BiDi/Module.cs @@ -17,7 +17,8 @@ // under the License. // -using System.Text.Json; +using OpenQA.Selenium.BiDi.Json; +using System.Text.Json.Serialization; namespace OpenQA.Selenium.BiDi; @@ -27,18 +28,24 @@ public abstract class Module protected Broker Broker { get; private set; } = null!; - protected abstract void Initialize(JsonSerializerOptions options); + internal BiDiJsonSerializerContext JsonContext { get; private set; } = null!; - internal static TModule Create(BiDi bidi, Broker broker, JsonSerializerOptions jsonSerializerOptions) + protected virtual void Initialize(JsonSerializerContext jsonSerializerContext) + { + JsonContext = (BiDiJsonSerializerContext)jsonSerializerContext; + } + + internal static TModule Create(BiDi bidi, Broker broker, BiDiJsonSerializerContext jsonSerializerContext) where TModule : Module, new() { TModule module = new() { BiDi = bidi, - Broker = broker + Broker = broker, + JsonContext = jsonSerializerContext }; - module.Initialize(jsonSerializerOptions); + module.Initialize(jsonSerializerContext); return module; } diff --git a/dotnet/src/webdriver/BiDi/Network/Collector.cs b/dotnet/src/webdriver/BiDi/Network/Collector.cs index 0a1f56a5064a5..6239c25fc3a17 100644 --- a/dotnet/src/webdriver/BiDi/Network/Collector.cs +++ b/dotnet/src/webdriver/BiDi/Network/Collector.cs @@ -18,25 +18,26 @@ // using System; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Network; public sealed class Collector : IAsyncDisposable { - private readonly BiDi _bidi; - - internal Collector(BiDi bidi, string id) + internal Collector(string id) { - _bidi = bidi; Id = id; } internal string Id { get; } + [JsonIgnore] + public BiDi BiDi { get; internal set; } + public async Task RemoveAsync() { - await _bidi.Network.RemoveDataCollectorAsync(this).ConfigureAwait(false); + await BiDi.Network.RemoveDataCollectorAsync(this).ConfigureAwait(false); } public async ValueTask DisposeAsync() diff --git a/dotnet/src/webdriver/BiDi/Network/Intercept.cs b/dotnet/src/webdriver/BiDi/Network/Intercept.cs index c266f791efaa3..2b5ee6510b52d 100644 --- a/dotnet/src/webdriver/BiDi/Network/Intercept.cs +++ b/dotnet/src/webdriver/BiDi/Network/Intercept.cs @@ -20,29 +20,30 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Network; public sealed class Intercept : IAsyncDisposable { - private readonly BiDi _bidi; - - internal Intercept(BiDi bidi, string id) + internal Intercept(string id) { - _bidi = bidi; Id = id; } internal string Id { get; } + [JsonIgnore] + public BiDi BiDi { get; internal set; } + IList OnBeforeRequestSentSubscriptions { get; } = []; IList OnResponseStartedSubscriptions { get; } = []; IList OnAuthRequiredSubscriptions { get; } = []; public async Task RemoveAsync() { - await _bidi.Network.RemoveInterceptAsync(this).ConfigureAwait(false); + await BiDi.Network.RemoveInterceptAsync(this).ConfigureAwait(false); foreach (var subscription in OnBeforeRequestSentSubscriptions) { @@ -62,21 +63,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 BiDi.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 BiDi.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 BiDi.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 74b874078e237..12bc9c98d1613 100644 --- a/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs +++ b/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs @@ -19,21 +19,17 @@ using System; using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Network; public sealed partial class NetworkModule : Module { - private NetworkJsonSerializerContext _jsonContext = null!; - public async Task AddDataCollectorAsync(IEnumerable DataTypes, int MaxEncodedDataSize, AddDataCollectorOptions? options = null) { var @params = new AddDataCollectorParameters(DataTypes, MaxEncodedDataSize, options?.CollectorType, options?.Contexts, options?.UserContexts); - var result = await Broker.ExecuteCommandAsync(new AddDataCollectorCommand(@params), options, _jsonContext.AddDataCollectorCommand, _jsonContext.AddDataCollectorResult).ConfigureAwait(false); + var result = await Broker.ExecuteCommandAsync(new AddDataCollectorCommand(@params), options, JsonContext.AddDataCollectorCommand, JsonContext.AddDataCollectorResult).ConfigureAwait(false); return result.Collector; } @@ -42,7 +38,7 @@ public async Task AddInterceptAsync(IEnumerable phase { var @params = new AddInterceptParameters(phases, options?.Contexts, options?.UrlPatterns); - var result = await Broker.ExecuteCommandAsync(new AddInterceptCommand(@params), options, _jsonContext.AddInterceptCommand, _jsonContext.AddInterceptResult).ConfigureAwait(false); + var result = await Broker.ExecuteCommandAsync(new AddInterceptCommand(@params), options, JsonContext.AddInterceptCommand, JsonContext.AddInterceptResult).ConfigureAwait(false); return result.Intercept; } @@ -51,56 +47,56 @@ public async Task RemoveDataCollectorAsync(Collector { var @params = new RemoveDataCollectorParameters(collector); - return await Broker.ExecuteCommandAsync(new RemoveDataCollectorCommand(@params), options, _jsonContext.RemoveDataCollectorCommand, _jsonContext.RemoveDataCollectorResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new RemoveDataCollectorCommand(@params), options, JsonContext.RemoveDataCollectorCommand, JsonContext.RemoveDataCollectorResult).ConfigureAwait(false); } public async Task RemoveInterceptAsync(Intercept intercept, RemoveInterceptOptions? options = null) { var @params = new RemoveInterceptParameters(intercept); - return await Broker.ExecuteCommandAsync(new RemoveInterceptCommand(@params), options, _jsonContext.RemoveInterceptCommand, _jsonContext.RemoveInterceptResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new RemoveInterceptCommand(@params), options, JsonContext.RemoveInterceptCommand, JsonContext.RemoveInterceptResult).ConfigureAwait(false); } public async Task SetCacheBehaviorAsync(CacheBehavior behavior, SetCacheBehaviorOptions? options = null) { var @params = new SetCacheBehaviorParameters(behavior, options?.Contexts); - return await Broker.ExecuteCommandAsync(new SetCacheBehaviorCommand(@params), options, _jsonContext.SetCacheBehaviorCommand, _jsonContext.SetCacheBehaviorResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetCacheBehaviorCommand(@params), options, JsonContext.SetCacheBehaviorCommand, JsonContext.SetCacheBehaviorResult).ConfigureAwait(false); } public async Task SetExtraHeadersAsync(IEnumerable
headers, SetExtraHeadersOptions? options = null) { var @params = new SetExtraHeadersParameters(headers, options?.Contexts, options?.UserContexts); - return await Broker.ExecuteCommandAsync(new SetExtraHeadersCommand(@params), options, _jsonContext.SetExtraHeadersCommand, _jsonContext.SetExtraHeadersResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new SetExtraHeadersCommand(@params), options, JsonContext.SetExtraHeadersCommand, JsonContext.SetExtraHeadersResult).ConfigureAwait(false); } public async Task ContinueRequestAsync(Request request, ContinueRequestOptions? options = null) { var @params = new ContinueRequestParameters(request, options?.Body, options?.Cookies, options?.Headers, options?.Method, options?.Url); - return await Broker.ExecuteCommandAsync(new ContinueRequestCommand(@params), options, _jsonContext.ContinueRequestCommand, _jsonContext.ContinueRequestResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new ContinueRequestCommand(@params), options, JsonContext.ContinueRequestCommand, JsonContext.ContinueRequestResult).ConfigureAwait(false); } public async Task ContinueResponseAsync(Request request, ContinueResponseOptions? options = null) { var @params = new ContinueResponseParameters(request, options?.Cookies, options?.Credentials, options?.Headers, options?.ReasonPhrase, options?.StatusCode); - return await Broker.ExecuteCommandAsync(new ContinueResponseCommand(@params), options, _jsonContext.ContinueResponseCommand, _jsonContext.ContinueResponseResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new ContinueResponseCommand(@params), options, JsonContext.ContinueResponseCommand, JsonContext.ContinueResponseResult).ConfigureAwait(false); } public async Task FailRequestAsync(Request request, FailRequestOptions? options = null) { var @params = new FailRequestParameters(request); - return await Broker.ExecuteCommandAsync(new FailRequestCommand(@params), options, _jsonContext.FailRequestCommand, _jsonContext.FailRequestResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new FailRequestCommand(@params), options, JsonContext.FailRequestCommand, JsonContext.FailRequestResult).ConfigureAwait(false); } public async Task GetDataAsync(DataType dataType, Request request, GetDataOptions? options = null) { var @params = new GetDataParameters(dataType, request, options?.Collector, options?.Disown); - var result = await Broker.ExecuteCommandAsync(new GetDataCommand(@params), options, _jsonContext.GetDataCommand, _jsonContext.GetDataResult).ConfigureAwait(false); + var result = await Broker.ExecuteCommandAsync(new GetDataCommand(@params), options, JsonContext.GetDataCommand, JsonContext.GetDataResult).ConfigureAwait(false); return result.Bytes; } @@ -109,108 +105,71 @@ public async Task ProvideResponseAsync(Request request, P { var @params = new ProvideResponseParameters(request, options?.Body, options?.Cookies, options?.Headers, options?.ReasonPhrase, options?.StatusCode); - return await Broker.ExecuteCommandAsync(new ProvideResponseCommand(@params), options, _jsonContext.ProvideResponseCommand, _jsonContext.ProvideResponseResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new ProvideResponseCommand(@params), options, JsonContext.ProvideResponseCommand, JsonContext.ProvideResponseResult).ConfigureAwait(false); } public async Task ContinueWithAuthAsync(Request request, AuthCredentials credentials, ContinueWithAuthCredentialsOptions? options = null) { - return await Broker.ExecuteCommandAsync(new ContinueWithAuthCommand(new ContinueWithAuthCredentials(request, credentials)), options, _jsonContext.ContinueWithAuthCommand, _jsonContext.ContinueWithAuthResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new ContinueWithAuthCommand(new ContinueWithAuthCredentials(request, credentials)), options, JsonContext.ContinueWithAuthCommand, JsonContext.ContinueWithAuthResult).ConfigureAwait(false); } public async Task ContinueWithAuthAsync(Request request, ContinueWithAuthDefaultCredentialsOptions? options = null) { - return await Broker.ExecuteCommandAsync(new ContinueWithAuthCommand(new ContinueWithAuthDefaultCredentials(request)), options, _jsonContext.ContinueWithAuthCommand, _jsonContext.ContinueWithAuthResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new ContinueWithAuthCommand(new ContinueWithAuthDefaultCredentials(request)), options, JsonContext.ContinueWithAuthCommand, JsonContext.ContinueWithAuthResult).ConfigureAwait(false); } public async Task ContinueWithAuthAsync(Request request, ContinueWithAuthCancelCredentialsOptions? options = null) { - return await Broker.ExecuteCommandAsync(new ContinueWithAuthCommand(new ContinueWithAuthCancelCredentials(request)), options, _jsonContext.ContinueWithAuthCommand, _jsonContext.ContinueWithAuthResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new ContinueWithAuthCommand(new ContinueWithAuthCancelCredentials(request)), options, JsonContext.ContinueWithAuthCommand, JsonContext.ContinueWithAuthResult).ConfigureAwait(false); } public async Task OnBeforeRequestSentAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("network.beforeRequestSent", handler, options, _jsonContext.BeforeRequestSentEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("network.beforeRequestSent", handler, options, JsonContext.BeforeRequestSentEventArgs).ConfigureAwait(false); } public async Task OnBeforeRequestSentAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("network.beforeRequestSent", handler, options, _jsonContext.BeforeRequestSentEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("network.beforeRequestSent", handler, options, JsonContext.BeforeRequestSentEventArgs).ConfigureAwait(false); } public async Task OnResponseStartedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("network.responseStarted", handler, options, _jsonContext.ResponseStartedEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("network.responseStarted", handler, options, JsonContext.ResponseStartedEventArgs).ConfigureAwait(false); } public async Task OnResponseStartedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("network.responseStarted", handler, options, _jsonContext.ResponseStartedEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("network.responseStarted", handler, options, JsonContext.ResponseStartedEventArgs).ConfigureAwait(false); } public async Task OnResponseCompletedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("network.responseCompleted", handler, options, _jsonContext.ResponseCompletedEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("network.responseCompleted", handler, options, JsonContext.ResponseCompletedEventArgs).ConfigureAwait(false); } public async Task OnResponseCompletedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("network.responseCompleted", handler, options, _jsonContext.ResponseCompletedEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("network.responseCompleted", handler, options, JsonContext.ResponseCompletedEventArgs).ConfigureAwait(false); } public async Task OnFetchErrorAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("network.fetchError", handler, options, _jsonContext.FetchErrorEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("network.fetchError", handler, options, JsonContext.FetchErrorEventArgs).ConfigureAwait(false); } public async Task OnFetchErrorAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("network.fetchError", handler, options, _jsonContext.FetchErrorEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("network.fetchError", handler, options, JsonContext.FetchErrorEventArgs).ConfigureAwait(false); } public async Task OnAuthRequiredAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("network.authRequired", handler, options, _jsonContext.AuthRequiredEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("network.authRequired", handler, options, JsonContext.AuthRequiredEventArgs).ConfigureAwait(false); } public async Task OnAuthRequiredAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("network.authRequired", handler, options, _jsonContext.AuthRequiredEventArgs).ConfigureAwait(false); - } - - protected override void Initialize(JsonSerializerOptions options) - { - _jsonContext = new NetworkJsonSerializerContext(options); + return await Broker.SubscribeAsync("network.authRequired", handler, options, JsonContext.AuthRequiredEventArgs).ConfigureAwait(false); } } - -[JsonSerializable(typeof(AddDataCollectorCommand))] -[JsonSerializable(typeof(AddDataCollectorResult))] -[JsonSerializable(typeof(AddInterceptCommand))] -[JsonSerializable(typeof(AddInterceptResult))] -[JsonSerializable(typeof(ContinueRequestCommand))] -[JsonSerializable(typeof(ContinueRequestResult))] -[JsonSerializable(typeof(ContinueResponseCommand))] -[JsonSerializable(typeof(ContinueResponseResult))] -[JsonSerializable(typeof(ContinueWithAuthCommand))] -[JsonSerializable(typeof(ContinueWithAuthResult))] -[JsonSerializable(typeof(FailRequestCommand))] -[JsonSerializable(typeof(FailRequestResult))] -[JsonSerializable(typeof(GetDataCommand))] -[JsonSerializable(typeof(GetDataResult))] -[JsonSerializable(typeof(ProvideResponseCommand))] -[JsonSerializable(typeof(ProvideResponseResult))] -[JsonSerializable(typeof(RemoveDataCollectorCommand))] -[JsonSerializable(typeof(RemoveDataCollectorResult))] -[JsonSerializable(typeof(RemoveInterceptCommand))] -[JsonSerializable(typeof(RemoveInterceptResult))] -[JsonSerializable(typeof(SetCacheBehaviorCommand))] -[JsonSerializable(typeof(SetCacheBehaviorResult))] -[JsonSerializable(typeof(SetExtraHeadersCommand))] -[JsonSerializable(typeof(SetExtraHeadersResult))] - -[JsonSerializable(typeof(BeforeRequestSentEventArgs))] -[JsonSerializable(typeof(ResponseStartedEventArgs))] -[JsonSerializable(typeof(ResponseCompletedEventArgs))] -[JsonSerializable(typeof(FetchErrorEventArgs))] -[JsonSerializable(typeof(AuthRequiredEventArgs))] -internal partial class NetworkJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/src/webdriver/BiDi/Permissions/PermissionsModule.cs b/dotnet/src/webdriver/BiDi/Permissions/PermissionsModule.cs index 476166c516370..d6fe779f00292 100644 --- a/dotnet/src/webdriver/BiDi/Permissions/PermissionsModule.cs +++ b/dotnet/src/webdriver/BiDi/Permissions/PermissionsModule.cs @@ -35,9 +35,11 @@ public async Task SetPermissionAsync(PermissionDescriptor d return await Broker.ExecuteCommandAsync(new SetPermissionCommand(@params), options, _jsonContext.SetPermissionCommand, _jsonContext.SetPermissionResult).ConfigureAwait(false); } - protected override void Initialize(JsonSerializerOptions options) + protected override void Initialize(JsonSerializerContext jsonSerializerContext) { - _jsonContext = new PermissionsJsonSerializerContext(options); + var jsonOptions = new JsonSerializerOptions(jsonSerializerContext.Options); + + _jsonContext = new PermissionsJsonSerializerContext(jsonOptions); } } diff --git a/dotnet/src/webdriver/BiDi/Script/Handle.cs b/dotnet/src/webdriver/BiDi/Script/Handle.cs index 5b5e4f63ede08..17a3a70c8be27 100644 --- a/dotnet/src/webdriver/BiDi/Script/Handle.cs +++ b/dotnet/src/webdriver/BiDi/Script/Handle.cs @@ -17,17 +17,19 @@ // under the License. // +using System.Text.Json.Serialization; + namespace OpenQA.Selenium.BiDi.Script; public sealed class Handle { - private readonly BiDi _bidi; - - public Handle(BiDi bidi, string id) + public Handle(string id) { - _bidi = bidi; Id = id; } public string Id { get; } + + [JsonIgnore] + public BiDi BiDi { get; internal set; } } diff --git a/dotnet/src/webdriver/BiDi/Script/InternalId.cs b/dotnet/src/webdriver/BiDi/Script/InternalId.cs index 2914c77c99a0c..31d276236ec20 100644 --- a/dotnet/src/webdriver/BiDi/Script/InternalId.cs +++ b/dotnet/src/webdriver/BiDi/Script/InternalId.cs @@ -17,17 +17,19 @@ // under the License. // +using System.Text.Json.Serialization; + namespace OpenQA.Selenium.BiDi.Script; public sealed class InternalId { - readonly BiDi _bidi; - - public InternalId(BiDi bidi, string id) + public InternalId(string id) { - _bidi = bidi; Id = id; } public string Id { get; } + + [JsonIgnore] + public BiDi BiDi { get; internal set; } } diff --git a/dotnet/src/webdriver/BiDi/Script/PreloadScript.cs b/dotnet/src/webdriver/BiDi/Script/PreloadScript.cs index 428825305709c..f65053eb8b0c2 100644 --- a/dotnet/src/webdriver/BiDi/Script/PreloadScript.cs +++ b/dotnet/src/webdriver/BiDi/Script/PreloadScript.cs @@ -18,25 +18,26 @@ // using System; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Script; public sealed class PreloadScript : IAsyncDisposable { - private readonly BiDi _bidi; - - public PreloadScript(BiDi bidi, string id) + internal PreloadScript(string id) { - _bidi = bidi; Id = id; } public string Id { get; } + [JsonIgnore] + public BiDi BiDi { get; internal set; } + public Task RemoveAsync() { - return _bidi.Script.RemovePreloadScriptAsync(this); + return BiDi.Script.RemovePreloadScriptAsync(this); } public async ValueTask DisposeAsync() diff --git a/dotnet/src/webdriver/BiDi/Script/Realm.cs b/dotnet/src/webdriver/BiDi/Script/Realm.cs index b3fd6f776e0c7..bdf1a1c87d43a 100644 --- a/dotnet/src/webdriver/BiDi/Script/Realm.cs +++ b/dotnet/src/webdriver/BiDi/Script/Realm.cs @@ -17,17 +17,19 @@ // under the License. // +using System.Text.Json.Serialization; + namespace OpenQA.Selenium.BiDi.Script; public sealed class Realm { - private readonly BiDi _bidi; - - public Realm(BiDi bidi, string id) + internal Realm(string id) { - _bidi = bidi; Id = id; } public string Id { get; } + + [JsonIgnore] + public BiDi BiDi { get; internal set; } } diff --git a/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs index bd1354decc6e8..e6287f1c4ecf8 100644 --- a/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs @@ -20,21 +20,17 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Script; public sealed class ScriptModule : Module { - private ScriptJsonSerializerContext _jsonContext = null!; - public async Task EvaluateAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string expression, bool awaitPromise, Target target, EvaluateOptions? options = null) { var @params = new EvaluateParameters(expression, target, awaitPromise, options?.ResultOwnership, options?.SerializationOptions, options?.UserActivation); - return await Broker.ExecuteCommandAsync(new EvaluateCommand(@params), options, _jsonContext.EvaluateCommand, _jsonContext.EvaluateResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new EvaluateCommand(@params), options, JsonContext.EvaluateCommand, JsonContext.EvaluateResult).ConfigureAwait(false); } public async Task EvaluateAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string expression, bool awaitPromise, Target target, EvaluateOptions? options = null) @@ -48,7 +44,7 @@ public async Task CallFunctionAsync([StringSyntax(StringSyntaxCo { var @params = new CallFunctionParameters(functionDeclaration, awaitPromise, target, options?.Arguments, options?.ResultOwnership, options?.SerializationOptions, options?.This, options?.UserActivation); - return await Broker.ExecuteCommandAsync(new CallFunctionCommand(@params), options, _jsonContext.CallFunctionCommand, _jsonContext.EvaluateResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new CallFunctionCommand(@params), options, JsonContext.CallFunctionCommand, JsonContext.EvaluateResult).ConfigureAwait(false); } public async Task CallFunctionAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null) @@ -62,120 +58,57 @@ public async Task DisownAsync(IEnumerable handles, Target { var @params = new DisownParameters(handles, target); - return await Broker.ExecuteCommandAsync(new DisownCommand(@params), options, _jsonContext.DisownCommand, _jsonContext.DisownResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new DisownCommand(@params), options, JsonContext.DisownCommand, JsonContext.DisownResult).ConfigureAwait(false); } public async Task GetRealmsAsync(GetRealmsOptions? options = null) { var @params = new GetRealmsParameters(options?.Context, options?.Type); - return await Broker.ExecuteCommandAsync(new GetRealmsCommand(@params), options, _jsonContext.GetRealmsCommand, _jsonContext.GetRealmsResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new GetRealmsCommand(@params), options, JsonContext.GetRealmsCommand, JsonContext.GetRealmsResult).ConfigureAwait(false); } public async Task AddPreloadScriptAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string functionDeclaration, AddPreloadScriptOptions? options = null) { var @params = new AddPreloadScriptParameters(functionDeclaration, options?.Arguments, options?.Contexts, options?.Sandbox); - return await Broker.ExecuteCommandAsync(new AddPreloadScriptCommand(@params), options, _jsonContext.AddPreloadScriptCommand, _jsonContext.AddPreloadScriptResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new AddPreloadScriptCommand(@params), options, JsonContext.AddPreloadScriptCommand, JsonContext.AddPreloadScriptResult).ConfigureAwait(false); } public async Task RemovePreloadScriptAsync(PreloadScript script, RemovePreloadScriptOptions? options = null) { var @params = new RemovePreloadScriptParameters(script); - return await Broker.ExecuteCommandAsync(new RemovePreloadScriptCommand(@params), options, _jsonContext.RemovePreloadScriptCommand, _jsonContext.RemovePreloadScriptResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new RemovePreloadScriptCommand(@params), options, JsonContext.RemovePreloadScriptCommand, JsonContext.RemovePreloadScriptResult).ConfigureAwait(false); } public async Task OnMessageAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("script.message", handler, options, _jsonContext.MessageEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("script.message", handler, options, JsonContext.MessageEventArgs).ConfigureAwait(false); } public async Task OnMessageAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("script.message", handler, options, _jsonContext.MessageEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("script.message", handler, options, JsonContext.MessageEventArgs).ConfigureAwait(false); } public async Task OnRealmCreatedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("script.realmCreated", handler, options, _jsonContext.RealmInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("script.realmCreated", handler, options, JsonContext.RealmInfo).ConfigureAwait(false); } public async Task OnRealmCreatedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("script.realmCreated", handler, options, _jsonContext.RealmInfo).ConfigureAwait(false); + return await Broker.SubscribeAsync("script.realmCreated", handler, options, JsonContext.RealmInfo).ConfigureAwait(false); } public async Task OnRealmDestroyedAsync(Func handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("script.realmDestroyed", handler, options, _jsonContext.RealmDestroyedEventArgs).ConfigureAwait(false); + return await Broker.SubscribeAsync("script.realmDestroyed", handler, options, JsonContext.RealmDestroyedEventArgs).ConfigureAwait(false); } public async Task OnRealmDestroyedAsync(Action handler, SubscriptionOptions? options = null) { - return await Broker.SubscribeAsync("script.realmDestroyed", handler, options, _jsonContext.RealmDestroyedEventArgs).ConfigureAwait(false); - } - - protected override void Initialize(JsonSerializerOptions options) - { - _jsonContext = new ScriptJsonSerializerContext(options); + return await Broker.SubscribeAsync("script.realmDestroyed", handler, options, JsonContext.RealmDestroyedEventArgs).ConfigureAwait(false); } } - -#region https://github.com/dotnet/runtime/issues/72604 -[JsonSerializable(typeof(EvaluateResultSuccess))] -[JsonSerializable(typeof(EvaluateResultException))] - -[JsonSerializable(typeof(NumberRemoteValue))] -[JsonSerializable(typeof(BooleanRemoteValue))] -[JsonSerializable(typeof(BigIntRemoteValue))] -[JsonSerializable(typeof(StringRemoteValue))] -[JsonSerializable(typeof(NullRemoteValue))] -[JsonSerializable(typeof(UndefinedRemoteValue))] -[JsonSerializable(typeof(SymbolRemoteValue))] -[JsonSerializable(typeof(ArrayRemoteValue))] -[JsonSerializable(typeof(ObjectRemoteValue))] -[JsonSerializable(typeof(FunctionRemoteValue))] -[JsonSerializable(typeof(RegExpRemoteValue))] -[JsonSerializable(typeof(DateRemoteValue))] -[JsonSerializable(typeof(MapRemoteValue))] -[JsonSerializable(typeof(SetRemoteValue))] -[JsonSerializable(typeof(WeakMapRemoteValue))] -[JsonSerializable(typeof(WeakSetRemoteValue))] -[JsonSerializable(typeof(GeneratorRemoteValue))] -[JsonSerializable(typeof(ErrorRemoteValue))] -[JsonSerializable(typeof(ProxyRemoteValue))] -[JsonSerializable(typeof(PromiseRemoteValue))] -[JsonSerializable(typeof(TypedArrayRemoteValue))] -[JsonSerializable(typeof(ArrayBufferRemoteValue))] -[JsonSerializable(typeof(NodeListRemoteValue))] -[JsonSerializable(typeof(HtmlCollectionRemoteValue))] -[JsonSerializable(typeof(NodeRemoteValue))] -[JsonSerializable(typeof(WindowProxyRemoteValue))] - -[JsonSerializable(typeof(WindowRealmInfo))] -[JsonSerializable(typeof(DedicatedWorkerRealmInfo))] -[JsonSerializable(typeof(SharedWorkerRealmInfo))] -[JsonSerializable(typeof(ServiceWorkerRealmInfo))] -[JsonSerializable(typeof(WorkerRealmInfo))] -[JsonSerializable(typeof(PaintWorkletRealmInfo))] -[JsonSerializable(typeof(AudioWorkletRealmInfo))] -[JsonSerializable(typeof(WorkletRealmInfo))] -#endregion - -[JsonSerializable(typeof(AddPreloadScriptCommand))] -[JsonSerializable(typeof(AddPreloadScriptResult))] -[JsonSerializable(typeof(DisownCommand))] -[JsonSerializable(typeof(DisownResult))] -[JsonSerializable(typeof(CallFunctionCommand))] -[JsonSerializable(typeof(EvaluateResult))] -[JsonSerializable(typeof(EvaluateCommand))] -[JsonSerializable(typeof(EvaluateResult))] -[JsonSerializable(typeof(GetRealmsCommand))] -[JsonSerializable(typeof(GetRealmsResult))] -[JsonSerializable(typeof(RemovePreloadScriptCommand))] -[JsonSerializable(typeof(RemovePreloadScriptResult))] - -[JsonSerializable(typeof(MessageEventArgs))] -[JsonSerializable(typeof(RealmDestroyedEventArgs))] -internal partial class ScriptJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/src/webdriver/BiDi/Session/SessionModule.cs b/dotnet/src/webdriver/BiDi/Session/SessionModule.cs index 368349c1a1dc4..aa61ee6cfd23b 100644 --- a/dotnet/src/webdriver/BiDi/Session/SessionModule.cs +++ b/dotnet/src/webdriver/BiDi/Session/SessionModule.cs @@ -18,61 +18,40 @@ // using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Session; internal sealed class SessionModule : Module { - private SessionJsonSerializerContext _jsonContext = null!; - public async Task StatusAsync(StatusOptions? options = null) { - return await Broker.ExecuteCommandAsync(new StatusCommand(), options, _jsonContext.StatusCommand, _jsonContext.StatusResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new StatusCommand(), options, JsonContext.StatusCommand, JsonContext.StatusResult).ConfigureAwait(false); } public async Task SubscribeAsync(IEnumerable events, SubscribeOptions? options = null) { var @params = new SubscribeParameters(events, options?.Contexts); - return await Broker.ExecuteCommandAsync(new(@params), options, _jsonContext.SubscribeCommand, _jsonContext.SubscribeResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new(@params), options, JsonContext.SubscribeCommand, JsonContext.SubscribeResult).ConfigureAwait(false); } public async Task UnsubscribeAsync(IEnumerable subscriptions, UnsubscribeByIdOptions? options = null) { var @params = new UnsubscribeByIdParameters(subscriptions); - return await Broker.ExecuteCommandAsync(new UnsubscribeByIdCommand(@params), options, _jsonContext.UnsubscribeByIdCommand, _jsonContext.UnsubscribeResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new UnsubscribeByIdCommand(@params), options, JsonContext.UnsubscribeByIdCommand, JsonContext.UnsubscribeResult).ConfigureAwait(false); } public async Task NewAsync(CapabilitiesRequest capabilitiesRequest, NewOptions? options = null) { var @params = new NewParameters(capabilitiesRequest); - return await Broker.ExecuteCommandAsync(new NewCommand(@params), options, _jsonContext.NewCommand, _jsonContext.NewResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new NewCommand(@params), options, JsonContext.NewCommand, JsonContext.NewResult).ConfigureAwait(false); } public async Task EndAsync(EndOptions? options = null) { - return await Broker.ExecuteCommandAsync(new EndCommand(), options, _jsonContext.EndCommand, _jsonContext.EndResult).ConfigureAwait(false); - } - - protected override void Initialize(JsonSerializerOptions options) - { - _jsonContext = new SessionJsonSerializerContext(options); + return await Broker.ExecuteCommandAsync(new EndCommand(), options, JsonContext.EndCommand, JsonContext.EndResult).ConfigureAwait(false); } } - -[JsonSerializable(typeof(StatusCommand))] -[JsonSerializable(typeof(StatusResult))] -[JsonSerializable(typeof(NewCommand))] -[JsonSerializable(typeof(NewResult))] -[JsonSerializable(typeof(EndCommand))] -[JsonSerializable(typeof(EndResult))] -[JsonSerializable(typeof(SubscribeCommand))] -[JsonSerializable(typeof(SubscribeResult))] -[JsonSerializable(typeof(UnsubscribeByIdCommand))] -[JsonSerializable(typeof(UnsubscribeResult))] -internal partial class SessionJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs b/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs index d2ef4c263f515..cf17d11332bc0 100644 --- a/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs +++ b/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs @@ -17,47 +17,30 @@ // under the License. // -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Storage; public sealed class StorageModule : Module { - private StorageJsonSerializerContext _jsonContext = null!; - public async Task GetCookiesAsync(GetCookiesOptions? options = null) { var @params = new GetCookiesParameters(options?.Filter, options?.Partition); - return await Broker.ExecuteCommandAsync(new GetCookiesCommand(@params), options, _jsonContext.GetCookiesCommand, _jsonContext.GetCookiesResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new GetCookiesCommand(@params), options, JsonContext.GetCookiesCommand, JsonContext.GetCookiesResult).ConfigureAwait(false); } public async Task DeleteCookiesAsync(DeleteCookiesOptions? options = null) { var @params = new DeleteCookiesParameters(options?.Filter, options?.Partition); - return await Broker.ExecuteCommandAsync(new DeleteCookiesCommand(@params), options, _jsonContext.DeleteCookiesCommand, _jsonContext.DeleteCookiesResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new DeleteCookiesCommand(@params), options, JsonContext.DeleteCookiesCommand, JsonContext.DeleteCookiesResult).ConfigureAwait(false); } public async Task SetCookieAsync(PartialCookie cookie, SetCookieOptions? options = null) { var @params = new SetCookieParameters(cookie, options?.Partition); - return await Broker.ExecuteCommandAsync(new SetCookieCommand(@params), options, _jsonContext.SetCookieCommand, _jsonContext.SetCookieResult).ConfigureAwait(false); - } - - protected override void Initialize(JsonSerializerOptions options) - { - _jsonContext = new StorageJsonSerializerContext(options); + return await Broker.ExecuteCommandAsync(new SetCookieCommand(@params), options, JsonContext.SetCookieCommand, JsonContext.SetCookieResult).ConfigureAwait(false); } } - -[JsonSerializable(typeof(GetCookiesCommand))] -[JsonSerializable(typeof(GetCookiesResult))] -[JsonSerializable(typeof(SetCookieCommand))] -[JsonSerializable(typeof(SetCookieResult))] -[JsonSerializable(typeof(DeleteCookiesCommand))] -[JsonSerializable(typeof(DeleteCookiesResult))] -internal partial class StorageJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/src/webdriver/BiDi/WebExtension/Extension.cs b/dotnet/src/webdriver/BiDi/WebExtension/Extension.cs index c19beb1579e70..db19136a2fc2e 100644 --- a/dotnet/src/webdriver/BiDi/WebExtension/Extension.cs +++ b/dotnet/src/webdriver/BiDi/WebExtension/Extension.cs @@ -17,24 +17,25 @@ // under the License. // +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.WebExtension; public sealed class Extension { - private readonly BiDi _bidi; - - public Extension(BiDi bidi, string id) + internal Extension(string id) { - _bidi = bidi; Id = id; } internal string Id { get; } + [JsonIgnore] + public BiDi BiDi { get; internal set; } + public Task UninstallAsync(UninstallOptions? options = null) { - return _bidi.WebExtension.UninstallAsync(this, options); + return BiDi.WebExtension.UninstallAsync(this, options); } } diff --git a/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs b/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs index 86cfea9d1f9a5..365d6e18eb860 100644 --- a/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs +++ b/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs @@ -17,38 +17,23 @@ // under the License. // -using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.WebExtension; public sealed class WebExtensionModule : Module { - private WebExtensionJsonSerializerContext _jsonContext = null!; - public async Task InstallAsync(ExtensionData extensionData, InstallOptions? options = null) { var @params = new InstallParameters(extensionData); - return await Broker.ExecuteCommandAsync(new InstallCommand(@params), options, _jsonContext.InstallCommand, _jsonContext.InstallResult).ConfigureAwait(false); + return await Broker.ExecuteCommandAsync(new InstallCommand(@params), options, JsonContext.InstallCommand, JsonContext.InstallResult).ConfigureAwait(false); } public async Task UninstallAsync(Extension extension, UninstallOptions? options = null) { var @params = new UninstallParameters(extension); - return await Broker.ExecuteCommandAsync(new UninstallCommand(@params), options, _jsonContext.UninstallCommand, _jsonContext.UninstallResult).ConfigureAwait(false); - } - - protected override void Initialize(JsonSerializerOptions options) - { - _jsonContext = new WebExtensionJsonSerializerContext(options); + return await Broker.ExecuteCommandAsync(new UninstallCommand(@params), options, JsonContext.UninstallCommand, JsonContext.UninstallResult).ConfigureAwait(false); } } - -[JsonSerializable(typeof(InstallCommand))] -[JsonSerializable(typeof(InstallResult))] -[JsonSerializable(typeof(UninstallCommand))] -[JsonSerializable(typeof(UninstallResult))] -internal partial class WebExtensionJsonSerializerContext : JsonSerializerContext;