diff --git a/dotnet/src/webdriver/IHasCapabilities.cs b/dotnet/src/webdriver/IHasCapabilities.cs index e15d8038ae80c..e428317b79b70 100644 --- a/dotnet/src/webdriver/IHasCapabilities.cs +++ b/dotnet/src/webdriver/IHasCapabilities.cs @@ -17,6 +17,8 @@ // under the License. // +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/IWritableCapabilities.cs b/dotnet/src/webdriver/IWritableCapabilities.cs index e2ab8ddeaea61..9d7fac6a38481 100644 --- a/dotnet/src/webdriver/IWritableCapabilities.cs +++ b/dotnet/src/webdriver/IWritableCapabilities.cs @@ -17,6 +17,8 @@ // under the License. // +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/Remote/DesiredCapabilities.cs b/dotnet/src/webdriver/Remote/DesiredCapabilities.cs index 7e3301ad9b374..a4f5fd28ffb36 100644 --- a/dotnet/src/webdriver/Remote/DesiredCapabilities.cs +++ b/dotnet/src/webdriver/Remote/DesiredCapabilities.cs @@ -63,7 +63,7 @@ public DesiredCapabilities() /// DesiredCapabilities capabilities = new DesiredCapabilities(new Dictionary]]>(){["browserName","firefox"],["version",string.Empty],["javaScript",true]}); /// /// - public DesiredCapabilities(Dictionary rawMap) + public DesiredCapabilities(Dictionary? rawMap) { if (rawMap != null) { diff --git a/dotnet/src/webdriver/Remote/ReadOnlyDesiredCapabilities.cs b/dotnet/src/webdriver/Remote/ReadOnlyDesiredCapabilities.cs index 997c54816fe7c..d47a173d57a6c 100644 --- a/dotnet/src/webdriver/Remote/ReadOnlyDesiredCapabilities.cs +++ b/dotnet/src/webdriver/Remote/ReadOnlyDesiredCapabilities.cs @@ -23,6 +23,8 @@ using System.Collections.ObjectModel; using System.Globalization; +#nullable enable + namespace OpenQA.Selenium.Remote { /// @@ -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; } } @@ -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; } } @@ -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; @@ -117,18 +105,12 @@ public bool AcceptInsecureCerts /// /// Gets the underlying Dictionary for a given set of capabilities. /// - IDictionary IHasCapabilitiesDictionary.CapabilitiesDictionary - { - get { return this.CapabilitiesDictionary; } - } + IDictionary IHasCapabilitiesDictionary.CapabilitiesDictionary => this.CapabilitiesDictionary; /// /// Gets the underlying Dictionary for a given set of capabilities. /// - internal IDictionary CapabilitiesDictionary - { - get { return new ReadOnlyDictionary(this.capabilities); } - } + internal IDictionary CapabilitiesDictionary => new ReadOnlyDictionary(this.capabilities); /// /// Gets the capability value with the specified name. @@ -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; } } @@ -167,20 +149,19 @@ public bool HasCapability(string capability) /// The capability to get. /// An object associated with the capability, or /// if the capability is not set on the browser. - 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; } /// @@ -219,20 +200,19 @@ public override string ToString() /// /// DesiredCapabilities you wish to compare /// true if they are the same or false if they are not - 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; } @@ -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; } diff --git a/dotnet/src/webdriver/Remote/RemoteSessionSettings.cs b/dotnet/src/webdriver/Remote/RemoteSessionSettings.cs index 2816439866c27..23e3f34127033 100644 --- a/dotnet/src/webdriver/Remote/RemoteSessionSettings.cs +++ b/dotnet/src/webdriver/Remote/RemoteSessionSettings.cs @@ -22,6 +22,8 @@ using System.Collections.Generic; using System.Globalization; +#nullable enable + namespace OpenQA.Selenium { /// @@ -32,10 +34,10 @@ public class RemoteSessionSettings : ICapabilities private const string FirstMatchCapabilityName = "firstMatch"; private const string AlwaysMatchCapabilityName = "alwaysMatch"; - private readonly List reservedSettingNames = new List() { FirstMatchCapabilityName, AlwaysMatchCapabilityName }; - private DriverOptions mustMatchDriverOptions; - private List firstMatchOptions = new List(); - private Dictionary remoteMetadataSettings = new Dictionary(); + private readonly HashSet reservedSettingNames = new HashSet() { FirstMatchCapabilityName, AlwaysMatchCapabilityName }; + private DriverOptions? mustMatchDriverOptions; + private readonly List firstMatchOptions = new List(); + private readonly Dictionary remoteMetadataSettings = new Dictionary(); /// /// Creates a new instance of the class. @@ -69,7 +71,7 @@ public RemoteSessionSettings(DriverOptions mustMatchDriverOptions, params Driver /// /// Gets a value indicating the options that must be matched by the remote end to create a session. /// - internal DriverOptions MustMatchDriverOptions => this.mustMatchDriverOptions; + internal DriverOptions? MustMatchDriverOptions => this.mustMatchDriverOptions; /// /// Gets a value indicating the number of options that may be matched by the remote end to create a session. @@ -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) @@ -203,7 +206,7 @@ public bool HasCapability(string capability) /// The capability to get. /// An object associated with the capability, or /// if the capability is not set in this set of capabilities. - public object GetCapability(string capability) + public object? GetCapability(string capability) { if (capability == AlwaysMatchCapabilityName) { @@ -227,9 +230,9 @@ public object GetCapability(string capability) /// Return a dictionary representation of this . /// /// A representation of this . - public Dictionary ToDictionary() + public Dictionary ToDictionary() { - Dictionary capabilitiesDictionary = new Dictionary(); + Dictionary capabilitiesDictionary = new Dictionary(); foreach (KeyValuePair remoteMetadataSetting in this.remoteMetadataSettings) { @@ -243,7 +246,7 @@ public Dictionary ToDictionary() if (this.firstMatchOptions.Count > 0) { - List optionsMatches = GetFirstMatchOptionsAsSerializableList(); + List optionsMatches = GetFirstMatchOptionsAsSerializableList(); capabilitiesDictionary["firstMatch"] = optionsMatches; } @@ -261,14 +264,14 @@ internal DriverOptions GetFirstMatchDriverOptions(int firstMatchIndex) return this.firstMatchOptions[firstMatchIndex]; } - private IDictionary GetAlwaysMatchOptionsAsSerializableDictionary() + private IDictionary? GetAlwaysMatchOptionsAsSerializableDictionary() { - return this.mustMatchDriverOptions.ToDictionary(); + return this.mustMatchDriverOptions?.ToDictionary(); } - private List GetFirstMatchOptionsAsSerializableList() + private List GetFirstMatchOptionsAsSerializableList() { - List optionsMatches = new List(this.firstMatchOptions.Count); + List optionsMatches = new List(this.firstMatchOptions.Count); foreach (DriverOptions options in this.firstMatchOptions) { optionsMatches.Add(options.ToDictionary());