Skip to content

Commit b595dfc

Browse files
committed
[dotnet] Enable external BiDi modules
1 parent 3d15087 commit b595dfc

File tree

12 files changed

+42
-38
lines changed

12 files changed

+42
-38
lines changed

dotnet/src/webdriver/BiDi/BiDi.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ namespace OpenQA.Selenium.BiDi;
3030

3131
public sealed class BiDi : IAsyncDisposable
3232
{
33-
private readonly Broker _broker;
34-
private readonly JsonSerializerOptions _jsonOptions;
33+
internal Broker Broker { get; }
34+
internal JsonSerializerOptions JsonOptions { get; }
3535
private readonly BiDiJsonSerializerContext _jsonContext;
3636

37-
private BiDi(string url)
37+
public JsonSerializerOptions DefaultBiDiOptions()
3838
{
39-
var uri = new Uri(url);
40-
41-
_jsonOptions = new JsonSerializerOptions
39+
return new JsonSerializerOptions
4240
{
4341
PropertyNameCaseInsensitive = true,
4442
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
@@ -61,20 +59,27 @@ private BiDi(string url)
6159
new WebExtensionConverter(this),
6260
}
6361
};
62+
}
63+
64+
private BiDi(string url)
65+
{
66+
var uri = new Uri(url);
6467

65-
_jsonContext = new BiDiJsonSerializerContext(_jsonOptions);
66-
67-
_broker = new Broker(this, uri, _jsonOptions);
68-
SessionModule = Module.Create<Session.SessionModule>(this, _broker, _jsonOptions, _jsonContext);
69-
BrowsingContext = Module.Create<BrowsingContext.BrowsingContextModule>(this, _broker, _jsonOptions, _jsonContext);
70-
Browser = Module.Create<Browser.BrowserModule>(this, _broker, _jsonOptions, _jsonContext);
71-
Network = Module.Create<Network.NetworkModule>(this, _broker, _jsonOptions, _jsonContext);
72-
InputModule = Module.Create<Input.InputModule>(this, _broker, _jsonOptions, _jsonContext);
73-
Script = Module.Create<Script.ScriptModule>(this, _broker, _jsonOptions, _jsonContext);
74-
Log = Module.Create<Log.LogModule>(this, _broker, _jsonOptions, _jsonContext);
75-
Storage = Module.Create<Storage.StorageModule>(this, _broker, _jsonOptions, _jsonContext);
76-
WebExtension = Module.Create<WebExtension.WebExtensionModule>(this, _broker, _jsonOptions, _jsonContext);
77-
Emulation = Module.Create<Emulation.EmulationModule>(this, _broker, _jsonOptions, _jsonContext);
68+
JsonOptions = DefaultBiDiOptions();
69+
70+
_jsonContext = new BiDiJsonSerializerContext(JsonOptions);
71+
72+
Broker = new Broker(this, uri, JsonOptions);
73+
SessionModule = Module.Create<Session.SessionModule>(this, JsonOptions, _jsonContext);
74+
BrowsingContext = Module.Create<BrowsingContext.BrowsingContextModule>(this, JsonOptions, _jsonContext);
75+
Browser = Module.Create<Browser.BrowserModule>(this, JsonOptions, _jsonContext);
76+
Network = Module.Create<Network.NetworkModule>(this, JsonOptions, _jsonContext);
77+
InputModule = Module.Create<Input.InputModule>(this, JsonOptions, _jsonContext);
78+
Script = Module.Create<Script.ScriptModule>(this, JsonOptions, _jsonContext);
79+
Log = Module.Create<Log.LogModule>(this, JsonOptions, _jsonContext);
80+
Storage = Module.Create<Storage.StorageModule>(this, JsonOptions, _jsonContext);
81+
WebExtension = Module.Create<WebExtension.WebExtensionModule>(this, JsonOptions, _jsonContext);
82+
Emulation = Module.Create<Emulation.EmulationModule>(this, JsonOptions, _jsonContext);
7883
}
7984

8085
internal Session.SessionModule SessionModule { get; }
@@ -106,7 +111,7 @@ public static async Task<BiDi> ConnectAsync(string url, BiDiOptions? options = n
106111
{
107112
var bidi = new BiDi(url);
108113

109-
await bidi._broker.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
114+
await bidi.Broker.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
110115

111116
return bidi;
112117
}
@@ -118,7 +123,7 @@ public Task EndAsync(Session.EndOptions? options = null)
118123

119124
public async ValueTask DisposeAsync()
120125
{
121-
await _broker.DisposeAsync().ConfigureAwait(false);
126+
await Broker.DisposeAsync().ConfigureAwait(false);
122127
GC.SuppressFinalize(this);
123128
}
124129
}

dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
namespace OpenQA.Selenium.BiDi.Browser;
2424

25-
public sealed class BrowserModule : Module
25+
public sealed class BrowserModule : InternalModule
2626
{
2727
public async Task<CloseResult> CloseAsync(CloseOptions? options = null)
2828
{

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace OpenQA.Selenium.BiDi.BrowsingContext;
2525

26-
public sealed class BrowsingContextModule : Module
26+
public sealed class BrowsingContextModule : InternalModule
2727
{
2828
public async Task<CreateResult> CreateAsync(ContextType type, CreateOptions? options = null)
2929
{

dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
namespace OpenQA.Selenium.BiDi.Emulation;
2424

25-
public sealed class EmulationModule : Module
25+
public sealed class EmulationModule : InternalModule
2626
{
2727
public async Task<SetTimezoneOverrideResult> SetTimezoneOverrideAsync(string? timezone, SetTimezoneOverrideOptions? options = null)
2828
{

dotnet/src/webdriver/BiDi/Input/InputModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace OpenQA.Selenium.BiDi.Input;
2525

26-
public sealed class InputModule : Module
26+
public sealed class InputModule : InternalModule
2727
{
2828
public async Task<PerformActionsResult> PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable<SourceActions> actions, PerformActionsOptions? options = null)
2929
{

dotnet/src/webdriver/BiDi/Log/LogModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace OpenQA.Selenium.BiDi.Log;
2525

26-
public sealed class LogModule : Module
26+
public sealed class LogModule : InternalModule
2727
{
2828
public async Task<Subscription> OnEntryAddedAsync(Func<LogEntry, Task> handler, SubscriptionOptions? options = null)
2929
{

dotnet/src/webdriver/BiDi/Module.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,28 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Communication;
21-
using OpenQA.Selenium.BiDi.Communication.Json;
2221
using System.Text.Json;
22+
using System.Text.Json.Serialization;
2323

2424
namespace OpenQA.Selenium.BiDi;
2525

2626
public abstract class Module
2727
{
2828
protected Broker Broker { get; private set; }
2929

30-
internal BiDiJsonSerializerContext JsonContext { get; private set; }
30+
internal JsonSerializerContext JsonContext { get; private set; }
3131

32-
protected virtual void Initialize(JsonSerializerOptions options) { }
32+
protected abstract JsonSerializerContext Initialize(JsonSerializerOptions options);
3333

34-
internal static TModule Create<TModule>(BiDi bidi, Broker broker, JsonSerializerOptions jsonOptions, BiDiJsonSerializerContext context)
34+
public static TModule Create<TModule>(BiDi bidi, JsonSerializerOptions jsonOptions, JsonSerializerContext? cachedContext = null)
3535
where TModule : Module, new()
3636
{
3737
TModule module = new()
3838
{
39-
Broker = broker,
40-
JsonContext = context
39+
Broker = bidi.Broker,
4140
};
4241

43-
module.Initialize(jsonOptions);
42+
module.JsonContext = cachedContext ?? module.Initialize(jsonOptions);
4443

4544
return module;
4645
}

dotnet/src/webdriver/BiDi/Network/NetworkModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace OpenQA.Selenium.BiDi.Network;
2626

27-
public sealed partial class NetworkModule : Module
27+
public sealed partial class NetworkModule : InternalModule
2828
{
2929
public async Task<Collector> AddDataCollectorAsync(IEnumerable<DataType> DataTypes, int MaxEncodedDataSize, AddDataCollectorOptions? options = null)
3030
{

dotnet/src/webdriver/BiDi/Script/ScriptModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace OpenQA.Selenium.BiDi.Script;
2626

27-
public sealed class ScriptModule : Module
27+
public sealed class ScriptModule : InternalModule
2828
{
2929
public async Task<EvaluateResult> EvaluateAsync(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
3030
{

dotnet/src/webdriver/BiDi/Session/SessionModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace OpenQA.Selenium.BiDi.Session;
2525

26-
internal sealed class SessionModule : Module
26+
internal sealed class SessionModule : InternalModule
2727
{
2828
public async Task<StatusResult> StatusAsync(StatusOptions? options = null)
2929
{

0 commit comments

Comments
 (0)