Skip to content
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
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/IHasCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/IWritableCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Remote/DesiredCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public DesiredCapabilities()
/// DesiredCapabilities capabilities = new DesiredCapabilities(new Dictionary<![CDATA[<string,object>]]>(){["browserName","firefox"],["version",string.Empty],["javaScript",true]});
/// </code>
/// </example>
public DesiredCapabilities(Dictionary<string, object> rawMap)
public DesiredCapabilities(Dictionary<string, object>? rawMap)
{
if (rawMap != null)
{
Expand Down
60 changes: 20 additions & 40 deletions dotnet/src/webdriver/Remote/ReadOnlyDesiredCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
using System.Collections.ObjectModel;
using System.Globalization;

#nullable enable

namespace OpenQA.Selenium.Remote
{
/// <summary>
Expand Down Expand Up @@ -56,14 +58,7 @@ public string BrowserName
{
get
{
string name = string.Empty;
object capabilityValue = this.GetCapability(CapabilityType.BrowserName);
if (capabilityValue != null)
{
name = capabilityValue.ToString();
}

return name;
return this.GetCapability(CapabilityType.BrowserName)?.ToString() ?? string.Empty;
}
}

Expand All @@ -85,14 +80,7 @@ public string Version
{
get
{
string browserVersion = string.Empty;
object capabilityValue = this.GetCapability(CapabilityType.Version);
if (capabilityValue != null)
{
browserVersion = capabilityValue.ToString();
}

return browserVersion;
return this.GetCapability(CapabilityType.Version)?.ToString() ?? string.Empty;
}
}

Expand All @@ -104,7 +92,7 @@ public bool AcceptInsecureCerts
get
{
bool acceptSSLCerts = false;
object capabilityValue = this.GetCapability(CapabilityType.AcceptInsecureCertificates);
object? capabilityValue = this.GetCapability(CapabilityType.AcceptInsecureCertificates);
if (capabilityValue != null)
{
acceptSSLCerts = (bool)capabilityValue;
Expand All @@ -117,18 +105,12 @@ public bool AcceptInsecureCerts
/// <summary>
/// Gets the underlying Dictionary for a given set of capabilities.
/// </summary>
IDictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary
{
get { return this.CapabilitiesDictionary; }
}
IDictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary => this.CapabilitiesDictionary;

/// <summary>
/// Gets the underlying Dictionary for a given set of capabilities.
/// </summary>
internal IDictionary<string, object> CapabilitiesDictionary
{
get { return new ReadOnlyDictionary<string, object>(this.capabilities); }
}
internal IDictionary<string, object> CapabilitiesDictionary => new ReadOnlyDictionary<string, object>(this.capabilities);

/// <summary>
/// Gets the capability value with the specified name.
Expand All @@ -142,12 +124,12 @@ public object this[string capabilityName]
{
get
{
if (!this.capabilities.ContainsKey(capabilityName))
if (!this.capabilities.TryGetValue(capabilityName, out object? capabilityValue))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The capability {0} is not present in this set of capabilities", capabilityName));
}

return this.capabilities[capabilityName];
return capabilityValue;
}
}

Expand All @@ -167,20 +149,19 @@ public bool HasCapability(string capability)
/// <param name="capability">The capability to get.</param>
/// <returns>An object associated with the capability, or <see langword="null"/>
/// if the capability is not set on the browser.</returns>
public object GetCapability(string capability)
public object? GetCapability(string capability)
{
object capabilityValue = null;
if (this.capabilities.ContainsKey(capability))
if (this.capabilities.TryGetValue(capability, out object? capabilityValue))
{
capabilityValue = this.capabilities[capability];
string capabilityValueString = capabilityValue as string;
if (capability == CapabilityType.Platform && capabilityValueString != null)
if (capability == CapabilityType.Platform && capabilityValue is string capabilityValueString)
{
capabilityValue = Platform.FromString(capabilityValue.ToString());
capabilityValue = Platform.FromString(capabilityValueString);
}

return capabilityValue;
}

return capabilityValue;
return null;
}

/// <summary>
Expand Down Expand Up @@ -219,20 +200,19 @@ public override string ToString()
/// </summary>
/// <param name="obj">DesiredCapabilities you wish to compare</param>
/// <returns>true if they are the same or false if they are not</returns>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (this == obj)
{
return true;
}

DesiredCapabilities other = obj as DesiredCapabilities;
if (other == null)
if (obj is not DesiredCapabilities other)
{
return false;
}

if (this.BrowserName != null ? this.BrowserName != other.BrowserName : other.BrowserName != null)
if (this.BrowserName != other.BrowserName)
{
return false;
}
Expand All @@ -242,7 +222,7 @@ public override bool Equals(object obj)
return false;
}

if (this.Version != null ? this.Version != other.Version : other.Version != null)
if (this.Version != other.Version)
{
return false;
}
Expand Down
31 changes: 17 additions & 14 deletions dotnet/src/webdriver/Remote/RemoteSessionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using System.Collections.Generic;
using System.Globalization;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand All @@ -32,10 +34,10 @@ public class RemoteSessionSettings : ICapabilities
private const string FirstMatchCapabilityName = "firstMatch";
private const string AlwaysMatchCapabilityName = "alwaysMatch";

private readonly List<string> reservedSettingNames = new List<string>() { FirstMatchCapabilityName, AlwaysMatchCapabilityName };
private DriverOptions mustMatchDriverOptions;
private List<DriverOptions> firstMatchOptions = new List<DriverOptions>();
private Dictionary<string, object> remoteMetadataSettings = new Dictionary<string, object>();
private readonly HashSet<string> reservedSettingNames = new HashSet<string>() { FirstMatchCapabilityName, AlwaysMatchCapabilityName };
private DriverOptions? mustMatchDriverOptions;
private readonly List<DriverOptions> firstMatchOptions = new List<DriverOptions>();
private readonly Dictionary<string, object> remoteMetadataSettings = new Dictionary<string, object>();

/// <summary>
/// Creates a new instance of the <see cref="RemoteSessionSettings"/> class.
Expand Down Expand Up @@ -69,7 +71,7 @@ public RemoteSessionSettings(DriverOptions mustMatchDriverOptions, params Driver
/// <summary>
/// Gets a value indicating the options that must be matched by the remote end to create a session.
/// </summary>
internal DriverOptions MustMatchDriverOptions => this.mustMatchDriverOptions;
internal DriverOptions? MustMatchDriverOptions => this.mustMatchDriverOptions;

/// <summary>
/// Gets a value indicating the number of options that may be matched by the remote end to create a session.
Expand All @@ -91,7 +93,8 @@ public object this[string capabilityName]
{
if (capabilityName == AlwaysMatchCapabilityName)
{
return this.GetAlwaysMatchOptionsAsSerializableDictionary();
return this.GetAlwaysMatchOptionsAsSerializableDictionary()
?? throw new ArgumentException("The \"alwaysMatch\" value has not been set", nameof(capabilityName));
}

if (capabilityName == FirstMatchCapabilityName)
Expand Down Expand Up @@ -203,7 +206,7 @@ public bool HasCapability(string capability)
/// <param name="capability">The capability to get.</param>
/// <returns>An object associated with the capability, or <see langword="null"/>
/// if the capability is not set in this set of capabilities.</returns>
public object GetCapability(string capability)
public object? GetCapability(string capability)
{
if (capability == AlwaysMatchCapabilityName)
{
Expand All @@ -227,9 +230,9 @@ public object GetCapability(string capability)
/// Return a dictionary representation of this <see cref="RemoteSessionSettings"/>.
/// </summary>
/// <returns>A <see cref="Dictionary{TKey, TValue}"/> representation of this <see cref="RemoteSessionSettings"/>.</returns>
public Dictionary<string, object> ToDictionary()
public Dictionary<string, object?> ToDictionary()
{
Dictionary<string, object> capabilitiesDictionary = new Dictionary<string, object>();
Dictionary<string, object?> capabilitiesDictionary = new Dictionary<string, object?>();

foreach (KeyValuePair<string, object> remoteMetadataSetting in this.remoteMetadataSettings)
{
Expand All @@ -243,7 +246,7 @@ public Dictionary<string, object> ToDictionary()

if (this.firstMatchOptions.Count > 0)
{
List<object> optionsMatches = GetFirstMatchOptionsAsSerializableList();
List<object?> optionsMatches = GetFirstMatchOptionsAsSerializableList();

capabilitiesDictionary["firstMatch"] = optionsMatches;
}
Expand All @@ -261,14 +264,14 @@ internal DriverOptions GetFirstMatchDriverOptions(int firstMatchIndex)
return this.firstMatchOptions[firstMatchIndex];
}

private IDictionary<string, object> GetAlwaysMatchOptionsAsSerializableDictionary()
private IDictionary<string, object>? GetAlwaysMatchOptionsAsSerializableDictionary()
{
return this.mustMatchDriverOptions.ToDictionary();
return this.mustMatchDriverOptions?.ToDictionary();
}

private List<object> GetFirstMatchOptionsAsSerializableList()
private List<object?> GetFirstMatchOptionsAsSerializableList()
{
List<object> optionsMatches = new List<object>(this.firstMatchOptions.Count);
List<object?> optionsMatches = new List<object?>(this.firstMatchOptions.Count);
foreach (DriverOptions options in this.firstMatchOptions)
{
optionsMatches.Add(options.ToDictionary());
Expand Down