diff --git a/dotnet/src/webdriver/BiDi/BiDi.cs b/dotnet/src/webdriver/BiDi/BiDi.cs index f008d32a9394f..62e34dbc4de3b 100644 --- a/dotnet/src/webdriver/BiDi/BiDi.cs +++ b/dotnet/src/webdriver/BiDi/BiDi.cs @@ -53,12 +53,14 @@ private BiDi() { } public Emulation.IEmulationModule Emulation => AsModule(); - public static async Task ConnectAsync(string url, Action? configure = null, CancellationToken cancellationToken = default) + public static async Task ConnectAsync(Action configure, CancellationToken cancellationToken = default) { + if (configure is null) throw new ArgumentNullException(nameof(configure)); + BiDiOptionsBuilder builder = new(); - configure?.Invoke(builder); + configure(builder); - var transport = await builder.TransportFactory(new Uri(url), cancellationToken).ConfigureAwait(false); + var transport = await builder.TransportFactory(cancellationToken).ConfigureAwait(false); BiDi bidi = new(); diff --git a/dotnet/src/webdriver/BiDi/BiDiOptionsBuilder.cs b/dotnet/src/webdriver/BiDi/BiDiOptionsBuilder.cs index 96b17b516bb56..f8f4cba9bfcef 100644 --- a/dotnet/src/webdriver/BiDi/BiDiOptionsBuilder.cs +++ b/dotnet/src/webdriver/BiDi/BiDiOptionsBuilder.cs @@ -27,22 +27,21 @@ namespace OpenQA.Selenium.BiDi; /// public sealed class BiDiOptionsBuilder { - internal Func> TransportFactory { get; private set; } - = (uri, ct) => WebSocketTransport.ConnectAsync(uri, null, ct); + private Func>? _transportFactory; /// - /// Configures the BiDi connection to use a WebSocket transport. + /// Configures the BiDi connection to use a WebSocket transport with the specified URL. /// - /// - /// WebSocket is the default transport; calling this method is only necessary - /// when you need to customize the underlying - /// (e.g., to set headers, proxy, or certificates). - /// + /// The WebSocket URL to connect to. /// An optional action to configure the before connecting. /// The current instance for chaining. - public BiDiOptionsBuilder UseWebSocket(Action? configure = null) + public BiDiOptionsBuilder UseWebSocket(string url, Action? configure = null) { - TransportFactory = (uri, ct) => WebSocketTransport.ConnectAsync(uri, configure, ct); + var uri = new Uri(url); + _transportFactory = ct => WebSocketTransport.ConnectAsync(uri, configure, ct); return this; } + + internal Func> TransportFactory + => _transportFactory ?? throw new BiDiException("Transport is not configured. Call UseWebSocket(url) on BiDiOptionsBuilder."); } diff --git a/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs b/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs index f3eed2d70a17d..9fb4473705aa2 100644 --- a/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs +++ b/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs @@ -34,7 +34,11 @@ public static async Task AsBiDiAsync(this IWebDriver webDriver, Action + { + configure?.Invoke(options); + options.UseWebSocket(webSocketUrl); + }, cancellationToken).ConfigureAwait(false); return bidi; }