Skip to content

Commit 6bf216e

Browse files
committed
[dotnet]: Adding ability to use By object with relative locators
Also marking RelativeBy.WithTagName as obsolete. Users should change their calls to `RelativeBy.WithLocator(By.TagName(...))`.
1 parent 5496386 commit 6bf216e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

dotnet/src/webdriver/RelativeBy.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private RelativeBy(object root, List<object> filters) : this()
7070
/// </summary>
7171
/// <param name="tagName">The tag name of the element to find.</param>
7272
/// <returns>A <see cref="RelativeBy"/> object to be used in finding the elements.</returns>
73+
[Obsolete("Use the WithLocator method instead, passing By.TagName.")]
7374
public static RelativeBy WithTagName(string tagName)
7475
{
7576
if (string.IsNullOrEmpty(tagName))
@@ -80,6 +81,17 @@ public static RelativeBy WithTagName(string tagName)
8081
return new RelativeBy(By.TagName(tagName));
8182
}
8283

84+
/// <summary>
85+
/// Creates a new <see cref="RelativeBy"/> for finding elements with the specified tag name.
86+
/// </summary>
87+
/// <param name="by">A By object that will be used to find the initial element.</param>
88+
/// <returns>A <see cref="RelativeBy"/> object to be used in finding the elements.</returns>
89+
public static RelativeBy WithLocator(By by)
90+
{
91+
return new RelativeBy(by);
92+
}
93+
94+
8395
/// <summary>
8496
/// Finds the first element matching the criteria.
8597
/// </summary>
@@ -111,7 +123,7 @@ public override ReadOnlyCollection<IWebElement> FindElements(ISearchContext cont
111123
filterParameters["filters"] = this.filters;
112124
parameters["relative"] = filterParameters;
113125
object rawElements = js.ExecuteScript(wrappedAtom, parameters);
114-
ReadOnlyCollection<IWebElement> elements = rawElements as ReadOnlyCollection<IWebElement>;
126+
ReadOnlyCollection<IWebElement> elements = rawElements as ReadOnlyCollection<IWebElement>;
115127
return elements;
116128
}
117129

dotnet/test/common/RelativeLocatorTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void ShouldBeAbleToFindElementsAboveAnother()
1616

1717
IWebElement lowest = driver.FindElement(By.Id("below"));
1818

19-
ReadOnlyCollection<IWebElement> elements = driver.FindElements(RelativeBy.WithTagName("p").Above(lowest));
19+
ReadOnlyCollection<IWebElement> elements = driver.FindElements(RelativeBy.WithLocator(By.TagName("p")).Above(lowest));
2020
List<string> elementIds = new List<string>();
2121
foreach (IWebElement element in elements)
2222
{
@@ -32,7 +32,7 @@ public void ShouldBeAbleToCombineFilters()
3232
{
3333
driver.Url = (EnvironmentManager.Instance.UrlBuilder.WhereIs("relative_locators.html"));
3434

35-
ReadOnlyCollection<IWebElement> seen = driver.FindElements(RelativeBy.WithTagName("td").Above(By.Id("center")).RightOf(By.Id("second")));
35+
ReadOnlyCollection<IWebElement> seen = driver.FindElements(RelativeBy.WithLocator(By.TagName("td")).Above(By.Id("center")).RightOf(By.Id("second")));
3636

3737
List<string> elementIds = new List<string>();
3838
foreach (IWebElement element in seen)

0 commit comments

Comments
 (0)