Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dotnet/src/webdriver/BiDi/BiDi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ private BiDi() { }

public Emulation.IEmulationModule Emulation => AsModule<Emulation.EmulationModule>();

public static async Task<IBiDi> ConnectAsync(string url, BiDiOptions? options = null, CancellationToken cancellationToken = default)
public static async Task<IBiDi> ConnectAsync(string url, Action<BiDiOptionsBuilder>? configure = null, CancellationToken cancellationToken = default)
{
var transport = await WebSocketTransport.ConnectAsync(new Uri(url), cancellationToken).ConfigureAwait(false);
BiDiOptionsBuilder builder = new();
configure?.Invoke(builder);

var transport = await builder.TransportFactory(new Uri(url), cancellationToken).ConfigureAwait(false);

BiDi bidi = new();

Expand Down
24 changes: 0 additions & 24 deletions dotnet/src/webdriver/BiDi/BiDiOptions.cs

This file was deleted.

48 changes: 48 additions & 0 deletions dotnet/src/webdriver/BiDi/BiDiOptionsBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// <copyright file="BiDiOptionsBuilder.cs" company="Selenium Committers">
// 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.
// </copyright>

using System.Net.WebSockets;

namespace OpenQA.Selenium.BiDi;

/// <summary>
/// Provides a fluent API for configuring BiDi connection options,
/// such as the underlying transport mechanism.
/// </summary>
public sealed class BiDiOptionsBuilder
{
internal Func<Uri, CancellationToken, Task<ITransport>> TransportFactory { get; private set; }
= (uri, ct) => WebSocketTransport.ConnectAsync(uri, null, ct);

/// <summary>
/// Configures the BiDi connection to use a WebSocket transport.
/// </summary>
/// <remarks>
/// WebSocket is the default transport; calling this method is only necessary
/// when you need to customize the underlying <see cref="ClientWebSocketOptions"/>
/// (e.g., to set headers, proxy, or certificates).
/// </remarks>
/// <param name="configure">An optional action to configure the <see cref="ClientWebSocketOptions"/> before connecting.</param>
/// <returns>The current <see cref="BiDiOptionsBuilder"/> instance for chaining.</returns>
public BiDiOptionsBuilder UseWebSocket(Action<ClientWebSocketOptions>? configure = null)
{
TransportFactory = (uri, ct) => WebSocketTransport.ConnectAsync(uri, configure, ct);
return this;
}
}
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace OpenQA.Selenium.BiDi;

public static class WebDriverExtensions
{
Comment thread
nvborisenko marked this conversation as resolved.
public static async Task<IBiDi> AsBiDiAsync(this IWebDriver webDriver, BiDiOptions? options = null, CancellationToken cancellationToken = default)
public static async Task<IBiDi> AsBiDiAsync(this IWebDriver webDriver, Action<BiDiOptionsBuilder>? configure = null, CancellationToken cancellationToken = default)
{
if (webDriver is null) throw new ArgumentNullException(nameof(webDriver));

Expand All @@ -34,7 +34,7 @@ public static async Task<IBiDi> AsBiDiAsync(this IWebDriver webDriver, BiDiOptio

if (webSocketUrl is null) throw new BiDiException("The driver is not compatible with bidirectional protocol or \"webSocketUrl\" not enabled in driver options.");

var bidi = await BiDi.ConnectAsync(webSocketUrl, options, cancellationToken).ConfigureAwait(false);
var bidi = await BiDi.ConnectAsync(webSocketUrl, configure, cancellationToken).ConfigureAwait(false);

return bidi;
}
Expand Down
4 changes: 3 additions & 1 deletion dotnet/src/webdriver/BiDi/WebSocketTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ sealed class WebSocketTransport(ClientWebSocket webSocket) : ITransport
private readonly SemaphoreSlim _socketSendSemaphoreSlim = new(1, 1);
private readonly MemoryStream _sharedMemoryStream = new();

public static async Task<WebSocketTransport> ConnectAsync(Uri uri, CancellationToken cancellationToken)
public static async Task<ITransport> ConnectAsync(Uri uri, Action<ClientWebSocketOptions>? configure, CancellationToken cancellationToken)
{
ClientWebSocket webSocket = new();

try
{
configure?.Invoke(webSocket.Options);

await webSocket.ConnectAsync(uri, cancellationToken).ConfigureAwait(false);
}
catch (Exception)
Expand Down
Loading