Skip to content

Commit 8009d93

Browse files
RenderMichaelsandeepsuryaprasad
authored andcommitted
[dotnet] Annotate Nullable Reference Types on OpenQA.Selenium.Internal (SeleniumHQ#14840)
1 parent 0eb734e commit 8009d93

File tree

8 files changed

+59
-79
lines changed

8 files changed

+59
-79
lines changed

dotnet/src/webdriver/ICapabilities.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium
2325
{
2426
/// <summary>
@@ -49,6 +51,6 @@ public interface ICapabilities
4951
/// <param name="capability">The capability to get.</param>
5052
/// <returns>An object associated with the capability, or <see langword="null"/>
5153
/// if the capability is not set on the browser.</returns>
52-
object GetCapability(string capability);
54+
object? GetCapability(string capability);
5355
}
5456
}

dotnet/src/webdriver/Internal/AndroidOptions.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,43 @@
1919

2020
using System;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.Internal
2325
{
2426
/// <summary>
2527
/// Provides a base class for options for browsers to be automated on Android.
2628
/// </summary>
2729
public class AndroidOptions
2830
{
29-
private string androidPackage;
30-
private string androidDeviceSerial;
31-
private string androidActivity;
32-
3331
/// <summary>
3432
/// Initializes a new instance of the <see cref="AndroidOptions"/> class.
3533
/// </summary>
3634
/// <param name="androidPackage"></param>
35+
/// <exception cref="ArgumentException">If <paramref name="androidPackage"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
3736
protected AndroidOptions(string androidPackage)
3837
{
3938
if (string.IsNullOrEmpty(androidPackage))
4039
{
4140
throw new ArgumentException("The Android package cannot be null or the empty string", nameof(androidPackage));
4241
}
4342

44-
this.androidPackage = androidPackage;
43+
this.AndroidPackage = androidPackage;
4544
}
4645

4746
/// <summary>
4847
/// The package name of the application to automate.
4948
/// </summary>
50-
public string AndroidPackage
51-
{
52-
get { return this.androidPackage; }
53-
}
49+
public string AndroidPackage { get; }
5450

5551
/// <summary>
5652
/// The serial number of the device on which to launch the application.
5753
/// </summary>
58-
public string AndroidDeviceSerial
59-
{
60-
get { return this.androidDeviceSerial; }
61-
set { this.androidDeviceSerial = value; }
62-
}
54+
public string? AndroidDeviceSerial { get; set; }
6355

6456
/// <summary>
6557
/// Gets or sets the name of the Activity hosting the app.
6658
/// </summary>
67-
public string AndroidActivity
68-
{
69-
get { return this.androidActivity; }
70-
set { this.androidActivity = value; }
71-
}
59+
public string? AndroidActivity { get; set; }
7260
}
7361
}

dotnet/src/webdriver/Internal/FileUtilities.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
using System.IO;
2424
using System.Reflection;
2525

