Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/ISearchContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using System;
using System.Collections.ObjectModel;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/IWrapsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/Internal/IWebDriverObjectReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System.Collections.Generic;

#nullable enable

namespace OpenQA.Selenium.Internal
{
/// <summary>
Expand Down
57 changes: 31 additions & 26 deletions dotnet/src/webdriver/ShadowRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;

#nullable enable

namespace OpenQA.Selenium
{
Expand All @@ -34,68 +37,67 @@ public class ShadowRoot : ISearchContext, IWrapsDriver, IWebDriverObjectReferenc
/// </summary>
public const string ShadowRootReferencePropertyName = "shadow-6066-11e4-a52e-4f735466cecf";

private WebDriver driver;
private string shadowRootId;
private readonly WebDriver driver;
private readonly string shadowRootId;

/// <summary>
/// Initializes a new instance of the <see cref="ShadowRoot"/> class.
/// </summary>
/// <param name="parentDriver">The <see cref="WebDriver"/> instance that is driving this shadow root.</param>
/// <param name="id">The ID value provided to identify the shadow root.</param>
/// <exception cref="ArgumentNullException">If <paramref name="parentDriver"/> or <paramref name="id"/> are <see langword="null"/>.</exception>
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));
}

/// <summary>
/// Gets the <see cref="IWebDriver"/> driving this shadow root.
/// </summary>
public IWebDriver WrappedDriver
{
get { return this.driver; }
}
public IWebDriver WrappedDriver => this.driver;

/// <summary>
/// Gets the internal ID for this ShadowRoot.
/// </summary>
string IWebDriverObjectReference.ObjectReferenceId
{
get { return this.shadowRootId; }
}
string IWebDriverObjectReference.ObjectReferenceId => this.shadowRootId;

internal static bool ContainsShadowRootReference(Dictionary<string, object> shadowRootDictionary)
internal static bool TryCreate(WebDriver parentDriver, Dictionary<string, object?> 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<string, object> shadowRootDictionary)
{
return new ShadowRoot(driver, shadowRootDictionary[ShadowRoot.ShadowRootReferencePropertyName].ToString());
shadowRoot = null;
return false;
}

/// <summary>
/// Finds the first <see cref="IWebElement"/> using the given method.
/// </summary>
/// <param name="by">The locating mechanism to use.</param>
/// <returns>The first matching <see cref="IWebElement"/> on the current context.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="by"/> is <see langword="null"/>.</exception>
/// <exception cref="NoSuchElementException">If no element matches the criteria.</exception>
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<string, object> parameters = new Dictionary<string, object>();
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);
}
Expand All @@ -107,26 +109,29 @@ public IWebElement FindElement(By by)
/// <param name="by">The locating mechanism to use.</param>
/// <returns>A <see cref="ReadOnlyCollection{T}"/> of all <see cref="IWebElement">WebElements</see>
/// matching the current criteria, or an empty list if nothing matches.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="by"/> is <see langword="null"/>.</exception>
public ReadOnlyCollection<IWebElement> 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<string, object> parameters = new Dictionary<string, object>();
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<string, object> IWebDriverObjectReference.ToDictionary()
{
Dictionary<string, object> shadowRootDictionary = new Dictionary<string, object>();
shadowRootDictionary.Add(ShadowRootReferencePropertyName, this.shadowRootId);
return shadowRootDictionary;
return new Dictionary<string, object>
{
[ShadowRootReferencePropertyName] = this.shadowRootId
};
}
}
}
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Loading