|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.htmlunit.by; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import org.junit.Ignore; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.openqa.selenium.By; |
| 27 | +import org.openqa.selenium.WebDriver; |
| 28 | +import org.openqa.selenium.WebElement; |
| 29 | +import org.openqa.selenium.htmlunit.WebDriverTestCase; |
| 30 | +import org.openqa.selenium.htmlunit.junit.BrowserRunner; |
| 31 | +import org.openqa.selenium.support.locators.RelativeLocator; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for RelativeLocator. |
| 35 | + * |
| 36 | + * @author Ronald Brill |
| 37 | + */ |
| 38 | +@RunWith(BrowserRunner.class) |
| 39 | +public class RelativeLocatorTest extends WebDriverTestCase { |
| 40 | + |
| 41 | + @Test |
| 42 | + @Ignore |
| 43 | + public void shouldBeAbleToFindElementsAboveAnotherWithTagName() throws Exception { |
| 44 | + final String html = getFileContent("relative_locators.html"); |
| 45 | + |
| 46 | + final WebDriver driver = loadPage2(html); |
| 47 | + |
| 48 | + final WebElement lowest = driver.findElement(By.id("below")); |
| 49 | + |
| 50 | + final List<WebElement> elements = driver.findElements(RelativeLocator.with(By.tagName("p")).above(lowest)); |
| 51 | + final List<String> ids = elements.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | +// @Test |
| 56 | +// void shouldBeAbleToFindElementsAboveAnotherWithTagName() { |
| 57 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 58 | +// |
| 59 | +// WebElement lowest = driver.findElement(By.id("below")); |
| 60 | +// |
| 61 | +// List<WebElement> elements = driver.findElements(with(tagName("p")).above(lowest)); |
| 62 | +// List<String> ids = |
| 63 | +// elements.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 64 | +// |
| 65 | +// assertThat(ids).containsExactly("mid", "above"); |
| 66 | +// } |
| 67 | + |
| 68 | +// @Test |
| 69 | +// void shouldBeAbleToFindElementsAboveAnotherWithXpath() { |
| 70 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 71 | +// |
| 72 | +// WebElement lowest = driver.findElement(By.id("seventh")); |
| 73 | +// |
| 74 | +// List<WebElement> seen = driver.findElements(with(xpath("//td[1]")).above(lowest)); |
| 75 | +// |
| 76 | +// List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 77 | +// |
| 78 | +// assertThat(ids).containsExactly("fourth", "first"); |
| 79 | +// } |
| 80 | +// |
| 81 | +// @Test |
| 82 | +// void shouldBeAbleToFindElementsAboveAnotherWithCssSelector() { |
| 83 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 84 | +// |
| 85 | +// WebElement lowest = driver.findElement(By.id("below")); |
| 86 | +// |
| 87 | +// List<WebElement> elements = driver.findElements(with(cssSelector("p")).above(lowest)); |
| 88 | +// List<String> ids = |
| 89 | +// elements.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 90 | +// |
| 91 | +// assertThat(ids).containsExactly("mid", "above"); |
| 92 | +// } |
| 93 | +// |
| 94 | +// @Test |
| 95 | +// void shouldBeAbleToCombineFilters() { |
| 96 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 97 | +// |
| 98 | +// List<WebElement> seen = |
| 99 | +// driver.findElements(with(tagName("td")).above(By.id("center")).toRightOf(By.id("second"))); |
| 100 | +// |
| 101 | +// List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 102 | +// |
| 103 | +// assertThat(ids).containsExactly("third"); |
| 104 | +// } |
| 105 | +// |
| 106 | +// @Test |
| 107 | +// void shouldBeAbleToCombineFiltersWithXpath() { |
| 108 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 109 | +// |
| 110 | +// List<WebElement> seen = |
| 111 | +// driver.findElements(with(xpath("//td[1]")).below(By.id("second")).above(By.id("seventh"))); |
| 112 | +// |
| 113 | +// List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 114 | +// |
| 115 | +// assertThat(ids).containsExactly("fourth"); |
| 116 | +// } |
| 117 | +// |
| 118 | +// @Test |
| 119 | +// void shouldBeAbleToCombineFiltersWithCssSelector() { |
| 120 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 121 | +// |
| 122 | +// List<WebElement> seen = |
| 123 | +// driver.findElements( |
| 124 | +// with(cssSelector("td")).above(By.id("center")).toRightOf(By.id("second"))); |
| 125 | +// |
| 126 | +// List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 127 | +// |
| 128 | +// assertThat(ids).containsExactly("third"); |
| 129 | +// } |
| 130 | +// |
| 131 | +// @Test |
| 132 | +// void exerciseNearLocatorWithTagName() { |
| 133 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 134 | +// |
| 135 | +// List<WebElement> seen = driver.findElements(with(tagName("td")).near(By.id("center"))); |
| 136 | +// |
| 137 | +// // Elements are sorted by proximity and then DOM insertion order. |
| 138 | +// // Proximity is determined using distance from center points, so |
| 139 | +// // we expect the order to be: |
| 140 | +// // 1. Directly above (short vertical distance, first in DOM) |
| 141 | +// // 2. Directly below (short vertical distance, later in DOM) |
| 142 | +// // 3. Directly left (slight longer distance horizontally, first in DOM) |
| 143 | +// // 4. Directly right (slight longer distance horizontally, later in DOM) |
| 144 | +// // 5-8. Diagonally close (pythagoras sorting, with top row first |
| 145 | +// // because of DOM insertion order) |
| 146 | +// List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 147 | +// assertThat(ids) |
| 148 | +// .containsExactly( |
| 149 | +// "second", "eighth", "fourth", "sixth", "first", "third", "seventh", "ninth"); |
| 150 | +// } |
| 151 | +// |
| 152 | +// @Test |
| 153 | +// void exerciseNearLocatorWithXpath() { |
| 154 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 155 | +// |
| 156 | +// List<WebElement> seen = driver.findElements(with(xpath("//td")).near(By.id("center"))); |
| 157 | +// |
| 158 | +// // Elements are sorted by proximity and then DOM insertion order. |
| 159 | +// // Proximity is determined using distance from center points, so |
| 160 | +// // we expect the order to be: |
| 161 | +// // 1. Directly above (short vertical distance, first in DOM) |
| 162 | +// // 2. Directly below (short vertical distance, later in DOM) |
| 163 | +// // 3. Directly left (slight longer distance horizontally, first in DOM) |
| 164 | +// // 4. Directly right (slight longer distance horizontally, later in DOM) |
| 165 | +// // 5-8. Diagonally close (pythagoras sorting, with top row first |
| 166 | +// // because of DOM insertion order) |
| 167 | +// List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 168 | +// |
| 169 | +// assertThat(ids) |
| 170 | +// .containsExactly( |
| 171 | +// "second", "eighth", "fourth", "sixth", "first", "third", "seventh", "ninth"); |
| 172 | +// } |
| 173 | +// |
| 174 | +// @Test |
| 175 | +// void exerciseNearLocatorWithCssSelector() { |
| 176 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 177 | +// |
| 178 | +// List<WebElement> seen = driver.findElements(with(cssSelector("td")).near(By.id("center"))); |
| 179 | +// |
| 180 | +// // Elements are sorted by proximity and then DOM insertion order. |
| 181 | +// // Proximity is determined using distance from center points, so |
| 182 | +// // we expect the order to be: |
| 183 | +// // 1. Directly above (short vertical distance, first in DOM) |
| 184 | +// // 2. Directly below (short vertical distance, later in DOM) |
| 185 | +// // 3. Directly left (slight longer distance horizontally, first in DOM) |
| 186 | +// // 4. Directly right (slight longer distance horizontally, later in DOM) |
| 187 | +// // 5-8. Diagonally close (pythagoras sorting, with top row first |
| 188 | +// // because of DOM insertion order) |
| 189 | +// List<String> ids = seen.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList()); |
| 190 | +// assertThat(ids) |
| 191 | +// .containsExactly( |
| 192 | +// "second", "eighth", "fourth", "sixth", "first", "third", "seventh", "ninth"); |
| 193 | +// } |
| 194 | +// |
| 195 | +// @Test |
| 196 | +// void ensureNoRepeatedElements() { |
| 197 | +// String url = |
| 198 | +// appServer.create( |
| 199 | +// new Page() |
| 200 | +// .withTitle("Repeated Elements") |
| 201 | +// .withStyles( |
| 202 | +// " .c {\n" |
| 203 | +// + " \tposition: absolute;\n" |
| 204 | +// + " \tborder: 1px solid black;\n" |
| 205 | +// + " \theight: 50px;\n" |
| 206 | +// + " \twidth: 50px;\n" |
| 207 | +// + " }") |
| 208 | +// .withBody( |
| 209 | +// "<span style=\"position: relative;\">\n" |
| 210 | +// + " <div id= \"a\" class=\"c\" style=\"left:25;top:0;\">El-A</div>\n" |
| 211 | +// + " <div id= \"b\" class=\"c\" style=\"left:78;top:30;\">El-B</div>\n" |
| 212 | +// + " <div id= \"c\" class=\"c\" style=\"left:131;top:60;\">El-C</div>\n" |
| 213 | +// + " <div id= \"d\" class=\"c\" style=\"left:0;top:53;\">El-D</div>\n" |
| 214 | +// + " <div id= \"e\" class=\"c\" style=\"left:53;top:83;\">El-E</div>\n" |
| 215 | +// + " <div id= \"f\" class=\"c\" style=\"left:106;top:113;\">El-F</div>\n" |
| 216 | +// + " </span>")); |
| 217 | +// |
| 218 | +// driver.get(url); |
| 219 | +// |
| 220 | +// WebElement base = driver.findElement(By.id("e")); |
| 221 | +// List<WebElement> cells = driver.findElements(with(tagName("div")).above(base)); |
| 222 | +// |
| 223 | +// WebElement a = driver.findElement(By.id("a")); |
| 224 | +// WebElement b = driver.findElement(By.id("b")); |
| 225 | +// |
| 226 | +// assertThat(cells) |
| 227 | +// .describedAs( |
| 228 | +// cells.stream().map(e -> e.getAttribute("id")).collect(Collectors.joining(", "))) |
| 229 | +// .isEqualTo(List.of(b, a)); |
| 230 | +// } |
| 231 | +// |
| 232 | +// @Test |
| 233 | +// void nearLocatorShouldFindNearElements() { |
| 234 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 235 | +// |
| 236 | +// WebElement rect1 = driver.findElement(By.id("rect1")); |
| 237 | +// |
| 238 | +// WebElement rect2 = driver.findElement(with(By.id("rect2")).near(rect1)); |
| 239 | +// |
| 240 | +// assertThat(rect2.getAttribute("id")).isEqualTo("rect2"); |
| 241 | +// } |
| 242 | +// |
| 243 | +// @Test |
| 244 | +// void nearLocatorShouldNotFindFarElements() { |
| 245 | +// driver.get(appServer.whereIs("relative_locators.html")); |
| 246 | +// |
| 247 | +// WebElement rect3 = driver.findElement(By.id("rect3")); |
| 248 | +// |
| 249 | +// assertThatExceptionOfType(NoSuchElementException.class) |
| 250 | +// .isThrownBy(() -> driver.findElement(with(By.id("rect4")).near(rect3))) |
| 251 | +// .withMessageContaining("Cannot locate an element using"); |
| 252 | +// } |
| 253 | +} |
0 commit comments