From 588bc7df58ac76ae2aa9d430759d3cf49b434534 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 26 Jan 2025 23:15:30 +0300 Subject: [PATCH 1/2] [dotnet] Improve bidi exception when it is not enabled --- dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs b/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs index 0a62e1467228a..6b5076bf8ae46 100644 --- a/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs +++ b/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs @@ -30,7 +30,7 @@ public static async Task AsBiDiAsync(this IWebDriver webDriver) { var webSocketUrl = ((IHasCapabilities)webDriver).Capabilities.GetCapability("webSocketUrl"); - if (webSocketUrl is null) throw new System.Exception("The driver is not compatible with bidirectional protocol or it is not enabled in driver options."); + 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.ToString()!).ConfigureAwait(false); From 875e1d8bde6b793223983cfb2548f87cd9474c33 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Mon, 27 Jan 2025 13:24:16 +0300 Subject: [PATCH 2/2] Avoid InvalidCastException and guard for null --- dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs b/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs index 6b5076bf8ae46..9d8f03511c062 100644 --- a/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs +++ b/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs @@ -18,6 +18,7 @@ // using OpenQA.Selenium.BiDi.Modules.BrowsingContext; +using System; using System.Threading.Tasks; #nullable enable @@ -28,11 +29,18 @@ public static class WebDriverExtensions { public static async Task AsBiDiAsync(this IWebDriver webDriver) { - var webSocketUrl = ((IHasCapabilities)webDriver).Capabilities.GetCapability("webSocketUrl"); + if (webDriver is null) throw new ArgumentNullException(nameof(webDriver)); + + string? webSocketUrl = null; + + 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.ToString()!).ConfigureAwait(false); + var bidi = await BiDi.ConnectAsync(webSocketUrl).ConfigureAwait(false); return bidi; }