|
28 | 28 | import static java.util.Collections.singletonList;
|
29 | 29 | import static org.assertj.core.api.Assertions.assertThat;
|
30 | 30 | import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
|
| 31 | +import static org.openqa.selenium.support.locators.RelativeLocator.withXpath; |
| 32 | +import static org.openqa.selenium.support.locators.RelativeLocator.withCssSelector; |
| 33 | + |
31 | 34 |
|
32 | 35 | public class RelativeLocatorTest extends JUnit4TestBase {
|
33 | 36 |
|
34 | 37 | @Test
|
35 |
| - public void shouldBeAbleToFindElementsAboveAnother() { |
| 38 | + public void shouldBeAbleToFindElementsAboveAnotherWithTagName() { |
36 | 39 | driver.get(appServer.whereIs("relative_locators.html"));
|
37 | 40 |
|
38 | 41 | WebElement lowest = driver.findElement(By.id("below"));
|
39 | 42 |
|
40 |
| - List<WebElement> elements = driver.findElements(withTagName("p").above(lowest)); |
| 43 | + List<WebElement> elements = driver.findElements(with(tagName("p")).above(lowest)); |
41 | 44 | List<String> ids = elements.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());
|
42 | 45 |
|
43 | 46 | assertThat(ids).containsExactly("mid", "above");
|
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void shouldBeAbleToFindElementsAboveAnotherWithXpath() { |
| 52 | + driver.get(appServer.whereIs("relative_locators.html")); |
| 53 | + |
| 54 | + WebElement lowest = driver.findElement(By.id("seventh")); |
| 55 | + |
| 56 | + List<WebElement> seen = driver.findElements(with(xpath("//td[1]")).above(lowest)); |
| 57 | + |
| 58 | + List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 59 | + |
| 60 | + assertThat(ids).containsExactly("fourth", "first"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void shouldBeAbleToFindElementsAboveAnotherwithCssSelector() { |
| 65 | + driver.get(appServer.whereIs("relative_locators.html")); |
| 66 | + |
| 67 | + WebElement lowest = driver.findElement(By.id("below")); |
| 68 | + |
| 69 | + List<WebElement> elements = driver.findElements(with(cssSelector("p")).above(lowest)); |
| 70 | + List<String> ids = elements.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 71 | + |
| 72 | + assertThat(ids).containsExactly("mid", "above"); |
| 73 | + |
44 | 74 | }
|
45 | 75 |
|
46 | 76 | @Test
|
47 | 77 | public void shouldBeAbleToCombineFilters() {
|
48 | 78 | driver.get(appServer.whereIs("relative_locators.html"));
|
49 | 79 |
|
50 |
| - List<WebElement> seen = driver.findElements(withTagName("td").above(By.id("center")).toRightOf(By.id("second"))); |
| 80 | + List<WebElement> seen = driver.findElements(with(tagName("td")).above(By.id("center")).toRightOf(By.id("second"))); |
51 | 81 |
|
52 | 82 | List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());
|
| 83 | + |
53 | 84 | assertThat(ids).containsExactly("third");
|
54 | 85 | }
|
55 | 86 |
|
56 | 87 | @Test
|
57 |
| - public void exerciseNearLocator() { |
| 88 | + public void shouldBeAbleToCombineFiltersWithXpath() { |
| 89 | + driver.get(appServer.whereIs("relative_locators.html")); |
| 90 | + |
| 91 | + List<WebElement> seen = driver.findElements(with(xpath("//td[1]")).below(By.id("second")).above(By.id("seventh"))); |
| 92 | + |
| 93 | + List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 94 | + |
| 95 | + assertThat(ids).containsExactly("fourth"); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void shouldBeAbleToCombineFiltersWithCssSelector() { |
| 101 | + driver.get(appServer.whereIs("relative_locators.html")); |
| 102 | + |
| 103 | + |
| 104 | + List<WebElement> seen = driver.findElements(with(cssSelector("td")).above(By.id("center")).toRightOf(By.id("second"))); |
| 105 | + |
| 106 | + List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 107 | + |
| 108 | + assertThat(ids).containsExactly("third"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void exerciseNearLocatorWithTagName() { |
| 113 | + driver.get(appServer.whereIs("relative_locators.html")); |
| 114 | + |
| 115 | + List<WebElement> seen = driver.findElements(with(tagName("td")).near(By.id("center"))); |
| 116 | + |
| 117 | + // Elements are sorted by proximity and then DOM insertion order. |
| 118 | + // Proximity is determined using distance from center points, so |
| 119 | + // we expect the order to be: |
| 120 | + // 1. Directly above (short vertical distance, first in DOM) |
| 121 | + // 2. Directly below (short vertical distance, later in DOM) |
| 122 | + // 3. Directly left (slight longer distance horizontally, first in DOM) |
| 123 | + // 4. Directly right (slight longer distance horizontally, later in DOM) |
| 124 | + // 5-8. Diagonally close (pythagorus sorting, with top row first |
| 125 | + // because of DOM insertion order) |
| 126 | + List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 127 | + assertThat(ids).containsExactly("second", "eighth", "fourth", "sixth", "first", "third", "seventh", "ninth"); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void exerciseNearLocatorWithXpath() { |
| 132 | + driver.get(appServer.whereIs("relative_locators.html")); |
| 133 | + |
| 134 | + List<WebElement> seen = driver.findElements(with(xpath("//td")).near(By.id("center"))); |
| 135 | + |
| 136 | + // Elements are sorted by proximity and then DOM insertion order. |
| 137 | + // Proximity is determined using distance from center points, so |
| 138 | + // we expect the order to be: |
| 139 | + // 1. Directly above (short vertical distance, first in DOM) |
| 140 | + // 2. Directly below (short vertical distance, later in DOM) |
| 141 | + // 3. Directly left (slight longer distance horizontally, first in DOM) |
| 142 | + // 4. Directly right (slight longer distance horizontally, later in DOM) |
| 143 | + // 5-8. Diagonally close (pythagorus sorting, with top row first |
| 144 | + // because of DOM insertion order) |
| 145 | + List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 146 | + |
| 147 | + assertThat(ids).containsExactly("second", "eighth", "fourth", "sixth", "first", "third", "seventh", "ninth"); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void exerciseNearLocatorWithCssSelector() { |
58 | 152 | driver.get(appServer.whereIs("relative_locators.html"));
|
59 | 153 |
|
60 |
| - List<WebElement> seen = driver.findElements(withTagName("td").near(By.id("center"))); |
| 154 | + List<WebElement> seen = driver.findElements(with(cssSelector("td")).near(By.id("center"))); |
61 | 155 |
|
62 | 156 | // Elements are sorted by proximity and then DOM insertion order.
|
63 | 157 | // Proximity is determined using distance from center points, so
|
|
0 commit comments