26+
#nullable enable
27+
2628
namespace OpenQA.Selenium.Internal
2729
{
2830
/// <summary>
@@ -40,7 +42,7 @@ internal static class FileUtilities
4042
/// <returns><see langword="true"/> if the copy is completed; otherwise <see langword="false"/>.</returns>
4143
public static bool CopyDirectory(string sourceDirectory, string destinationDirectory)
4244
{
43-
bool copyComplete = false;
45+
bool copyComplete;
4446
DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(sourceDirectory);
4547
DirectoryInfo destinationDirectoryInfo = new DirectoryInfo(destinationDirectory);
4648

@@ -132,7 +134,7 @@ public static string FindFile(string fileName)
132134

133135
// If it's not in the same directory as the executing assembly,
134136
// try looking in the system path.
135-
string systemPath = Environment.GetEnvironmentVariable("PATH");
137+
string? systemPath = Environment.GetEnvironmentVariable("PATH");
136138
if (!string.IsNullOrEmpty(systemPath))
137139
{
138140
string expandedPath = Environment.ExpandEnvironmentVariables(systemPath);
@@ -165,7 +167,7 @@ public static string FindFile(string fileName)
165167
public static string GetCurrentDirectory()
166168
{
167169
Assembly executingAssembly = typeof(FileUtilities).Assembly;
168-
string location = null;
170+
string? location = null;
169171

170172
// Make sure not to call Path.GetDirectoryName if assembly location is null or empty
171173
if (!string.IsNullOrEmpty(executingAssembly.Location))
@@ -184,13 +186,13 @@ public static string GetCurrentDirectory()
184186
location = Directory.GetCurrentDirectory();
185187
}
186188

187-
string currentDirectory = location;
189+
string currentDirectory = location!;
188190

189191
// If we're shadow copying, get the directory from the codebase instead
190192
if (AppDomain.CurrentDomain.ShadowCopyFiles)
191193
{
192194
Uri uri = new Uri(executingAssembly.CodeBase);
193-
currentDirectory = Path.GetDirectoryName(uri.LocalPath);
195+
currentDirectory = Path.GetDirectoryName(uri.LocalPath)!;
194196
}
195197

196198
return currentDirectory;

dotnet/src/webdriver/Internal/IFindsElement.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System.Collections.ObjectModel;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.Internal
2325
{
2426
/// <summary>

dotnet/src/webdriver/Internal/IHasCapabilitiesDictionary.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
using System.Collections.Generic;
2121

22+
#nullable enable
23+
2224
namespace OpenQA.Selenium.Internal
2325
{
2426
/// <summary>

dotnet/src/webdriver/Internal/ResourceUtilities.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@
2222
using System.Reflection;
2323
using System.Runtime.InteropServices;
2424

25+
#nullable enable
26+
2527
namespace OpenQA.Selenium.Internal
2628
{
2729
/// <summary>
2830
/// Encapsulates methods for finding and extracting WebDriver resources.
2931
/// </summary>
3032
internal static class ResourceUtilities
3133
{
32-
private static string productVersion;
33-
private static string platformFamily;
34+
private static string? productVersion;
35+
private static string? platformFamily;
3436

3537
/// <summary>
3638
/// Gets a string representing the informational version of the Selenium product.
@@ -60,18 +62,7 @@ public static string ProductVersion
6062
/// <summary>
6163
/// Gets a string representing the platform family on which the Selenium assembly is executing.
6264
/// </summary>
63-
public static string PlatformFamily
64-
{
65-
get
66-
{
67-
if (string.IsNullOrEmpty(platformFamily))
68-
{
69-
platformFamily = GetPlatformString();
70-
}
71-
72-
return platformFamily;
73-
}
74-
}
65+
public static string PlatformFamily => platformFamily ??= GetPlatformString();
7566

7667
/// <summary>
7768
/// Gets a <see cref="Stream"/> that contains the resource to use.
@@ -94,7 +85,7 @@ public static string PlatformFamily
9485
/// </remarks>
9586
public static Stream GetResourceStream(string fileName, string resourceId)
9687
{
97-
Stream resourceStream = null;
88+
Stream? resourceStream;
9889
string resourceFilePath = Path.Combine(FileUtilities.GetCurrentDirectory(), Path.GetFileName(fileName));
9990
if (File.Exists(resourceFilePath))
10091
{
@@ -125,20 +116,22 @@ public static Stream GetResourceStream(string fileName, string resourceId)
125116

126117
private static string GetPlatformString()
127118
{
128-
string platformName = "unknown";
129119
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
130120
{
131-
platformName = "windows";
121+
return "windows";
132122
}
133123
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
134124
{
135-
platformName = "linux";
125+
return "linux";
136126
}
137127
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
138128
{
139-
platformName = "mac";
129+
return "mac";
130+
}
131+
else
132+
{
133+
return "unknown";
140134
}
141-
return platformName;
142135
}
143136
}
144137
}

dotnet/src/webdriver/Internal/ResponseValueJsonConverter.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
using System.Text.Json;
2323
using System.Text.Json.Serialization;
2424

25+
#nullable enable
26+
2527
namespace OpenQA.Selenium.Internal
2628
{
2729
/// <summary>
2830
/// Converts the response to JSON
2931
/// </summary>
3032
internal class ResponseValueJsonConverter : JsonConverter<object>
3133
{
32-
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
34+
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
3335
{
3436
return ProcessReadToken(ref reader, options);
3537
}
@@ -67,19 +69,19 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp
6769
}
6870
}
6971

70-
private static object ProcessReadToken(ref Utf8JsonReader reader, JsonSerializerOptions options)
72+
private static object? ProcessReadToken(ref Utf8JsonReader reader, JsonSerializerOptions options)
7173
{
7274
// Recursively processes a token. This is required for elements that next other elements.
73-
object processedObject;
75+
object? processedObject;
7476

7577
switch (reader.TokenType)
7678
{
7779
case JsonTokenType.StartObject:
7880
{
79-
Dictionary<string, object> dictionaryValue = [];
81+
Dictionary<string, object?> dictionaryValue = [];
8082
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
8183
{
82-
string elementKey = reader.GetString();
84+
string elementKey = reader.GetString()!;
8385
reader.Read();
8486
dictionaryValue.Add(elementKey, ProcessReadToken(ref reader, options));
8587
}
@@ -90,7 +92,7 @@ private static object ProcessReadToken(ref Utf8JsonReader reader, JsonSerializer
9092

9193
case JsonTokenType.StartArray:
9294
{
93-
List<object> arrayValue = [];
95+
List<object?> arrayValue = [];
9496
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
9597
{
9698
arrayValue.Add(ProcessReadToken(ref reader, options));

dotnet/src/webdriver/Internal/ReturnedCapabilities.cs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
using System.Collections.ObjectModel;
2323
using System.Globalization;
2424

25+
#nullable enable
26+
2527
namespace OpenQA.Selenium.Internal
2628
{
2729
/// <summary>
2830
/// Class to Create the capabilities of the browser you require for <see cref="IWebDriver"/>.
2931
/// If you wish to use default values use the static methods
3032
/// </summary>
31-
internal class ReturnedCapabilities : ICapabilities, IHasCapabilitiesDictionary
33+
internal sealed class ReturnedCapabilities : ICapabilities, IHasCapabilitiesDictionary
3234
{
3335
private readonly Dictionary<string, object> capabilities = new Dictionary<string, object>();
3436

@@ -43,32 +45,26 @@ public ReturnedCapabilities()
4345
/// Initializes a new instance of the <see cref="ReturnedCapabilities"/> class
4446
/// </summary>
4547
/// <param name="rawMap">Dictionary of items for the remote driver</param>
46-
public ReturnedCapabilities(Dictionary<string, object> rawMap)
48+
public ReturnedCapabilities(Dictionary<string, object>? rawMap)
4749
{
4850
if (rawMap != null)
4951
{
50-
foreach (string key in rawMap.Keys)
52+
foreach (KeyValuePair<string, object> rawItem in rawMap)
5153
{
52-
this.capabilities[key] = rawMap[key];
54+
this.capabilities[rawItem.Key] = rawItem.Value;
5355
}
5456
}
5557
}
5658

5759
/// <summary>
58-
/// Gets the browser name
60+
/// Gets the browser name, or <see cref="string.Empty"/> if not specified.
5961
/// </summary>
6062
public string BrowserName
6163
{
6264
get
6365
{
64-
string name = string.Empty;
65-
object capabilityValue = this.GetCapability(CapabilityType.BrowserName);
66-
if (capabilityValue != null)
67-
{
68-
name = capabilityValue.ToString();
69-
}
70-
71-
return name;
66+
object? capabilityValue = this.GetCapability(CapabilityType.BrowserName);
67+
return capabilityValue?.ToString() ?? string.Empty;
7268
}
7369
}
7470

@@ -84,30 +80,24 @@ public object this[string capabilityName]
8480
{
8581
get
8682
{
87-
if (!this.capabilities.ContainsKey(capabilityName))
83+
if (!this.capabilities.TryGetValue(capabilityName, out object? capabilityValue))
8884
{
8985
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The capability {0} is not present in this set of capabilities", capabilityName));
9086
}
9187

92-
return this.capabilities[capabilityName];
88+
return capabilityValue;
9389
}
9490
}
9591

9692
/// <summary>
9793
/// Gets the underlying Dictionary for a given set of capabilities.
9894
/// </summary>
99-
IDictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary
100-
{
101-
get { return this.CapabilitiesDictionary; }
102-
}
95+
IDictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary => this.CapabilitiesDictionary;
10396

10497
/// <summary>
10598
/// Gets the internal capabilities dictionary.
10699
/// </summary>
107-
internal IDictionary<string, object> CapabilitiesDictionary
108-
{
109-
get { return new ReadOnlyDictionary<string, object>(this.capabilities); }
110-
}
100+
internal IDictionary<string, object> CapabilitiesDictionary => new ReadOnlyDictionary<string, object>(this.capabilities);
111101

112102
/// <summary>
113103
/// Gets a value indicating whether the browser has a given capability.
@@ -125,15 +115,14 @@ public bool HasCapability(string capability)
125115
/// <param name="capability">The capability to get.</param>
126116
/// <returns>An object associated with the capability, or <see langword="null"/>
127117
/// if the capability is not set on the browser.</returns>
128-
public object GetCapability(string capability)
118+
public object? GetCapability(string capability)
129119
{
130-
object capabilityValue = null;
131-
if (this.capabilities.ContainsKey(capability))
120+
if (this.capabilities.TryGetValue(capability, out object? capabilityValue))
132121
{
133-
capabilityValue = this.capabilities[capability];
122+
return capabilityValue;
134123
}
135124

136-
return capabilityValue;
125+
return null;
137126
}
138127

139128
/// <summary>

0 commit comments

Comments
 (0)