diff --git a/dotnet/src/webdriver/ICapabilities.cs b/dotnet/src/webdriver/ICapabilities.cs index 1a66162ffb098..df0c8a776c77e 100644 --- a/dotnet/src/webdriver/ICapabilities.cs +++ b/dotnet/src/webdriver/ICapabilities.cs @@ -19,6 +19,8 @@ using System; +#nullable enable + namespace OpenQA.Selenium { /// @@ -49,6 +51,6 @@ public interface ICapabilities /// The capability to get. /// An object associated with the capability, or /// if the capability is not set on the browser. - object GetCapability(string capability); + object? GetCapability(string capability); } } diff --git a/dotnet/src/webdriver/Internal/AndroidOptions.cs b/dotnet/src/webdriver/Internal/AndroidOptions.cs index e2d9e5b94763c..d002d1e6acc81 100644 --- a/dotnet/src/webdriver/Internal/AndroidOptions.cs +++ b/dotnet/src/webdriver/Internal/AndroidOptions.cs @@ -19,6 +19,8 @@ using System; +#nullable enable + namespace OpenQA.Selenium.Internal { /// @@ -26,14 +28,11 @@ namespace OpenQA.Selenium.Internal /// public class AndroidOptions { - private string androidPackage; - private string androidDeviceSerial; - private string androidActivity; - /// /// Initializes a new instance of the class. /// /// + /// If is or . protected AndroidOptions(string androidPackage) { if (string.IsNullOrEmpty(androidPackage)) @@ -41,33 +40,22 @@ protected AndroidOptions(string androidPackage) throw new ArgumentException("The Android package cannot be null or the empty string", nameof(androidPackage)); } - this.androidPackage = androidPackage; + this.AndroidPackage = androidPackage; } /// /// The package name of the application to automate. /// - public string AndroidPackage - { - get { return this.androidPackage; } - } + public string AndroidPackage { get; } /// /// The serial number of the device on which to launch the application. /// - public string AndroidDeviceSerial - { - get { return this.androidDeviceSerial; } - set { this.androidDeviceSerial = value; } - } + public string? AndroidDeviceSerial { get; set; } /// /// Gets or sets the name of the Activity hosting the app. /// - public string AndroidActivity - { - get { return this.androidActivity; } - set { this.androidActivity = value; } - } + public string? AndroidActivity { get; set; } } } diff --git a/dotnet/src/webdriver/Internal/FileUtilities.cs b/dotnet/src/webdriver/Internal/FileUtilities.cs index 0d83a6927aeca..d288a58e5f296 100644 --- a/dotnet/src/webdriver/Internal/FileUtilities.cs +++ b/dotnet/src/webdriver/Internal/FileUtilities.cs @@ -23,6 +23,8 @@ using System.IO; using System.Reflection; +#nullable enable + namespace OpenQA.Selenium.Internal { /// @@ -40,7 +42,7 @@ internal static class FileUtilities /// if the copy is completed; otherwise . public static bool CopyDirectory(string sourceDirectory, string destinationDirectory) { - bool copyComplete = false; + bool copyComplete; DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(sourceDirectory); DirectoryInfo destinationDirectoryInfo = new DirectoryInfo(destinationDirectory); @@ -132,7 +134,7 @@ public static string FindFile(string fileName) // If it's not in the same directory as the executing assembly, // try looking in the system path. - string systemPath = Environment.GetEnvironmentVariable("PATH"); + string? systemPath = Environment.GetEnvironmentVariable("PATH"); if (!string.IsNullOrEmpty(systemPath)) { string expandedPath = Environment.ExpandEnvironmentVariables(systemPath); @@ -165,7 +167,7 @@ public static string FindFile(string fileName) public static string GetCurrentDirectory() { Assembly executingAssembly = typeof(FileUtilities).Assembly; - string location = null; + string? location = null; // Make sure not to call Path.GetDirectoryName if assembly location is null or empty if (!string.IsNullOrEmpty(executingAssembly.Location)) @@ -184,13 +186,13 @@ public static string GetCurrentDirectory() location = Directory.GetCurrentDirectory(); } - string currentDirectory = location; + string currentDirectory = location!; // If we're shadow copying, get the directory from the codebase instead if (AppDomain.CurrentDomain.ShadowCopyFiles) { Uri uri = new Uri(executingAssembly.CodeBase); - currentDirectory = Path.GetDirectoryName(uri.LocalPath); + currentDirectory = Path.GetDirectoryName(uri.LocalPath)!; } return currentDirectory; diff --git a/dotnet/src/webdriver/Internal/IFindsElement.cs b/dotnet/src/webdriver/Internal/IFindsElement.cs index a43055f9c91bd..7bcd08751a8fb 100644 --- a/dotnet/src/webdriver/Internal/IFindsElement.cs +++ b/dotnet/src/webdriver/Internal/IFindsElement.cs @@ -19,6 +19,8 @@ using System.Collections.ObjectModel; +#nullable enable + namespace OpenQA.Selenium.Internal { /// diff --git a/dotnet/src/webdriver/Internal/IHasCapabilitiesDictionary.cs b/dotnet/src/webdriver/Internal/IHasCapabilitiesDictionary.cs index 8c281818c4e0d..8723b5b11e580 100644 --- a/dotnet/src/webdriver/Internal/IHasCapabilitiesDictionary.cs +++ b/dotnet/src/webdriver/Internal/IHasCapabilitiesDictionary.cs @@ -19,6 +19,8 @@ using System.Collections.Generic; +#nullable enable + namespace OpenQA.Selenium.Internal { /// diff --git a/dotnet/src/webdriver/Internal/ResourceUtilities.cs b/dotnet/src/webdriver/Internal/ResourceUtilities.cs index 2f854a70214d4..0f22a33681c83 100644 --- a/dotnet/src/webdriver/Internal/ResourceUtilities.cs +++ b/dotnet/src/webdriver/Internal/ResourceUtilities.cs @@ -22,6 +22,8 @@ using System.Reflection; using System.Runtime.InteropServices; +#nullable enable + namespace OpenQA.Selenium.Internal { /// @@ -29,8 +31,8 @@ namespace OpenQA.Selenium.Internal /// internal static class ResourceUtilities { - private static string productVersion; - private static string platformFamily; + private static string? productVersion; + private static string? platformFamily; /// /// Gets a string representing the informational version of the Selenium product. @@ -60,18 +62,7 @@ public static string ProductVersion /// /// Gets a string representing the platform family on which the Selenium assembly is executing. /// - public static string PlatformFamily - { - get - { - if (string.IsNullOrEmpty(platformFamily)) - { - platformFamily = GetPlatformString(); - } - - return platformFamily; - } - } + public static string PlatformFamily => platformFamily ??= GetPlatformString(); /// /// Gets a that contains the resource to use. @@ -94,7 +85,7 @@ public static string PlatformFamily /// public static Stream GetResourceStream(string fileName, string resourceId) { - Stream resourceStream = null; + Stream? resourceStream; string resourceFilePath = Path.Combine(FileUtilities.GetCurrentDirectory(), Path.GetFileName(fileName)); if (File.Exists(resourceFilePath)) { @@ -125,20 +116,22 @@ public static Stream GetResourceStream(string fileName, string resourceId) private static string GetPlatformString() { - string platformName = "unknown"; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - platformName = "windows"; + return "windows"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - platformName = "linux"; + return "linux"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - platformName = "mac"; + return "mac"; + } + else + { + return "unknown"; } - return platformName; } } } diff --git a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs index 2732cebbd13a2..66b962b2878a8 100644 --- a/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs +++ b/dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs @@ -22,6 +22,8 @@ using System.Text.Json; using System.Text.Json.Serialization; +#nullable enable + namespace OpenQA.Selenium.Internal { /// @@ -29,7 +31,7 @@ namespace OpenQA.Selenium.Internal /// internal class ResponseValueJsonConverter : JsonConverter { - public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return ProcessReadToken(ref reader, options); } @@ -67,19 +69,19 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp } } - private static object ProcessReadToken(ref Utf8JsonReader reader, JsonSerializerOptions options) + private static object? ProcessReadToken(ref Utf8JsonReader reader, JsonSerializerOptions options) { // Recursively processes a token. This is required for elements that next other elements. - object processedObject; + object? processedObject; switch (reader.TokenType) { case JsonTokenType.StartObject: { - Dictionary dictionaryValue = []; + Dictionary dictionaryValue = []; while (reader.Read() && reader.TokenType != JsonTokenType.EndObject) { - string elementKey = reader.GetString(); + string elementKey = reader.GetString()!; reader.Read(); dictionaryValue.Add(elementKey, ProcessReadToken(ref reader, options)); } @@ -90,7 +92,7 @@ private static object ProcessReadToken(ref Utf8JsonReader reader, JsonSerializer case JsonTokenType.StartArray: { - List arrayValue = []; + List arrayValue = []; while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) { arrayValue.Add(ProcessReadToken(ref reader, options)); diff --git a/dotnet/src/webdriver/Internal/ReturnedCapabilities.cs b/dotnet/src/webdriver/Internal/ReturnedCapabilities.cs index 3bac423108923..2932b75273e84 100644 --- a/dotnet/src/webdriver/Internal/ReturnedCapabilities.cs +++ b/dotnet/src/webdriver/Internal/ReturnedCapabilities.cs @@ -22,13 +22,15 @@ using System.Collections.ObjectModel; using System.Globalization; +#nullable enable + namespace OpenQA.Selenium.Internal { /// /// Class to Create the capabilities of the browser you require for . /// If you wish to use default values use the static methods /// - internal class ReturnedCapabilities : ICapabilities, IHasCapabilitiesDictionary + internal sealed class ReturnedCapabilities : ICapabilities, IHasCapabilitiesDictionary { private readonly Dictionary capabilities = new Dictionary(); @@ -43,32 +45,26 @@ public ReturnedCapabilities() /// Initializes a new instance of the class /// /// Dictionary of items for the remote driver - public ReturnedCapabilities(Dictionary rawMap) + public ReturnedCapabilities(Dictionary? rawMap) { if (rawMap != null) { - foreach (string key in rawMap.Keys) + foreach (KeyValuePair rawItem in rawMap) { - this.capabilities[key] = rawMap[key]; + this.capabilities[rawItem.Key] = rawItem.Value; } } } /// - /// Gets the browser name + /// Gets the browser name, or if not specified. /// public string BrowserName { get { - string name = string.Empty; - object capabilityValue = this.GetCapability(CapabilityType.BrowserName); - if (capabilityValue != null) - { - name = capabilityValue.ToString(); - } - - return name; + object? capabilityValue = this.GetCapability(CapabilityType.BrowserName); + return capabilityValue?.ToString() ?? string.Empty; } } @@ -84,30 +80,24 @@ 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; } } /// /// Gets the underlying Dictionary for a given set of capabilities. /// - IDictionary IHasCapabilitiesDictionary.CapabilitiesDictionary - { - get { return this.CapabilitiesDictionary; } - } + IDictionary IHasCapabilitiesDictionary.CapabilitiesDictionary => this.CapabilitiesDictionary; /// /// Gets the internal capabilities dictionary. /// - internal IDictionary CapabilitiesDictionary - { - get { return new ReadOnlyDictionary(this.capabilities); } - } + internal IDictionary CapabilitiesDictionary => new ReadOnlyDictionary(this.capabilities); /// /// Gets a value indicating whether the browser has a given capability. @@ -125,15 +115,14 @@ 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]; + return capabilityValue; } - return capabilityValue; + return null; } ///