Skip to content
19 changes: 16 additions & 3 deletions dotnet/src/webdriver/BiDi/Broker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,23 @@ private void ProcessReceivedMessage(byte[]? data)
case "success":
if (id is null) throw new JsonException("The remote end responded with 'success' message type, but missed required 'id' property.");

if (_pendingCommands.TryGetValue(id.Value, out var successCommand))
if (_pendingCommands.TryGetValue(id.Value, out var command))
{
successCommand.TaskCompletionSource.SetResult((EmptyResult)JsonSerializer.Deserialize(ref resultReader, successCommand.JsonResultTypeInfo)!);
_pendingCommands.TryRemove(id.Value, out _);
try
{
var commandResult = JsonSerializer.Deserialize(ref resultReader, command.JsonResultTypeInfo)
?? throw new JsonException("Remote end returned null command result in the 'result' property.");

command.TaskCompletionSource.SetResult((EmptyResult)commandResult);
}
catch (Exception ex)
{
command.TaskCompletionSource.SetException(ex);
}
finally
{
_pendingCommands.TryRemove(id.Value, out _);
}
}
else
{
Expand Down
22 changes: 18 additions & 4 deletions dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace OpenQA.Selenium.BiDi.Browser;

public sealed class BrowserModule : Module
{
private BiDiJsonSerializerContext _jsonContext = null!;
private BrowserJsonSerializerContext _jsonContext = null!;

public async Task<CloseResult> CloseAsync(CloseOptions? options = null)
{
return await Broker.ExecuteCommandAsync(new CloseCommand(), options, _jsonContext.Browser_CloseCommand, _jsonContext.Browser_CloseResult).ConfigureAwait(false);
return await Broker.ExecuteCommandAsync(new CloseCommand(), options, _jsonContext.CloseCommand, _jsonContext.CloseResult).ConfigureAwait(false);
}

public async Task<CreateUserContextResult> CreateUserContextAsync(CreateUserContextOptions? options = null)
Expand Down Expand Up @@ -80,6 +80,20 @@ public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorDeniedAsync(SetD

protected override void Initialize(JsonSerializerOptions options)
{
_jsonContext = new BiDiJsonSerializerContext(options);
_jsonContext = new BrowserJsonSerializerContext(options);
}
}

[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;
44 changes: 40 additions & 4 deletions dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Json;
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 BiDiJsonSerializerContext _jsonContext = null!;
private BrowsingContextJsonSerializerContext _jsonContext = null!;

public async Task<CreateResult> CreateAsync(ContextType type, CreateOptions? options = null)
{
Expand Down Expand Up @@ -67,7 +67,7 @@ public async Task<CloseResult> CloseAsync(BrowsingContext context, CloseOptions?
{
var @params = new CloseParameters(context, options?.PromptUnload);

return await Broker.ExecuteCommandAsync(new CloseCommand(@params), options, _jsonContext.BrowsingContext_CloseCommand, _jsonContext.BrowsingContext_CloseResult).ConfigureAwait(false);
return await Broker.ExecuteCommandAsync(new CloseCommand(@params), options, _jsonContext.CloseCommand, _jsonContext.CloseResult).ConfigureAwait(false);
}

public async Task<TraverseHistoryResult> TraverseHistoryAsync(BrowsingContext context, int delta, TraverseHistoryOptions? options = null)
Expand Down Expand Up @@ -254,6 +254,42 @@ public async Task<Subscription> OnUserPromptClosedAsync(Action<UserPromptClosedE

protected override void Initialize(JsonSerializerOptions options)
{
_jsonContext = new BiDiJsonSerializerContext(options);
_jsonContext = new BrowsingContextJsonSerializerContext(options);
}
}

[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;
22 changes: 19 additions & 3 deletions dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace OpenQA.Selenium.BiDi.Emulation;

public sealed class EmulationModule : Module
{
private BiDiJsonSerializerContext _jsonContext = null!;
private EmulationJsonSerializerContext _jsonContext = null!;

public async Task<SetTimezoneOverrideResult> SetTimezoneOverrideAsync(string? timezone, SetTimezoneOverrideOptions? options = null)
{
Expand Down Expand Up @@ -94,6 +94,22 @@ public async Task<SetGeolocationOverrideResult> SetGeolocationPositionErrorOverr

protected override void Initialize(JsonSerializerOptions options)
{
_jsonContext = new BiDiJsonSerializerContext(options);
_jsonContext = new EmulationJsonSerializerContext(options);
}
}

[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;
18 changes: 15 additions & 3 deletions dotnet/src/webdriver/BiDi/Input/InputModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Json;
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 BiDiJsonSerializerContext _jsonContext = null!;
private InputJsonSerializerContext _jsonContext = null!;

public async Task<PerformActionsResult> PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable<SourceActions> actions, PerformActionsOptions? options = null)
{
Expand All @@ -51,6 +51,18 @@ public async Task<SetFilesResult> SetFilesAsync(BrowsingContext.BrowsingContext

protected override void Initialize(JsonSerializerOptions options)
{
_jsonContext = new BiDiJsonSerializerContext(options);
_jsonContext = new InputJsonSerializerContext(options);
}
}

[JsonSerializable(typeof(PerformActionsCommand))]
[JsonSerializable(typeof(PerformActionsResult))]
[JsonSerializable(typeof(ReleaseActionsCommand))]
[JsonSerializable(typeof(ReleaseActionsResult))]
[JsonSerializable(typeof(SetFilesCommand))]
[JsonSerializable(typeof(SetFilesResult))]
[JsonSerializable(typeof(IEnumerable<IPointerSourceAction>))]
[JsonSerializable(typeof(IEnumerable<IKeySourceAction>))]
[JsonSerializable(typeof(IEnumerable<INoneSourceAction>))]
[JsonSerializable(typeof(IEnumerable<IWheelSourceAction>))]
internal partial class InputJsonSerializerContext : JsonSerializerContext;
Loading