Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Add nullability annotations to SessionId #14841

Merged
merged 18 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions dotnet/src/webdriver/SessionId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@
// under the License.
// </copyright>

using System;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// Provides a mechanism for maintaining a session for a test
/// </summary>
public class SessionId
{
private string sessionOpaqueKey;
private readonly string sessionOpaqueKey;

/// <summary>
/// Initializes a new instance of the <see cref="SessionId"/> class
/// </summary>
/// <param name="opaqueKey">Key for the session in use</param>
/// <exception cref="ArgumentNullException">If <paramref name="opaqueKey"/> is <see langword="null"/>.</exception>
public SessionId(string opaqueKey)
{
this.sessionOpaqueKey = opaqueKey;
this.sessionOpaqueKey = opaqueKey ?? throw new ArgumentNullException(nameof(opaqueKey));
}

/// <summary>
Expand All @@ -54,20 +59,13 @@ public override int GetHashCode()
}

/// <summary>
/// Compares two Sessions
/// Indicates whether the current session ID value is the same as <paramref name="obj"/>.
/// </summary>
/// <param name="obj">Session to compare</param>
/// <returns>True if they are equal or False if they are not</returns>
public override bool Equals(object obj)
/// <param name="obj">The session to compare to.</param>
/// <returns><see langword="true"/> if the values are equal; otherwise, <see langword="false"/>.</returns>
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);
}
}
}
19 changes: 10 additions & 9 deletions dotnet/src/webdriver/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<string> registeredCommands = new List<string>();

/// <summary>
Expand Down Expand Up @@ -191,10 +192,7 @@ public bool IsActionExecutor
/// <summary>
/// Gets the <see cref="SessionId"/> for the current session of this driver.
/// </summary>
public SessionId SessionId
{
get { return this.sessionId; }
}
public SessionId SessionId { get; private set; }

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

Response commandResponse;

Expand Down Expand Up @@ -641,6 +639,7 @@ protected virtual async Task<Response> ExecuteAsync(string driverCommandToExecut
/// Starts a session with the driver
/// </summary>
/// <param name="capabilities">Capabilities of the browser</param>
[MemberNotNull(nameof(SessionId))]
protected void StartSession(ICapabilities capabilities)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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);
}
Expand All @@ -739,7 +740,7 @@ protected virtual void Dispose(bool disposing)
}
finally
{
this.sessionId = null;
this.SessionId = null;
}
this.executor.Dispose();
}
Expand Down
Loading