Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
71 changes: 58 additions & 13 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
public class SessionId : IEquatable<SessionId>, IEquatable<string>
{
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 @@ -50,24 +55,64 @@ public override string ToString()
/// <returns>The hash code of the key</returns>
public override int GetHashCode()
{
return this.sessionOpaqueKey.GetHashCode();
return StringComparer.Ordinal.GetHashCode(this.sessionOpaqueKey);
}

/// <summary>
/// Indicates whether the current session ID value is the same as <paramref name="obj"/>.
/// </summary>
/// <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)
{
return Equals(obj as SessionId);
}

/// <summary>
/// Indicates whether the current session ID value is the same as <paramref name="other"/>.
/// </summary>
/// <param name="other">A value to compare with this session ID.</param>
/// <returns><see langword="true"/> if the current session ID is equal to the <paramref name="other"/> parameter; otherwise, <see langword="false"/>.</returns>
public bool Equals(SessionId? other)
{
return other is not null && Equals(other.sessionOpaqueKey);
}

/// <summary>
/// Indicates whether the current session ID value is the same as <paramref name="other"/>.
/// </summary>
/// <param name="other">A value to compare with this session ID></param>
/// <returns><see langword="true"/> if the current session ID is equal to the <paramref name="other"/> parameter; otherwise, <see langword="false"/>.</returns>
public bool Equals(string? other)
{
return string.Equals(this.sessionOpaqueKey, other, StringComparison.Ordinal);
}

/// <summary>
/// Compares two Sessions
/// Compares the two values to determine equality.
/// </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="left">The value to compare with <paramref name="right"/>.</param>
/// <param name="right">The value to compare with <paramref name="left"/>.</param>
/// <returns><see langword="true"/> if <paramref name="left"/> is equal to <paramref name="right"/>; otherwise, <see langword="false"/>.</returns>
public static bool operator ==(SessionId? left, SessionId? right)
{
bool objectsAreEqual = false;
SessionId other = obj as SessionId;
if (other != null)
if (left is null)
{
objectsAreEqual = this.sessionOpaqueKey.Equals(other.sessionOpaqueKey);
return right is null;
}

return objectsAreEqual;
return left.Equals(right);
}

/// <summary>
/// Compares the two values to determine inequality.
/// </summary>
/// <param name="left">The value to compare with <paramref name="right"/>.</param>
/// <param name="right">The value to compare with <paramref name="left"/>.</param>
/// <returns><see langword="true"/> if <paramref name="left"/> is not equal to <paramref name="right"/>; otherwise, <see langword="false"/>.</returns>
public static bool operator !=(SessionId? left, SessionId? right)
{
return !(left == right);
}
}
}
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ protected void StartSession(ICapabilities capabilities)

ReturnedCapabilities returnedCapabilities = new ReturnedCapabilities(rawCapabilities);
this.capabilities = returnedCapabilities;
this.sessionId = new SessionId(response.SessionId);
this.sessionId = response.SessionId is null ? null : new SessionId(response.SessionId);
}

/// <summary>
Expand Down