3030import org .openqa .selenium .By ;
3131import org .openqa .selenium .NoSuchElementException ;
3232import org .openqa .selenium .WebElement ;
33+ import org .openqa .selenium .environment .webserver .Page ;
3334import org .openqa .selenium .testing .JupiterTestBase ;
3435
3536class RelativeLocatorTest extends JupiterTestBase {
@@ -130,7 +131,7 @@ void shouldFindElementsRightOfAnother() {
130131
131132 WebElement midpoint = driver .findElement (By .id ("center" ));
132133
133- List <WebElement > elements = driver .findElements (with (tagName ("td" )).right (midpoint ));
134+ List <WebElement > elements = driver .findElements (with (tagName ("td" )).toRightOf (midpoint ));
134135 List <String > ids =
135136 elements .stream ().map (e -> e .getAttribute ("id" )).collect (Collectors .toList ());
136137
@@ -169,11 +170,11 @@ void shouldBeAbleToFindElementsStraightLeftOfAnother() {
169170
170171 WebElement right = driver .findElement (By .id ("right" ));
171172
172- List <WebElement > elements = driver .findElements (with (tagName ("td" )).straightLeft (right ));
173+ List <WebElement > elements = driver .findElements (with (tagName ("td" )).straightLeftOf (right ));
173174 List <String > ids =
174175 elements .stream ().map (e -> e .getAttribute ("id" )).collect (Collectors .toList ());
175176
176- assertThat (ids ).containsExactly ("left " , "center " );
177+ assertThat (ids ).containsExactly ("center " , "left " );
177178 }
178179
179180 @ Test
@@ -182,7 +183,7 @@ void shouldBeAbleToFindElementsStraightRightOfAnother() {
182183
183184 WebElement left = driver .findElement (By .id ("left" ));
184185
185- List <WebElement > elements = driver .findElements (with (tagName ("td" )).straightRight (left ));
186+ List <WebElement > elements = driver .findElements (with (tagName ("td" )).straightRightOf (left ));
186187 List <String > ids =
187188 elements .stream ().map (e -> e .getAttribute ("id" )).collect (Collectors .toList ());
188189
@@ -259,7 +260,7 @@ void shouldBeAbleToCombineStraightFilters() {
259260 driver .findElements (
260261 with (tagName ("td" ))
261262 .straightBelow (By .id ("topRight" ))
262- .straightRight (By .id ("bottomLeft" )));
263+ .straightRightOf (By .id ("bottomLeft" )));
263264
264265 List <String > ids = seen .stream ().map (e -> e .getAttribute ("id" )).collect (Collectors .toList ());
265266
@@ -312,7 +313,27 @@ void exerciseNearLocatorWithCssSelector() {
312313
313314 @ Test
314315 void ensureNoRepeatedElements () {
315- driver .get (appServer .whereIs ("relative_locators.html" ));
316+ String url =
317+ appServer .create (
318+ new Page ()
319+ .withTitle ("Repeated Elements" )
320+ .withStyles (
321+ " .c {\n "
322+ + " \t position: absolute;\n "
323+ + " \t border: 1px solid black;\n "
324+ + " \t height: 50px;\n "
325+ + " \t width: 50px;\n "
326+ + " }" )
327+ .withBody (
328+ "<span style=\" position: relative;\" >\n "
329+ + " <div id= \" a\" class=\" c\" style=\" left:25;top:0;\" >El-A</div>\n "
330+ + " <div id= \" b\" class=\" c\" style=\" left:78;top:30;\" >El-B</div>\n "
331+ + " <div id= \" c\" class=\" c\" style=\" left:131;top:60;\" >El-C</div>\n "
332+ + " <div id= \" d\" class=\" c\" style=\" left:0;top:53;\" >El-D</div>\n "
333+ + " <div id= \" e\" class=\" c\" style=\" left:53;top:83;\" >El-E</div>\n "
334+ + " <div id= \" f\" class=\" c\" style=\" left:106;top:113;\" >El-F</div>\n "
335+ + " </span>" ));
336+ driver .get (url );
316337
317338 WebElement base = driver .findElement (By .id ("e" ));
318339 List <WebElement > cells = driver .findElements (with (tagName ("div" )).above (base ));
@@ -340,10 +361,10 @@ void nearLocatorShouldFindNearElements() {
340361 void nearLocatorShouldNotFindFarElements () {
341362 driver .get (appServer .whereIs ("relative_locators.html" ));
342363
343- WebElement rect3 = driver .findElement (By .id ("rect3 " ));
364+ WebElement rect = driver .findElement (By .id ("rect1 " ));
344365
345366 assertThatExceptionOfType (NoSuchElementException .class )
346- .isThrownBy (() -> driver .findElement (with (By .id ("rect4" )).near (rect3 )))
367+ .isThrownBy (() -> driver .findElement (with (By .id ("rect4" )).near (rect )))
347368 .withMessageContaining ("Cannot locate an element using" );
348369 }
349370}
0 commit comments