Skip to content

Commit

Permalink
Allow RelativeBy to start with any locator, not just tag name (#9273)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustasMonkev authored Mar 23, 2021
1 parent 9623950 commit 6a3f867
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class RelativeLocator {

private static final Json JSON = new Json();
private static final String FIND_ELEMENTS;

static {
try {
String location = String.format(
Expand All @@ -87,14 +88,31 @@ public class RelativeLocator {
throw new UncheckedIOException(e);
}
}

private static final int CLOSE_IN_PIXELS = 100;

/**
* Start of a relative locator, finding elements by tag name.
*/
public static RelativeBy withTagName(String tagName) {

public static RelativeBy with(By by) {
Require.nonNull("By to look for", by);
return new RelativeBy(by);
}

public static By tagName(String tagName) {
Require.nonNull("Tag name to look for", tagName);
return new RelativeBy(By.tagName(tagName));
return By.tagName(tagName);
}

public static By xpath(String xpathExpression) {
Require.nonNull("xpath to look for", xpathExpression);
return By.xpath(xpathExpression);
}

public static By cssSelector(String cssSelectorExpression) {
Require.nonNull("css selector to look for", cssSelectorExpression);
return By.cssSelector(cssSelectorExpression);
}

public static class RelativeBy extends By implements By.Remotable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,130 @@
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
import static org.openqa.selenium.support.locators.RelativeLocator.withXpath;
import static org.openqa.selenium.support.locators.RelativeLocator.withCssSelector;


public class RelativeLocatorTest extends JUnit4TestBase {

@Test
public void shouldBeAbleToFindElementsAboveAnother() {
public void shouldBeAbleToFindElementsAboveAnotherWithTagName() {
driver.get(appServer.whereIs("relative_locators.html"));

WebElement lowest = driver.findElement(By.id("below"));

List<WebElement> elements = driver.findElements(withTagName("p").above(lowest));
List<WebElement> elements = driver.findElements(with(tagName("p")).above(lowest));
List<String> ids = elements.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());

assertThat(ids).containsExactly("mid", "above");

}

@Test
public void shouldBeAbleToFindElementsAboveAnotherWithXpath() {
driver.get(appServer.whereIs("relative_locators.html"));

WebElement lowest = driver.findElement(By.id("seventh"));

List<WebElement> seen = driver.findElements(with(xpath("//td[1]")).above(lowest));

List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());

assertThat(ids).containsExactly("fourth", "first");
}

@Test
public void shouldBeAbleToFindElementsAboveAnotherwithCssSelector() {
driver.get(appServer.whereIs("relative_locators.html"));

WebElement lowest = driver.findElement(By.id("below"));

List<WebElement> elements = driver.findElements(with(cssSelector("p")).above(lowest));
List<String> ids = elements.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());

assertThat(ids).containsExactly("mid", "above");

}

@Test
public void shouldBeAbleToCombineFilters() {
driver.get(appServer.whereIs("relative_locators.html"));

List<WebElement> seen = driver.findElements(withTagName("td").above(By.id("center")).toRightOf(By.id("second")));
List<WebElement> seen = driver.findElements(with(tagName("td")).above(By.id("center")).toRightOf(By.id("second")));

List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());

assertThat(ids).containsExactly("third");
}

@Test
public void exerciseNearLocator() {
public void shouldBeAbleToCombineFiltersWithXpath() {
driver.get(appServer.whereIs("relative_locators.html"));

List<WebElement> seen = driver.findElements(with(xpath("//td[1]")).below(By.id("second")).above(By.id("seventh")));

List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());

assertThat(ids).containsExactly("fourth");

}

@Test
public void shouldBeAbleToCombineFiltersWithCssSelector() {
driver.get(appServer.whereIs("relative_locators.html"));


List<WebElement> seen = driver.findElements(with(cssSelector("td")).above(By.id("center")).toRightOf(By.id("second")));

List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());

assertThat(ids).containsExactly("third");
}

@Test
public void exerciseNearLocatorWithTagName() {
driver.get(appServer.whereIs("relative_locators.html"));

List<WebElement> seen = driver.findElements(with(tagName("td")).near(By.id("center")));

// Elements are sorted by proximity and then DOM insertion order.
// Proximity is determined using distance from center points, so
// we expect the order to be:
// 1. Directly above (short vertical distance, first in DOM)
// 2. Directly below (short vertical distance, later in DOM)
// 3. Directly left (slight longer distance horizontally, first in DOM)
// 4. Directly right (slight longer distance horizontally, later in DOM)
// 5-8. Diagonally close (pythagorus sorting, with top row first
// because of DOM insertion order)
List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());
assertThat(ids).containsExactly("second", "eighth", "fourth", "sixth", "first", "third", "seventh", "ninth");
}

@Test
public void exerciseNearLocatorWithXpath() {
driver.get(appServer.whereIs("relative_locators.html"));

List<WebElement> seen = driver.findElements(with(xpath("//td")).near(By.id("center")));

// Elements are sorted by proximity and then DOM insertion order.
// Proximity is determined using distance from center points, so
// we expect the order to be:
// 1. Directly above (short vertical distance, first in DOM)
// 2. Directly below (short vertical distance, later in DOM)
// 3. Directly left (slight longer distance horizontally, first in DOM)
// 4. Directly right (slight longer distance horizontally, later in DOM)
// 5-8. Diagonally close (pythagorus sorting, with top row first
// because of DOM insertion order)
List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());

assertThat(ids).containsExactly("second", "eighth", "fourth", "sixth", "first", "third", "seventh", "ninth");
}

@Test
public void exerciseNearLocatorWithCssSelector() {
driver.get(appServer.whereIs("relative_locators.html"));

List<WebElement> seen = driver.findElements(withTagName("td").near(By.id("center")));
List<WebElement> seen = driver.findElements(with(cssSelector("td")).near(By.id("center")));

// Elements are sorted by proximity and then DOM insertion order.
// Proximity is determined using distance from center points, so
Expand Down

0 comments on commit 6a3f867

Please sign in to comment.