Skip to content

Commit c137b29

Browse files
committed
ad test cases (wip)
1 parent fde1f70 commit c137b29

File tree

2 files changed

+319
-0
lines changed

2 files changed

+319
-0
lines changed
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
<html>
3+
<head>
4+
<title>Relative Locators</title>
5+
<style>
6+
table {
7+
text-align: center;
8+
border: solid;
9+
}
10+
td {
11+
border: solid;
12+
}
13+
.small-rectangle {
14+
border: 1px solid black;
15+
width: 100px;
16+
height: 50px;
17+
margin: 5px 25px;
18+
}
19+
.big-rectangle {
20+
border: 1px solid black;
21+
width: 150px;
22+
height: 400px;
23+
}
24+
</style>
25+
</head>
26+
<body>
27+
<h1>Relative Locator Tests</h1>
28+
<p id="above">This text is above.
29+
<p id="mid">This is a paragraph of text in the middle.
30+
<p id="below">This text is below.
31+
32+
33+
<table>
34+
<tr>
35+
<td id="first">1</td>
36+
<td id="second" style="width: 100px">2</td>
37+
<td id="third">3</td>
38+
</tr>
39+
<tr>
40+
<td id="fourth">4</td>
41+
<td id="center">5</td>
42+
<td id="sixth">6</td>
43+
</tr>
44+
<tr>
45+
<td id="seventh">7</td>
46+
<td id="eighth">8</td>
47+
<td id="ninth">9</td>
48+
</tr>
49+
</table>
50+
51+
<div class="small-rectangle" id="rect1">
52+
Rectangle 1
53+
</div>
54+
<div class="big-rectangle" id="rect2">
55+
Rectangle 2, which is near Rectangle 1
56+
</div>
57+
58+
<div class="small-rectangle" style="margin:60px 25px" id="rect3">
59+
Rectangle 3
60+
</div>
61+
<div class="big-rectangle" id="rect4">
62+
Rectangle 4, which is not near Rectangle 2 because it is more than 50 px away
63+
</div>
64+
65+
</body>
66+
</html>

0 commit comments

Comments
 (0)