diff --git a/dotnet/src/webdriver/SessionId.cs b/dotnet/src/webdriver/SessionId.cs index 7cf90da7f69d1..8f605d4731775 100644 --- a/dotnet/src/webdriver/SessionId.cs +++ b/dotnet/src/webdriver/SessionId.cs @@ -17,6 +17,10 @@ // under the License. // +using System; + +#nullable enable + namespace OpenQA.Selenium { /// @@ -24,15 +28,16 @@ namespace OpenQA.Selenium /// public class SessionId { - private string sessionOpaqueKey; + private readonly string sessionOpaqueKey; /// /// Initializes a new instance of the class /// /// Key for the session in use + /// If is . public SessionId(string opaqueKey) { - this.sessionOpaqueKey = opaqueKey; + this.sessionOpaqueKey = opaqueKey ?? throw new ArgumentNullException(nameof(opaqueKey)); } /// @@ -54,20 +59,13 @@ public override int GetHashCode() } /// - /// Compares two Sessions + /// Indicates whether the current session ID value is the same as . /// - /// Session to compare - /// True if they are equal or False if they are not - public override bool Equals(object obj) + /// The session to compare to. + /// if the values are equal; otherwise, . + public override bool Equals(object? obj) { - bool objectsAreEqual = false; - SessionId other = obj as SessionId; - if (other != null) - { - objectsAreEqual = this.sessionOpaqueKey.Equals(other.sessionOpaqueKey); - } - - return objectsAreEqual; + return obj is SessionId otherSession && this.sessionOpaqueKey.Equals(otherSession.sessionOpaqueKey); } } } diff --git a/dotnet/src/webdriver/WebDriver.cs b/dotnet/src/webdriver/WebDriver.cs index 9bcca8b28f8b3..672aa83b5b6e6 100644 --- a/dotnet/src/webdriver/WebDriver.cs +++ b/dotnet/src/webdriver/WebDriver.cs @@ -24,6 +24,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Threading.Tasks; @@ -44,7 +45,7 @@ public class WebDriver : IWebDriver, ISearchContext, IJavaScriptExecutor, IFinds private IFileDetector fileDetector = new DefaultFileDetector(); private NetworkManager network; private WebElementFactory elementFactory; - private SessionId sessionId; + private List registeredCommands = new List(); /// @@ -191,10 +192,7 @@ public bool IsActionExecutor /// /// Gets the for the current session of this driver. /// - public SessionId SessionId - { - get { return this.sessionId; } - } + public SessionId SessionId { get; private set; } /// /// Gets or sets the responsible for detecting @@ -612,7 +610,7 @@ protected virtual Response Execute(string driverCommandToExecute, /// A containing information about the success or failure of the command and any data returned by the command. protected virtual async Task ExecuteAsync(string driverCommandToExecute, Dictionary parameters) { - Command commandToExecute = new Command(this.sessionId, driverCommandToExecute, parameters); + Command commandToExecute = new Command(SessionId, driverCommandToExecute, parameters); Response commandResponse; @@ -641,6 +639,7 @@ protected virtual async Task ExecuteAsync(string driverCommandToExecut /// Starts a session with the driver /// /// Capabilities of the browser + [MemberNotNull(nameof(SessionId))] protected void StartSession(ICapabilities capabilities) { Dictionary parameters = new Dictionary(); @@ -679,7 +678,9 @@ protected void StartSession(ICapabilities capabilities) ReturnedCapabilities returnedCapabilities = new ReturnedCapabilities(rawCapabilities); this.capabilities = returnedCapabilities; - this.sessionId = new SessionId(response.SessionId); + + string sessionId = response.SessionId ?? throw new WebDriverException($"The remote end did not respond with ID of a session when it was required. {response.Value}"); + this.SessionId = new SessionId(sessionId); } /// @@ -723,7 +724,7 @@ protected virtual void Dispose(bool disposing) { try { - if (this.sessionId is not null) + if (this.SessionId is not null) { this.Execute(DriverCommand.Quit, null); } @@ -739,7 +740,7 @@ protected virtual void Dispose(bool disposing) } finally { - this.sessionId = null; + this.SessionId = null; } this.executor.Dispose(); }