Skip to content

Commit 875e1d8

Browse files
committed
Avoid InvalidCastException and guard for null
1 parent 588bc7d commit 875e1d8

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs

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

2020
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
21+
using System;
2122
using System.Threading.Tasks;
2223

2324
#nullable enable
@@ -28,11 +29,18 @@ public static class WebDriverExtensions
2829
{
2930
public static async Task<BiDi> AsBiDiAsync(this IWebDriver webDriver)
3031
{
31-
var webSocketUrl = ((IHasCapabilities)webDriver).Capabilities.GetCapability("webSocketUrl");
32+
if (webDriver is null) throw new ArgumentNullException(nameof(webDriver));
33+
34+
string? webSocketUrl = null;
35+
36+
if (webDriver is IHasCapabilities hasCapabilities)
37+
{
38+
webSocketUrl = hasCapabilities.Capabilities.GetCapability("webSocketUrl")?.ToString();
39+
}
3240

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

35-
var bidi = await BiDi.ConnectAsync(webSocketUrl.ToString()!).ConfigureAwait(false);
43+
var bidi = await BiDi.ConnectAsync(webSocketUrl).ConfigureAwait(false);
3644

3745
return bidi;
3846
}

0 commit comments

Comments
 (0)