Skip to content

Commit ec6bea1

Browse files
RenderMichaelsandeepsuryaprasad
authored andcommitted
[dotnet] Annotate nullability on SafariOptions, error, and enums (SeleniumHQ#15219)
* [dotnet] Annotate nullability on `SafariOptions`, error, and enums * Convert `resultMap` initialization to `Add` style instead of indexer
1 parent 5d8b940 commit ec6bea1

File tree

7 files changed

+66
-60
lines changed

7 files changed

+66
-60
lines changed

Diff for: dotnet/src/webdriver/CapabilityType.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
// </copyright>
1919

2020
using System.Collections.Generic;
21+
using System.Diagnostics.CodeAnalysis;
22+
23+
#nullable enable
2124

2225
namespace OpenQA.Selenium
2326
{
@@ -167,7 +170,8 @@ public static class CapabilityType
167170
/// </summary>
168171
public static readonly string EnableDownloads = "se:downloadsEnabled";
169172

170-
private static readonly List<string> KnownSpecCompliantCapabilityNames = new List<string>() {
173+
private static readonly HashSet<string> KnownSpecCompliantCapabilityNames = new HashSet<string>()
174+
{
171175
BrowserName,
172176
BrowserVersion,
173177
PlatformName,
@@ -188,8 +192,13 @@ public static class CapabilityType
188192
/// <param name="capabilityName">The name of the capability to check for compliance.</param>
189193
/// <returns><see langword="true"/> if the capability name is valid according to the rules
190194
/// of the specification; otherwise, <see langword="false"/>.</returns>
191-
public static bool IsSpecCompliantCapabilityName(string capabilityName)
195+
public static bool IsSpecCompliantCapabilityName([NotNullWhen(true)] string? capabilityName)
192196
{
197+
if (capabilityName is null)
198+
{
199+
return false;
200+
}
201+
193202
if (KnownSpecCompliantCapabilityNames.Contains(capabilityName) || capabilityName.Contains(":"))
194203
{
195204
return true;

Diff for: dotnet/src/webdriver/IRotatable.cs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium
2123
{
2224
/// <summary>

Diff for: dotnet/src/webdriver/Safari/SafariOptions.cs

+9-22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium.Safari
2123
{
2224
/// <summary>
@@ -46,17 +48,13 @@ public class SafariOptions : DriverOptions
4648
private const string EnableAutomaticInspectionSafariOption = "safari:automaticInspection";
4749
private const string EnableAutomaticProfilingSafariOption = "safari:automaticProfiling";
4850

49-
private bool enableAutomaticInspection = false;
50-
private bool enableAutomaticProfiling = false;
51-
private bool technologyPreview = false;
52-
5351
/// <summary>
5452
/// Initializes a new instance of the <see cref="SafariOptions"/> class.
5553
/// </summary>
5654
public SafariOptions() : base()
5755
{
5856
this.BrowserName = BrowserNameValue;
59-
this.technologyPreview = false;
57+
this.TechnologyPreview = false;
6058
this.AddKnownCapabilityName(SafariOptions.EnableAutomaticInspectionSafariOption, "EnableAutomaticInspection property");
6159
this.AddKnownCapabilityName(SafariOptions.EnableAutomaticProfilingSafariOption, "EnableAutomaticProfiling property");
6260
}
@@ -66,38 +64,27 @@ public SafariOptions() : base()
6664
/// </summary>
6765
public void UseTechnologyPreview()
6866
{
69-
this.technologyPreview = true;
67+
this.TechnologyPreview = true;
7068
this.BrowserName = "Safari Technology Preview";
7169
}
7270

7371
/// <summary>
7472
/// Gets or sets a value indicating whether to have the driver preload the
7573
/// Web Inspector and JavaScript debugger in the background.
7674
/// </summary>
77-
public bool TechnologyPreview
78-
{
79-
get { return this.technologyPreview; }
80-
}
75+
public bool TechnologyPreview { get; private set; } = false;
8176

8277
/// <summary>
8378
/// Gets or sets a value indicating whether to have the driver preload the
8479
/// Web Inspector and JavaScript debugger in the background.
8580
/// </summary>
86-
public bool EnableAutomaticInspection
87-
{
88-
get { return this.enableAutomaticInspection; }
89-
set { this.enableAutomaticInspection = value; }
90-
}
81+
public bool EnableAutomaticInspection { get; set; } = false;
9182

9283
/// <summary>
9384
/// Gets or sets a value indicating whether to have the driver preload the
9485
/// Web Inspector and start a timeline recording in the background.
9586
/// </summary>
96-
public bool EnableAutomaticProfiling
97-
{
98-
get { return this.enableAutomaticProfiling; }
99-
set { this.enableAutomaticProfiling = value; }
100-
}
87+
public bool EnableAutomaticProfiling { get; set; } = false;
10188

10289
/// <summary>
10390
/// Returns ICapabilities for Safari with these options included as
@@ -108,12 +95,12 @@ public bool EnableAutomaticProfiling
10895
public override ICapabilities ToCapabilities()
10996
{
11097
IWritableCapabilities capabilities = this.GenerateDesiredCapabilities(true);
111-
if (this.enableAutomaticInspection)
98+
if (this.EnableAutomaticInspection)
11299
{
113100
capabilities.SetCapability(EnableAutomaticInspectionSafariOption, true);
114101
}
115102

116-
if (this.enableAutomaticProfiling)
103+
if (this.EnableAutomaticProfiling)
117104
{
118105
capabilities.SetCapability(EnableAutomaticProfilingSafariOption, true);
119106
}

Diff for: dotnet/src/webdriver/ScreenOrientation.cs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium
2123
{
2224
/// <summary>

Diff for: dotnet/src/webdriver/WebDriverError.cs

+37-36
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using System;
2021
using System.Collections.Generic;
2122

23+
#nullable enable
24+
2225
namespace OpenQA.Selenium
2326
{
2427
/// <summary>
@@ -167,54 +170,52 @@ internal static class WebDriverError
167170
/// </summary>
168171
public const string UnsupportedOperation = "unsupported operation";
169172

170-
private static readonly Dictionary<string, WebDriverResult> resultMap;
171-
172-
static WebDriverError()
173+
private static readonly Dictionary<string, WebDriverResult> resultMap = new Dictionary<string, WebDriverResult>
173174
{
174-
resultMap = new Dictionary<string, WebDriverResult>();
175-
resultMap[ElementClickIntercepted] = WebDriverResult.ElementClickIntercepted;
176-
resultMap[ElementNotInteractable] = WebDriverResult.ElementNotInteractable;
177-
resultMap[InsecureCertificate] = WebDriverResult.InsecureCertificate;
178-
resultMap[InvalidArgument] = WebDriverResult.InvalidArgument;
179-
resultMap[InvalidCookieDomain] = WebDriverResult.InvalidCookieDomain;
180-
resultMap[InvalidElementState] = WebDriverResult.InvalidElementState;
181-
resultMap[InvalidSelector] = WebDriverResult.InvalidSelector;
182-
resultMap[InvalidSessionId] = WebDriverResult.NoSuchDriver;
183-
resultMap[JavaScriptError] = WebDriverResult.UnexpectedJavaScriptError;
184-
resultMap[MoveTargetOutOfBounds] = WebDriverResult.MoveTargetOutOfBounds;
185-
resultMap[NoSuchAlert] = WebDriverResult.NoAlertPresent;
186-
resultMap[NoSuchCookie] = WebDriverResult.NoSuchCookie;
187-
resultMap[NoSuchElement] = WebDriverResult.NoSuchElement;
188-
resultMap[NoSuchFrame] = WebDriverResult.NoSuchFrame;
189-
resultMap[NoSuchWindow] = WebDriverResult.NoSuchWindow;
190-
resultMap[NoSuchShadowRoot] = WebDriverResult.NoSuchShadowRoot;
191-
resultMap[ScriptTimeout] = WebDriverResult.AsyncScriptTimeout;
192-
resultMap[SessionNotCreated] = WebDriverResult.SessionNotCreated;
193-
resultMap[StaleElementReference] = WebDriverResult.ObsoleteElement;
194-
resultMap[DetachedShadowRoot] = WebDriverResult.DetachedShadowRoot;
195-
resultMap[Timeout] = WebDriverResult.Timeout;
196-
resultMap[UnableToSetCookie] = WebDriverResult.UnableToSetCookie;
197-
resultMap[UnableToCaptureScreen] = WebDriverResult.UnableToCaptureScreen;
198-
resultMap[UnexpectedAlertOpen] = WebDriverResult.UnexpectedAlertOpen;
199-
resultMap[UnknownCommand] = WebDriverResult.UnknownCommand;
200-
resultMap[UnknownError] = WebDriverResult.UnknownError;
201-
resultMap[UnknownMethod] = WebDriverResult.UnknownMethod;
202-
resultMap[UnsupportedOperation] = WebDriverResult.UnsupportedOperation;
203-
}
175+
{ ElementClickIntercepted, WebDriverResult.ElementClickIntercepted },
176+
{ ElementNotInteractable, WebDriverResult.ElementNotInteractable },
177+
{ InsecureCertificate, WebDriverResult.InsecureCertificate },
178+
{ InvalidArgument, WebDriverResult.InvalidArgument },
179+
{ InvalidCookieDomain, WebDriverResult.InvalidCookieDomain },
180+
{ InvalidElementState, WebDriverResult.InvalidElementState },
181+
{ InvalidSelector, WebDriverResult.InvalidSelector },
182+
{ InvalidSessionId, WebDriverResult.NoSuchDriver },
183+
{ JavaScriptError, WebDriverResult.UnexpectedJavaScriptError },
184+
{ MoveTargetOutOfBounds, WebDriverResult.MoveTargetOutOfBounds },
185+
{ NoSuchAlert, WebDriverResult.NoAlertPresent },
186+
{ NoSuchCookie, WebDriverResult.NoSuchCookie },
187+
{ NoSuchElement, WebDriverResult.NoSuchElement },
188+
{ NoSuchFrame, WebDriverResult.NoSuchFrame },
189+
{ NoSuchWindow, WebDriverResult.NoSuchWindow },
190+
{ NoSuchShadowRoot, WebDriverResult.NoSuchShadowRoot },
191+
{ ScriptTimeout, WebDriverResult.AsyncScriptTimeout },
192+
{ SessionNotCreated, WebDriverResult.SessionNotCreated },
193+
{ StaleElementReference, WebDriverResult.ObsoleteElement },
194+
{ DetachedShadowRoot, WebDriverResult.DetachedShadowRoot },
195+
{ Timeout, WebDriverResult.Timeout },
196+
{ UnableToSetCookie, WebDriverResult.UnableToSetCookie },
197+
{ UnableToCaptureScreen, WebDriverResult.UnableToCaptureScreen },
198+
{ UnexpectedAlertOpen, WebDriverResult.UnexpectedAlertOpen },
199+
{ UnknownCommand, WebDriverResult.UnknownCommand },
200+
{ UnknownError, WebDriverResult.UnknownError },
201+
{ UnknownMethod, WebDriverResult.UnknownMethod },
202+
{ UnsupportedOperation, WebDriverResult.UnsupportedOperation }
203+
};
204204

205205
/// <summary>
206206
/// Converts a string error to a <see cref="WebDriverResult"/> value.
207207
/// </summary>
208208
/// <param name="error">The error string to convert.</param>
209209
/// <returns>The converted <see cref="WebDriverResult"/> value.</returns>
210+
/// <exception cref="ArgumentNullException">If <paramref name="error"/> is <see langword="null"/>.</exception>
210211
public static WebDriverResult ResultFromError(string error)
211212
{
212-
if (!resultMap.ContainsKey(error))
213+
if (!resultMap.TryGetValue(error, out WebDriverResult result))
213214
{
214-
error = UnsupportedOperation;
215+
return WebDriverResult.UnsupportedOperation;
215216
}
216217

217-
return resultMap[error];
218+
return result;
218219
}
219220
}
220221
}

Diff for: dotnet/src/webdriver/WebDriverResult.cs

+2
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>

Diff for: dotnet/src/webdriver/WindowType.cs

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
// under the License.
1818
// </copyright>
1919

20+
21+
#nullable enable
22+
2023
namespace OpenQA.Selenium
2124
{
2225
/// <summary>

0 commit comments

Comments
 (0)