Skip to content

[dotnet] Enable ShadowRoot to use By query mechanism #15336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
32 changes: 27 additions & 5 deletions dotnet/src/webdriver/ShadowRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace OpenQA.Selenium
/// <summary>
/// Provides a representation of an element's shadow root.
/// </summary>
public class ShadowRoot : ISearchContext, IWrapsDriver, IWebDriverObjectReference
public class ShadowRoot : ISearchContext, IWrapsDriver, IWebDriverObjectReference, IFindsElement
{
/// <summary>
/// The property name that represents an element shadow root in the wire protocol.
Expand Down Expand Up @@ -93,10 +93,21 @@ public IWebElement FindElement(By by)
throw new ArgumentNullException(nameof(by), "by cannot be null");
}

return by.FindElement(this);
}

/// <summary>
/// Finds a child element matching the given mechanism and value.
/// </summary>
/// <param name="mechanism">The mechanism by which to find the element.</param>
/// <param name="value">The value to use to search for the element.</param>
/// <returns>The first <see cref="IWebElement"/> matching the given criteria.</returns>
public virtual IWebElement FindElement(string mechanism, string value)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("id", this.shadowRootId);
parameters.Add("using", by.Mechanism);
parameters.Add("value", by.Criteria);
parameters.Add("using", mechanism);
parameters.Add("value", value);

Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElement, parameters);
return this.driver.GetElementFromResponse(commandResponse)!;
Expand All @@ -117,10 +128,21 @@ public ReadOnlyCollection<IWebElement> FindElements(By by)
throw new ArgumentNullException(nameof(by), "by cannot be null");
}

return by.FindElements(this);
}

/// <summary>
/// Finds all child elements matching the given mechanism and value.
/// </summary>
/// <param name="mechanism">The mechanism by which to find the elements.</param>
/// <param name="value">The value to use to search for the elements.</param>
/// <returns>A collection of all of the <see cref="IWebElement">IWebElements</see> matching the given criteria.</returns>
public virtual ReadOnlyCollection<IWebElement> FindElements(string mechanism, string value)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("id", this.shadowRootId);
parameters.Add("using", by.Mechanism);
parameters.Add("value", by.Criteria);
parameters.Add("using", mechanism);
parameters.Add("value", value);
Copy link
Contributor Author

@RenderMichael RenderMichael Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem - RelativeBy. Before this PR, calling this with a relative locator would have Mechanism = "" and Criteria = "". The exception would be confusing, but still clear something is wrong here.

Now, it does a full-document search and ignores the shadow root. I think this is the worst of all options: wrong behavior.

For that reason, I propose we explicitly throw if we have relative locators + shadow root queries.


Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElements, parameters);
return this.driver.GetElementsFromResponse(commandResponse);
Expand Down
16 changes: 16 additions & 0 deletions dotnet/test/common/ShadowRootHandlingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// </copyright>

using NUnit.Framework;
using System.Collections.ObjectModel;

namespace OpenQA.Selenium
{
Expand All @@ -42,8 +43,23 @@ public void ShouldFindElementUnderShadowRoot()
driver.Url = shadowRootPage;
IWebElement element = driver.FindElement(By.CssSelector("custom-checkbox-element"));
ISearchContext shadowRoot = element.GetShadowRoot();

IWebElement elementInShadow = shadowRoot.FindElement(By.CssSelector("input"));
Assert.That(elementInShadow.GetAttribute("type"), Is.EqualTo("checkbox"), "Did not find element in shadow root");

ReadOnlyCollection<IWebElement> elementsInShadow = shadowRoot.FindElements(By.CssSelector("input"));
Assert.That(elementsInShadow, Has.One.Items, "Did not find element in shadow root");
Assert.That(elementsInShadow[0].GetAttribute("type"), Is.EqualTo("checkbox"), "Did not find element in shadow root");
}

[Test]
[IgnoreBrowser(Browser.Firefox, "Firefox does not support finding Shadow DOM elements")]
public void ShouldUseByMethodsToFindElementsUnderShadowRoot()
{
driver.Url = shadowRootPage;
IWebElement element = driver.FindElement(By.CssSelector("custom-checkbox-element"));
ISearchContext shadowRoot = element.GetShadowRoot();
Assert.That(shadowRoot.FindElements(By.Id("")), Is.Empty, "Searching for elements by empty ID should return empty");
}

[Test]
Expand Down
Loading