From 0e0e092efa8224ba93051dfa8d01f9747b547b22 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Wed, 5 Mar 2025 12:58:08 -0500 Subject: [PATCH 1/4] [dotnet] Fix null warnings in `RelativeBy` by sealing the type --- dotnet/src/webdriver/RelativeBy.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/dotnet/src/webdriver/RelativeBy.cs b/dotnet/src/webdriver/RelativeBy.cs index 553ae402a780b..d128a914444f7 100644 --- a/dotnet/src/webdriver/RelativeBy.cs +++ b/dotnet/src/webdriver/RelativeBy.cs @@ -33,12 +33,9 @@ public class RelativeBy : By { private readonly string wrappedAtom; private readonly object root; - private readonly List filters = new List(); + private readonly List filters; - /// - /// Prevents a default instance of the class. - /// - protected RelativeBy() : base() + private static string GetWrappedAtom() { string atom; using (Stream atomStream = ResourceUtilities.GetResourceStream("find-elements.js", "find-elements.js")) @@ -49,17 +46,14 @@ protected RelativeBy() : base() } } - wrappedAtom = string.Format(CultureInfo.InvariantCulture, "/* findElements */return ({0}).apply(null, arguments);", atom); + return string.Format(CultureInfo.InvariantCulture, "/* findElements */return ({0}).apply(null, arguments);", atom); } - private RelativeBy(object root, List? filters = null) : this() + private RelativeBy(object root, List? filters = null) { + this.wrappedAtom = GetWrappedAtom(); this.root = GetSerializableRoot(root); - - if (filters != null) - { - this.filters.AddRange(filters); - } + this.filters = filters is null ? [] : [.. filters]; // Clone filters } /// From 1d9c1b140a55962308be7d550a4278b0575db92a Mon Sep 17 00:00:00 2001 From: Michael Render Date: Wed, 5 Mar 2025 13:11:57 -0500 Subject: [PATCH 2/4] minimize changes --- dotnet/src/webdriver/RelativeBy.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/RelativeBy.cs b/dotnet/src/webdriver/RelativeBy.cs index d128a914444f7..bf66479d7f5a6 100644 --- a/dotnet/src/webdriver/RelativeBy.cs +++ b/dotnet/src/webdriver/RelativeBy.cs @@ -33,7 +33,7 @@ public class RelativeBy : By { private readonly string wrappedAtom; private readonly object root; - private readonly List filters; + private readonly List filters = new List(); private static string GetWrappedAtom() { @@ -53,7 +53,10 @@ private RelativeBy(object root, List? filters = null) { this.wrappedAtom = GetWrappedAtom(); this.root = GetSerializableRoot(root); - this.filters = filters is null ? [] : [.. filters]; // Clone filters + if (filters != null) + { + this.filters.AddRange(filters); + } } /// From 31f4ee949dfae55e15535865be0b47b93c7c5bfa Mon Sep 17 00:00:00 2001 From: Michael Render Date: Tue, 25 Mar 2025 01:48:12 -0400 Subject: [PATCH 3/4] Address other low-hanging nullability warnings --- dotnet/src/support/Extensions/WebDriverExtensions.cs | 2 +- dotnet/src/support/UI/SelectElement.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/support/Extensions/WebDriverExtensions.cs b/dotnet/src/support/Extensions/WebDriverExtensions.cs index 209a08f433756..9b0274b6afded 100644 --- a/dotnet/src/support/Extensions/WebDriverExtensions.cs +++ b/dotnet/src/support/Extensions/WebDriverExtensions.cs @@ -115,7 +115,7 @@ public static void ExecuteJavaScript(this IWebDriver driver, string script, para } } - private static object ExecuteJavaScriptInternal(IWebDriver driver, string script, object?[] args) + private static object? ExecuteJavaScriptInternal(IWebDriver driver, string script, object?[] args) { IJavaScriptExecutor? executor = GetDriverAs(driver) ?? throw new WebDriverException("Driver does not implement IJavaScriptExecutor"); diff --git a/dotnet/src/support/UI/SelectElement.cs b/dotnet/src/support/UI/SelectElement.cs index 3eec3812560a0..070554d54192c 100644 --- a/dotnet/src/support/UI/SelectElement.cs +++ b/dotnet/src/support/UI/SelectElement.cs @@ -53,7 +53,7 @@ public SelectElement(IWebElement element) this.WrappedElement = element; // let check if it's a multiple - string attribute = element.GetAttribute("multiple"); + string? attribute = element.GetAttribute("multiple"); this.IsMultiple = attribute != null && !attribute.Equals("false", StringComparison.OrdinalIgnoreCase); } From 88f4a8530931c7df3e5cd55898cba4993fe3d233 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Tue, 25 Mar 2025 01:49:08 -0400 Subject: [PATCH 4/4] Seal the type explicitly --- dotnet/src/webdriver/RelativeBy.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/RelativeBy.cs b/dotnet/src/webdriver/RelativeBy.cs index bf66479d7f5a6..57d9b7a3f844c 100644 --- a/dotnet/src/webdriver/RelativeBy.cs +++ b/dotnet/src/webdriver/RelativeBy.cs @@ -29,7 +29,7 @@ namespace OpenQA.Selenium /// /// Provides a mechanism for finding elements spatially relative to other elements. /// - public class RelativeBy : By + public sealed class RelativeBy : By { private readonly string wrappedAtom; private readonly object root; @@ -70,7 +70,6 @@ public static RelativeBy WithLocator(By by) return new RelativeBy(by); } - /// /// Finds the first element matching the criteria. ///