Skip to content

Commit

Permalink
[dotnet] Improve bidi exception when it is not enabled (#15163)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko authored Jan 27, 2025
1 parent 9536fef commit ffe8d4b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// </copyright>

using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System;
using System.Threading.Tasks;

#nullable enable
Expand All @@ -28,11 +29,18 @@ public static class WebDriverExtensions
{
public static async Task<BiDi> AsBiDiAsync(this IWebDriver webDriver)
{
var webSocketUrl = ((IHasCapabilities)webDriver).Capabilities.GetCapability("webSocketUrl");
if (webDriver is null) throw new ArgumentNullException(nameof(webDriver));

if (webSocketUrl is null) throw new System.Exception("The driver is not compatible with bidirectional protocol or it is not enabled in driver options.");
string? webSocketUrl = null;

var bidi = await BiDi.ConnectAsync(webSocketUrl.ToString()!).ConfigureAwait(false);
if (webDriver is IHasCapabilities hasCapabilities)
{
webSocketUrl = hasCapabilities.Capabilities.GetCapability("webSocketUrl")?.ToString();
}

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).ConfigureAwait(false);

return bidi;
}
Expand Down

0 comments on commit ffe8d4b

Please sign in to comment.