Skip to content

Commit 64d6ade

Browse files
committed
[dotnet] Enable ShadowRoot to use By query mechanism
1 parent 129da86 commit 64d6ade

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

dotnet/src/webdriver/ShadowRoot.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace OpenQA.Selenium
3030
/// <summary>
3131
/// Provides a representation of an element's shadow root.
3232
/// </summary>
33-
public class ShadowRoot : ISearchContext, IWrapsDriver, IWebDriverObjectReference
33+
public class ShadowRoot : ISearchContext, IWrapsDriver, IWebDriverObjectReference, IFindsElement
3434
{
3535
/// <summary>
3636
/// The property name that represents an element shadow root in the wire protocol.
@@ -93,10 +93,21 @@ public IWebElement FindElement(By by)
9393
throw new ArgumentNullException(nameof(by), "by cannot be null");
9494
}
9595

96+
return by.FindElement(this);
97+
}
98+
99+
/// <summary>
100+
/// Finds a child element matching the given mechanism and value.
101+
/// </summary>
102+
/// <param name="mechanism">The mechanism by which to find the element.</param>
103+
/// <param name="value">The value to use to search for the element.</param>
104+
/// <returns>The first <see cref="IWebElement"/> matching the given criteria.</returns>
105+
public virtual IWebElement FindElement(string mechanism, string value)
106+
{
96107
Dictionary<string, object> parameters = new Dictionary<string, object>();
97108
parameters.Add("id", this.shadowRootId);
98-
parameters.Add("using", by.Mechanism);
99-
parameters.Add("value", by.Criteria);
109+
parameters.Add("using", mechanism);
110+
parameters.Add("value", value);
100111

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

131+
return by.FindElements(this);
132+
}
133+
134+
/// <summary>
135+
/// Finds all child elements matching the given mechanism and value.
136+
/// </summary>
137+
/// <param name="mechanism">The mechanism by which to find the elements.</param>
138+
/// <param name="value">The value to use to search for the elements.</param>
139+
/// <returns>A collection of all of the <see cref="IWebElement">IWebElements</see> matching the given criteria.</returns>
140+
public virtual ReadOnlyCollection<IWebElement> FindElements(string mechanism, string value)
141+
{
120142
Dictionary<string, object> parameters = new Dictionary<string, object>();
121143
parameters.Add("id", this.shadowRootId);
122-
parameters.Add("using", by.Mechanism);
123-
parameters.Add("value", by.Criteria);
144+
parameters.Add("using", mechanism);
145+
parameters.Add("value", value);
124146

125147
Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElements, parameters);
126148
return this.driver.GetElementsFromResponse(commandResponse);

dotnet/test/common/ShadowRootHandlingTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// </copyright>
1919

2020
using NUnit.Framework;
21+
using System.Collections.ObjectModel;
2122

2223
namespace OpenQA.Selenium
2324
{
@@ -42,8 +43,23 @@ public void ShouldFindElementUnderShadowRoot()
4243
driver.Url = shadowRootPage;
4344
IWebElement element = driver.FindElement(By.CssSelector("custom-checkbox-element"));
4445
ISearchContext shadowRoot = element.GetShadowRoot();
46+
4547
IWebElement elementInShadow = shadowRoot.FindElement(By.CssSelector("input"));
4648
Assert.That(elementInShadow.GetAttribute("type"), Is.EqualTo("checkbox"), "Did not find element in shadow root");
49+
50+
ReadOnlyCollection<IWebElement> elementsInShadow = shadowRoot.FindElements(By.CssSelector("input"));
51+
Assert.That(elementsInShadow, Has.One.Items, "Did not find element in shadow root");
52+
Assert.That(elementsInShadow[0].GetAttribute("type"), Is.EqualTo("checkbox"), "Did not find element in shadow root");
53+
}
54+
55+
[Test]
56+
[IgnoreBrowser(Browser.Firefox, "Firefox does not support finding Shadow DOM elements")]
57+
public void ShouldUseByMethodsToFindElementsUnderShadowRoot()
58+
{
59+
driver.Url = shadowRootPage;
60+
IWebElement element = driver.FindElement(By.CssSelector("custom-checkbox-element"));
61+
ISearchContext shadowRoot = element.GetShadowRoot();
62+
Assert.That(shadowRoot.FindElements(By.Id("")), Is.Empty, "Searching for elements by empty ID should return empty");
4763
}
4864

4965
[Test]

0 commit comments

Comments
 (0)