From ccff1c270f36b27f2eaa69e45571fe207f855012 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 3 Feb 2025 02:32:59 -0500 Subject: [PATCH 1/2] [dotnet] Annotate nullability on `SafariOptions`, error, and enums --- dotnet/src/webdriver/CapabilityType.cs | 13 +++- dotnet/src/webdriver/IRotatable.cs | 2 + dotnet/src/webdriver/Safari/SafariOptions.cs | 31 +++------ dotnet/src/webdriver/ScreenOrientation.cs | 2 + dotnet/src/webdriver/WebDriverError.cs | 73 ++++++++++---------- dotnet/src/webdriver/WebDriverResult.cs | 2 + dotnet/src/webdriver/WindowType.cs | 3 + 7 files changed, 66 insertions(+), 60 deletions(-) diff --git a/dotnet/src/webdriver/CapabilityType.cs b/dotnet/src/webdriver/CapabilityType.cs index bb4bf8519e4ea..c04d54ff81957 100644 --- a/dotnet/src/webdriver/CapabilityType.cs +++ b/dotnet/src/webdriver/CapabilityType.cs @@ -18,6 +18,9 @@ // using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; + +#nullable enable namespace OpenQA.Selenium { @@ -167,7 +170,8 @@ public static class CapabilityType /// public static readonly string EnableDownloads = "se:downloadsEnabled"; - private static readonly List KnownSpecCompliantCapabilityNames = new List() { + private static readonly HashSet KnownSpecCompliantCapabilityNames = new HashSet() + { BrowserName, BrowserVersion, PlatformName, @@ -188,8 +192,13 @@ public static class CapabilityType /// The name of the capability to check for compliance. /// if the capability name is valid according to the rules /// of the specification; otherwise, . - public static bool IsSpecCompliantCapabilityName(string capabilityName) + public static bool IsSpecCompliantCapabilityName([NotNullWhen(true)] string? capabilityName) { + if (capabilityName is null) + { + return false; + } + if (KnownSpecCompliantCapabilityNames.Contains(capabilityName) || capabilityName.Contains(":")) { return true; diff --git a/dotnet/src/webdriver/IRotatable.cs b/dotnet/src/webdriver/IRotatable.cs index e67b0d401a9dc..02bb76c795210 100644 --- a/dotnet/src/webdriver/IRotatable.cs +++ b/dotnet/src/webdriver/IRotatable.cs @@ -17,6 +17,8 @@ // under the License. // +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/Safari/SafariOptions.cs b/dotnet/src/webdriver/Safari/SafariOptions.cs index 25f4753ff81ad..afc38a7849efc 100644 --- a/dotnet/src/webdriver/Safari/SafariOptions.cs +++ b/dotnet/src/webdriver/Safari/SafariOptions.cs @@ -17,6 +17,8 @@ // under the License. // +#nullable enable + namespace OpenQA.Selenium.Safari { /// @@ -46,17 +48,13 @@ public class SafariOptions : DriverOptions private const string EnableAutomaticInspectionSafariOption = "safari:automaticInspection"; private const string EnableAutomaticProfilingSafariOption = "safari:automaticProfiling"; - private bool enableAutomaticInspection = false; - private bool enableAutomaticProfiling = false; - private bool technologyPreview = false; - /// /// Initializes a new instance of the class. /// public SafariOptions() : base() { this.BrowserName = BrowserNameValue; - this.technologyPreview = false; + this.TechnologyPreview = false; this.AddKnownCapabilityName(SafariOptions.EnableAutomaticInspectionSafariOption, "EnableAutomaticInspection property"); this.AddKnownCapabilityName(SafariOptions.EnableAutomaticProfilingSafariOption, "EnableAutomaticProfiling property"); } @@ -66,7 +64,7 @@ public SafariOptions() : base() /// public void UseTechnologyPreview() { - this.technologyPreview = true; + this.TechnologyPreview = true; this.BrowserName = "Safari Technology Preview"; } @@ -74,30 +72,19 @@ public void UseTechnologyPreview() /// Gets or sets a value indicating whether to have the driver preload the /// Web Inspector and JavaScript debugger in the background. /// - public bool TechnologyPreview - { - get { return this.technologyPreview; } - } + public bool TechnologyPreview { get; private set; } = false; /// /// Gets or sets a value indicating whether to have the driver preload the /// Web Inspector and JavaScript debugger in the background. /// - public bool EnableAutomaticInspection - { - get { return this.enableAutomaticInspection; } - set { this.enableAutomaticInspection = value; } - } + public bool EnableAutomaticInspection { get; set; } = false; /// /// Gets or sets a value indicating whether to have the driver preload the /// Web Inspector and start a timeline recording in the background. /// - public bool EnableAutomaticProfiling - { - get { return this.enableAutomaticProfiling; } - set { this.enableAutomaticProfiling = value; } - } + public bool EnableAutomaticProfiling { get; set; } = false; /// /// Returns ICapabilities for Safari with these options included as @@ -108,12 +95,12 @@ public bool EnableAutomaticProfiling public override ICapabilities ToCapabilities() { IWritableCapabilities capabilities = this.GenerateDesiredCapabilities(true); - if (this.enableAutomaticInspection) + if (this.EnableAutomaticInspection) { capabilities.SetCapability(EnableAutomaticInspectionSafariOption, true); } - if (this.enableAutomaticProfiling) + if (this.EnableAutomaticProfiling) { capabilities.SetCapability(EnableAutomaticProfilingSafariOption, true); } diff --git a/dotnet/src/webdriver/ScreenOrientation.cs b/dotnet/src/webdriver/ScreenOrientation.cs index fb496b4703c9b..1d23b2e0bc9f3 100644 --- a/dotnet/src/webdriver/ScreenOrientation.cs +++ b/dotnet/src/webdriver/ScreenOrientation.cs @@ -17,6 +17,8 @@ // under the License. // +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/WebDriverError.cs b/dotnet/src/webdriver/WebDriverError.cs index 5811d924f53cb..a090c2ad6e730 100644 --- a/dotnet/src/webdriver/WebDriverError.cs +++ b/dotnet/src/webdriver/WebDriverError.cs @@ -17,8 +17,11 @@ // under the License. // +using System; using System.Collections.Generic; +#nullable enable + namespace OpenQA.Selenium { /// @@ -167,54 +170,52 @@ internal static class WebDriverError /// public const string UnsupportedOperation = "unsupported operation"; - private static readonly Dictionary resultMap; - - static WebDriverError() + private static readonly Dictionary resultMap = new Dictionary { - resultMap = new Dictionary(); - resultMap[ElementClickIntercepted] = WebDriverResult.ElementClickIntercepted; - resultMap[ElementNotInteractable] = WebDriverResult.ElementNotInteractable; - resultMap[InsecureCertificate] = WebDriverResult.InsecureCertificate; - resultMap[InvalidArgument] = WebDriverResult.InvalidArgument; - resultMap[InvalidCookieDomain] = WebDriverResult.InvalidCookieDomain; - resultMap[InvalidElementState] = WebDriverResult.InvalidElementState; - resultMap[InvalidSelector] = WebDriverResult.InvalidSelector; - resultMap[InvalidSessionId] = WebDriverResult.NoSuchDriver; - resultMap[JavaScriptError] = WebDriverResult.UnexpectedJavaScriptError; - resultMap[MoveTargetOutOfBounds] = WebDriverResult.MoveTargetOutOfBounds; - resultMap[NoSuchAlert] = WebDriverResult.NoAlertPresent; - resultMap[NoSuchCookie] = WebDriverResult.NoSuchCookie; - resultMap[NoSuchElement] = WebDriverResult.NoSuchElement; - resultMap[NoSuchFrame] = WebDriverResult.NoSuchFrame; - resultMap[NoSuchWindow] = WebDriverResult.NoSuchWindow; - resultMap[NoSuchShadowRoot] = WebDriverResult.NoSuchShadowRoot; - resultMap[ScriptTimeout] = WebDriverResult.AsyncScriptTimeout; - resultMap[SessionNotCreated] = WebDriverResult.SessionNotCreated; - resultMap[StaleElementReference] = WebDriverResult.ObsoleteElement; - resultMap[DetachedShadowRoot] = WebDriverResult.DetachedShadowRoot; - resultMap[Timeout] = WebDriverResult.Timeout; - resultMap[UnableToSetCookie] = WebDriverResult.UnableToSetCookie; - resultMap[UnableToCaptureScreen] = WebDriverResult.UnableToCaptureScreen; - resultMap[UnexpectedAlertOpen] = WebDriverResult.UnexpectedAlertOpen; - resultMap[UnknownCommand] = WebDriverResult.UnknownCommand; - resultMap[UnknownError] = WebDriverResult.UnknownError; - resultMap[UnknownMethod] = WebDriverResult.UnknownMethod; - resultMap[UnsupportedOperation] = WebDriverResult.UnsupportedOperation; - } + [ElementClickIntercepted] = WebDriverResult.ElementClickIntercepted, + [ElementNotInteractable] = WebDriverResult.ElementNotInteractable, + [InsecureCertificate] = WebDriverResult.InsecureCertificate, + [InvalidArgument] = WebDriverResult.InvalidArgument, + [InvalidCookieDomain] = WebDriverResult.InvalidCookieDomain, + [InvalidElementState] = WebDriverResult.InvalidElementState, + [InvalidSelector] = WebDriverResult.InvalidSelector, + [InvalidSessionId] = WebDriverResult.NoSuchDriver, + [JavaScriptError] = WebDriverResult.UnexpectedJavaScriptError, + [MoveTargetOutOfBounds] = WebDriverResult.MoveTargetOutOfBounds, + [NoSuchAlert] = WebDriverResult.NoAlertPresent, + [NoSuchCookie] = WebDriverResult.NoSuchCookie, + [NoSuchElement] = WebDriverResult.NoSuchElement, + [NoSuchFrame] = WebDriverResult.NoSuchFrame, + [NoSuchWindow] = WebDriverResult.NoSuchWindow, + [NoSuchShadowRoot] = WebDriverResult.NoSuchShadowRoot, + [ScriptTimeout] = WebDriverResult.AsyncScriptTimeout, + [SessionNotCreated] = WebDriverResult.SessionNotCreated, + [StaleElementReference] = WebDriverResult.ObsoleteElement, + [DetachedShadowRoot] = WebDriverResult.DetachedShadowRoot, + [Timeout] = WebDriverResult.Timeout, + [UnableToSetCookie] = WebDriverResult.UnableToSetCookie, + [UnableToCaptureScreen] = WebDriverResult.UnableToCaptureScreen, + [UnexpectedAlertOpen] = WebDriverResult.UnexpectedAlertOpen, + [UnknownCommand] = WebDriverResult.UnknownCommand, + [UnknownError] = WebDriverResult.UnknownError, + [UnknownMethod] = WebDriverResult.UnknownMethod, + [UnsupportedOperation] = WebDriverResult.UnsupportedOperation + }; /// /// Converts a string error to a value. /// /// The error string to convert. /// The converted value. + /// If is . public static WebDriverResult ResultFromError(string error) { - if (!resultMap.ContainsKey(error)) + if (!resultMap.TryGetValue(error, out WebDriverResult result)) { - error = UnsupportedOperation; + return WebDriverResult.UnsupportedOperation; } - return resultMap[error]; + return result; } } } diff --git a/dotnet/src/webdriver/WebDriverResult.cs b/dotnet/src/webdriver/WebDriverResult.cs index a18dcce7c6a42..03b9b20a83f3e 100644 --- a/dotnet/src/webdriver/WebDriverResult.cs +++ b/dotnet/src/webdriver/WebDriverResult.cs @@ -19,6 +19,8 @@ using System; +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/WindowType.cs b/dotnet/src/webdriver/WindowType.cs index 2b4f3b8e2bc9f..7e1200d7b9f8e 100644 --- a/dotnet/src/webdriver/WindowType.cs +++ b/dotnet/src/webdriver/WindowType.cs @@ -17,6 +17,9 @@ // under the License. // + +#nullable enable + namespace OpenQA.Selenium { /// From a492392272bb24502a5fefad14302a495d7fa7b2 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Mon, 3 Feb 2025 12:53:49 -0500 Subject: [PATCH 2/2] Convert `resultMap` initialization to `Add` style instead of indexer --- dotnet/src/webdriver/WebDriverError.cs | 56 +++++++++++++------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/dotnet/src/webdriver/WebDriverError.cs b/dotnet/src/webdriver/WebDriverError.cs index a090c2ad6e730..f4a139cac505b 100644 --- a/dotnet/src/webdriver/WebDriverError.cs +++ b/dotnet/src/webdriver/WebDriverError.cs @@ -172,34 +172,34 @@ internal static class WebDriverError private static readonly Dictionary resultMap = new Dictionary { - [ElementClickIntercepted] = WebDriverResult.ElementClickIntercepted, - [ElementNotInteractable] = WebDriverResult.ElementNotInteractable, - [InsecureCertificate] = WebDriverResult.InsecureCertificate, - [InvalidArgument] = WebDriverResult.InvalidArgument, - [InvalidCookieDomain] = WebDriverResult.InvalidCookieDomain, - [InvalidElementState] = WebDriverResult.InvalidElementState, - [InvalidSelector] = WebDriverResult.InvalidSelector, - [InvalidSessionId] = WebDriverResult.NoSuchDriver, - [JavaScriptError] = WebDriverResult.UnexpectedJavaScriptError, - [MoveTargetOutOfBounds] = WebDriverResult.MoveTargetOutOfBounds, - [NoSuchAlert] = WebDriverResult.NoAlertPresent, - [NoSuchCookie] = WebDriverResult.NoSuchCookie, - [NoSuchElement] = WebDriverResult.NoSuchElement, - [NoSuchFrame] = WebDriverResult.NoSuchFrame, - [NoSuchWindow] = WebDriverResult.NoSuchWindow, - [NoSuchShadowRoot] = WebDriverResult.NoSuchShadowRoot, - [ScriptTimeout] = WebDriverResult.AsyncScriptTimeout, - [SessionNotCreated] = WebDriverResult.SessionNotCreated, - [StaleElementReference] = WebDriverResult.ObsoleteElement, - [DetachedShadowRoot] = WebDriverResult.DetachedShadowRoot, - [Timeout] = WebDriverResult.Timeout, - [UnableToSetCookie] = WebDriverResult.UnableToSetCookie, - [UnableToCaptureScreen] = WebDriverResult.UnableToCaptureScreen, - [UnexpectedAlertOpen] = WebDriverResult.UnexpectedAlertOpen, - [UnknownCommand] = WebDriverResult.UnknownCommand, - [UnknownError] = WebDriverResult.UnknownError, - [UnknownMethod] = WebDriverResult.UnknownMethod, - [UnsupportedOperation] = WebDriverResult.UnsupportedOperation + { ElementClickIntercepted, WebDriverResult.ElementClickIntercepted }, + { ElementNotInteractable, WebDriverResult.ElementNotInteractable }, + { InsecureCertificate, WebDriverResult.InsecureCertificate }, + { InvalidArgument, WebDriverResult.InvalidArgument }, + { InvalidCookieDomain, WebDriverResult.InvalidCookieDomain }, + { InvalidElementState, WebDriverResult.InvalidElementState }, + { InvalidSelector, WebDriverResult.InvalidSelector }, + { InvalidSessionId, WebDriverResult.NoSuchDriver }, + { JavaScriptError, WebDriverResult.UnexpectedJavaScriptError }, + { MoveTargetOutOfBounds, WebDriverResult.MoveTargetOutOfBounds }, + { NoSuchAlert, WebDriverResult.NoAlertPresent }, + { NoSuchCookie, WebDriverResult.NoSuchCookie }, + { NoSuchElement, WebDriverResult.NoSuchElement }, + { NoSuchFrame, WebDriverResult.NoSuchFrame }, + { NoSuchWindow, WebDriverResult.NoSuchWindow }, + { NoSuchShadowRoot, WebDriverResult.NoSuchShadowRoot }, + { ScriptTimeout, WebDriverResult.AsyncScriptTimeout }, + { SessionNotCreated, WebDriverResult.SessionNotCreated }, + { StaleElementReference, WebDriverResult.ObsoleteElement }, + { DetachedShadowRoot, WebDriverResult.DetachedShadowRoot }, + { Timeout, WebDriverResult.Timeout }, + { UnableToSetCookie, WebDriverResult.UnableToSetCookie }, + { UnableToCaptureScreen, WebDriverResult.UnableToCaptureScreen }, + { UnexpectedAlertOpen, WebDriverResult.UnexpectedAlertOpen }, + { UnknownCommand, WebDriverResult.UnknownCommand }, + { UnknownError, WebDriverResult.UnknownError }, + { UnknownMethod, WebDriverResult.UnknownMethod }, + { UnsupportedOperation, WebDriverResult.UnsupportedOperation } }; ///