diff --git a/dotnet/src/webdriver/ISearchContext.cs b/dotnet/src/webdriver/ISearchContext.cs index 53c5f33ae36da..a8e244ffdddee 100644 --- a/dotnet/src/webdriver/ISearchContext.cs +++ b/dotnet/src/webdriver/ISearchContext.cs @@ -20,6 +20,8 @@ using System; using System.Collections.ObjectModel; +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/IWrapsDriver.cs b/dotnet/src/webdriver/IWrapsDriver.cs index 27af4c7e56faa..15fe5afa2bcf3 100644 --- a/dotnet/src/webdriver/IWrapsDriver.cs +++ b/dotnet/src/webdriver/IWrapsDriver.cs @@ -17,6 +17,8 @@ // under the License. // +#nullable enable + namespace OpenQA.Selenium { /// diff --git a/dotnet/src/webdriver/Internal/IWebDriverObjectReference.cs b/dotnet/src/webdriver/Internal/IWebDriverObjectReference.cs index bc67fdd7c6a6b..a62286ca57676 100644 --- a/dotnet/src/webdriver/Internal/IWebDriverObjectReference.cs +++ b/dotnet/src/webdriver/Internal/IWebDriverObjectReference.cs @@ -19,6 +19,8 @@ using System.Collections.Generic; +#nullable enable + namespace OpenQA.Selenium.Internal { /// diff --git a/dotnet/src/webdriver/ShadowRoot.cs b/dotnet/src/webdriver/ShadowRoot.cs index 459674b27c1d5..a872d4fbc7d74 100644 --- a/dotnet/src/webdriver/ShadowRoot.cs +++ b/dotnet/src/webdriver/ShadowRoot.cs @@ -21,6 +21,9 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; + +#nullable enable namespace OpenQA.Selenium { @@ -34,49 +37,46 @@ public class ShadowRoot : ISearchContext, IWrapsDriver, IWebDriverObjectReferenc /// public const string ShadowRootReferencePropertyName = "shadow-6066-11e4-a52e-4f735466cecf"; - private WebDriver driver; - private string shadowRootId; + private readonly WebDriver driver; + private readonly string shadowRootId; /// /// Initializes a new instance of the class. /// /// The instance that is driving this shadow root. /// The ID value provided to identify the shadow root. + /// If or are . public ShadowRoot(WebDriver parentDriver, string id) { - this.driver = parentDriver; - this.shadowRootId = id; + this.driver = parentDriver ?? throw new ArgumentNullException(nameof(parentDriver)); + this.shadowRootId = id ?? throw new ArgumentNullException(nameof(id)); } /// /// Gets the driving this shadow root. /// - public IWebDriver WrappedDriver - { - get { return this.driver; } - } + public IWebDriver WrappedDriver => this.driver; /// /// Gets the internal ID for this ShadowRoot. /// - string IWebDriverObjectReference.ObjectReferenceId - { - get { return this.shadowRootId; } - } + string IWebDriverObjectReference.ObjectReferenceId => this.shadowRootId; - internal static bool ContainsShadowRootReference(Dictionary shadowRootDictionary) + internal static bool TryCreate(WebDriver parentDriver, Dictionary shadowRootDictionary, [NotNullWhen(true)] out ShadowRoot? shadowRoot) { - if (shadowRootDictionary == null) + if (shadowRootDictionary is null) { throw new ArgumentNullException(nameof(shadowRootDictionary), "The dictionary containing the shadow root reference cannot be null"); } - return shadowRootDictionary.ContainsKey(ShadowRootReferencePropertyName); - } + if (shadowRootDictionary.TryGetValue(ShadowRootReferencePropertyName, out object? shadowRootValue)) + { + shadowRoot = new ShadowRoot(parentDriver, shadowRootValue?.ToString()!); + return true; + } - internal static ShadowRoot FromDictionary(WebDriver driver, Dictionary shadowRootDictionary) - { - return new ShadowRoot(driver, shadowRootDictionary[ShadowRoot.ShadowRootReferencePropertyName].ToString()); + shadowRoot = null; + return false; } /// @@ -84,18 +84,20 @@ internal static ShadowRoot FromDictionary(WebDriver driver, Dictionary /// The locating mechanism to use. /// The first matching on the current context. + /// If is . /// If no element matches the criteria. public IWebElement FindElement(By by) { - if (by == null) + if (by is null) { - throw new ArgumentNullException(nameof(@by), "by cannot be null"); + throw new ArgumentNullException(nameof(by), "by cannot be null"); } Dictionary parameters = new Dictionary(); parameters.Add("id", this.shadowRootId); parameters.Add("using", by.Mechanism); parameters.Add("value", by.Criteria); + Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElement, parameters); return this.driver.GetElementFromResponse(commandResponse); } @@ -107,26 +109,29 @@ public IWebElement FindElement(By by) /// The locating mechanism to use. /// A of all WebElements /// matching the current criteria, or an empty list if nothing matches. + /// If is . public ReadOnlyCollection FindElements(By by) { - if (by == null) + if (by is null) { - throw new ArgumentNullException(nameof(@by), "by cannot be null"); + throw new ArgumentNullException(nameof(by), "by cannot be null"); } Dictionary parameters = new Dictionary(); parameters.Add("id", this.shadowRootId); parameters.Add("using", by.Mechanism); parameters.Add("value", by.Criteria); + Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElements, parameters); return this.driver.GetElementsFromResponse(commandResponse); } Dictionary IWebDriverObjectReference.ToDictionary() { - Dictionary shadowRootDictionary = new Dictionary(); - shadowRootDictionary.Add(ShadowRootReferencePropertyName, this.shadowRootId); - return shadowRootDictionary; + return new Dictionary + { + [ShadowRootReferencePropertyName] = this.shadowRootId + }; } } } diff --git a/dotnet/src/webdriver/WebDriver.cs b/dotnet/src/webdriver/WebDriver.cs index 235b2e648058e..0720811caba33 100644 --- a/dotnet/src/webdriver/WebDriver.cs +++ b/dotnet/src/webdriver/WebDriver.cs @@ -980,9 +980,9 @@ private object ParseJavaScriptReturnValue(object responseValue) { returnValue = this.elementFactory.CreateElement(resultAsDictionary); } - else if (ShadowRoot.ContainsShadowRootReference(resultAsDictionary)) + else if (ShadowRoot.TryCreate(this, resultAsDictionary, out ShadowRoot shadowRoot)) { - returnValue = ShadowRoot.FromDictionary(this, resultAsDictionary); + returnValue = shadowRoot; } else {