Skip to content

Commit c73f9d9

Browse files
RenderMichaelsandeepsuryaprasad
authored andcommitted
[dotnet] Add nullability annotations to SessionId (SeleniumHQ#14841)
1 parent fa78ee0 commit c73f9d9

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

dotnet/src/webdriver/SessionId.cs

+12-14
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,27 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System;
21+
22+
#nullable enable
23+
2024
namespace OpenQA.Selenium
2125
{
2226
/// <summary>
2327
/// Provides a mechanism for maintaining a session for a test
2428
/// </summary>
2529
public class SessionId
2630
{
27-
private string sessionOpaqueKey;
31+
private readonly string sessionOpaqueKey;
2832

2933
/// <summary>
3034
/// Initializes a new instance of the <see cref="SessionId"/> class
3135
/// </summary>
3236
/// <param name="opaqueKey">Key for the session in use</param>
37+
/// <exception cref="ArgumentNullException">If <paramref name="opaqueKey"/> is <see langword="null"/>.</exception>
3338
public SessionId(string opaqueKey)
3439
{
35-
this.sessionOpaqueKey = opaqueKey;
40+
this.sessionOpaqueKey = opaqueKey ?? throw new ArgumentNullException(nameof(opaqueKey));
3641
}
3742

3843
/// <summary>
@@ -54,20 +59,13 @@ public override int GetHashCode()
5459
}
5560

5661
/// <summary>
57-
/// Compares two Sessions
62+
/// Indicates whether the current session ID value is the same as <paramref name="obj"/>.
5863
/// </summary>
59-
/// <param name="obj">Session to compare</param>
60-
/// <returns>True if they are equal or False if they are not</returns>
61-
public override bool Equals(object obj)
64+
/// <param name="obj">The session to compare to.</param>
65+
/// <returns><see langword="true"/> if the values are equal; otherwise, <see langword="false"/>.</returns>
66+
public override bool Equals(object? obj)
6267
{
63-
bool objectsAreEqual = false;
64-
SessionId other = obj as SessionId;
65-
if (other != null)
66-
{
67-
objectsAreEqual = this.sessionOpaqueKey.Equals(other.sessionOpaqueKey);
68-
}
69-
70-
return objectsAreEqual;
68+
return obj is SessionId otherSession && this.sessionOpaqueKey.Equals(otherSession.sessionOpaqueKey);
7169
}
7270
}
7371
}

dotnet/src/webdriver/WebDriver.cs

+10-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System.Collections;
2525
using System.Collections.Generic;
2626
using System.Collections.ObjectModel;
27+
using System.Diagnostics.CodeAnalysis;
2728
using System.Globalization;
2829
using System.Threading.Tasks;
2930

@@ -44,7 +45,7 @@ public class WebDriver : IWebDriver, ISearchContext, IJavaScriptExecutor, IFinds
4445
private IFileDetector fileDetector = new DefaultFileDetector();
4546
private NetworkManager network;
4647
private WebElementFactory elementFactory;
47-
private SessionId sessionId;
48+
4849
private List<string> registeredCommands = new List<string>();
4950

5051
/// <summary>
@@ -191,10 +192,7 @@ public bool IsActionExecutor
191192
/// <summary>
192193
/// Gets the <see cref="SessionId"/> for the current session of this driver.
193194
/// </summary>
194-
public SessionId SessionId
195-
{
196-
get { return this.sessionId; }
197-
}
195+
public SessionId SessionId { get; private set; }
198196

199197
/// <summary>
200198
/// Gets or sets the <see cref="IFileDetector"/> responsible for detecting
@@ -612,7 +610,7 @@ protected virtual Response Execute(string driverCommandToExecute,
612610
/// <returns>A <see cref="Response"/> containing information about the success or failure of the command and any data returned by the command.</returns>
613611
protected virtual async Task<Response> ExecuteAsync(string driverCommandToExecute, Dictionary<string, object> parameters)
614612
{
615-
Command commandToExecute = new Command(this.sessionId, driverCommandToExecute, parameters);
613+
Command commandToExecute = new Command(SessionId, driverCommandToExecute, parameters);
616614

617615
Response commandResponse;
618616

@@ -641,6 +639,7 @@ protected virtual async Task<Response> ExecuteAsync(string driverCommandToExecut
641639
/// Starts a session with the driver
642640
/// </summary>
643641
/// <param name="capabilities">Capabilities of the browser</param>
642+
[MemberNotNull(nameof(SessionId))]
644643
protected void StartSession(ICapabilities capabilities)
645644
{
646645
Dictionary<string, object> parameters = new Dictionary<string, object>();
@@ -679,7 +678,9 @@ protected void StartSession(ICapabilities capabilities)
679678

680679
ReturnedCapabilities returnedCapabilities = new ReturnedCapabilities(rawCapabilities);
681680
this.capabilities = returnedCapabilities;
682-
this.sessionId = new SessionId(response.SessionId);
681+
682+
string sessionId = response.SessionId ?? throw new WebDriverException($"The remote end did not respond with ID of a session when it was required. {response.Value}");
683+
this.SessionId = new SessionId(sessionId);
683684
}
684685

685686
/// <summary>
@@ -723,7 +724,7 @@ protected virtual void Dispose(bool disposing)
723724
{
724725
try
725726
{
726-
if (this.sessionId is not null)
727+
if (this.SessionId is not null)
727728
{
728729
this.Execute(DriverCommand.Quit, null);
729730
}
@@ -739,7 +740,7 @@ protected virtual void Dispose(bool disposing)
739740
}
740741
finally
741742
{
742-
this.sessionId = null;
743+
this.SessionId = null;
743744
}
744745
this.executor.Dispose();
745746
}

0 commit comments

Comments
 (0)