Skip to content

Commit

Permalink
Adding more tests for relative locators
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Sep 21, 2021
1 parent 3971e29 commit d1fd313
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions java/test/org/openqa/selenium/support/locators/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ java_selenium_test_suite(
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/remote",
"//java/src/org/openqa/selenium/support/locators",
"//java/test/org/openqa/selenium/environment",
"//java/test/org/openqa/selenium/testing:test-base",
artifact("junit:junit"),
artifact("org.assertj:assertj-core"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.environment.webserver.Page;
import org.openqa.selenium.testing.JUnit4TestBase;

import java.util.List;
Expand All @@ -44,7 +45,6 @@ public void shouldBeAbleToFindElementsAboveAnotherWithTagName() {
List<String> ids = elements.stream().map(e -> e.getAttribute("id")).collect(Collectors.toList());

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

}

@Test
Expand Down Expand Up @@ -165,4 +165,36 @@ public void exerciseNearLocatorWithCssSelector() {
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 ensureNoRepeatedElements() {
String url = appServer.create(new Page()
.withTitle("Repeated Elements")
.withStyles(" .c {\n" +
" \tposition: absolute;\n" +
" \tborder: 1px solid black;\n" +
" \theight: 50px;\n" +
" \twidth: 50px;\n" +
" }")
.withBody("<span style=\"position: relative;\">\n" +
" <div id= \"a\" class=\"c\" style=\"left:25;top:0;\">El-A</div>\n" +
" <div id= \"b\" class=\"c\" style=\"left:78;top:30;\">El-B</div>\n" +
" <div id= \"c\" class=\"c\" style=\"left:131;top:60;\">El-C</div>\n" +
" <div id= \"d\" class=\"c\" style=\"left:0;top:53;\">El-D</div>\n" +
" <div id= \"e\" class=\"c\" style=\"left:53;top:83;\">El-E</div>\n" +
" <div id= \"f\" class=\"c\" style=\"left:106;top:113;\">El-F</div>\n" +
" </span>"));

driver.get(url);

WebElement base = driver.findElement(By.id("e"));
List<WebElement> cells = driver.findElements(with(tagName("div")).above(base));

WebElement a = driver.findElement(By.id("a"));
WebElement b = driver.findElement(By.id("b"));

assertThat(cells)
.describedAs(cells.stream().map(e -> e.getAttribute("id")).collect(Collectors.joining(", ")))
.isEqualTo(List.of(b, a));
}
}
32 changes: 30 additions & 2 deletions javascript/atoms/test/relative_locator_test.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!DOCTYPE html>
<html>
<head>
<title>relative_locator_test.html</title>
Expand Down Expand Up @@ -53,13 +52,22 @@
width: 40px;
background-color: blue;
}

.c {
position: absolute;
border: 1px solid black;
height: 50px;
width: 50px;
}
</style>

<script src="test_bootstrap.js"></script>
<script type="text/javascript">
goog.require('bot.locators');
goog.require('goog.array');
goog.require('goog.testing.jsunit');
</script>

<script type="text/javascript">

function above(locator) {
Expand Down Expand Up @@ -163,6 +171,16 @@
assertEquals("below", found[0].getAttribute("id"));
}

function testShouldNotRepeatElements() {
var base = bot.locators.findElement({id: "e"});

var found = bot.locators.findElements({relative: {root: {tagName: "div"}, filters: [{kind: "above", args: [base]}]}});

assertEquals(2, found.length);
assertEquals("b", found[0].getAttribute("id"));
assertEquals("a", found[1].getAttribute("id"));
}

function testShouldOrderNodesByProximity() {
// Create an iframe to hold the content
var frame = document.createElement("iframe");
Expand Down Expand Up @@ -217,11 +235,11 @@
<body>

<h1>Relative Locator Tests</h1>

<p id="above">This text is above.
<p id="mid">This is a paragraph of text in the middle.
<p id="below">This text is below.


<table>
<tr>
<td id="first">1</td>
Expand All @@ -240,4 +258,14 @@ <h1>Relative Locator Tests</h1>
</tr>
</table>

<span style="position: relative">
<div id= "a" class="c" style="left:25;top:0;">El-A</div>
<div id= "b" class="c" style="left:78;top:30;">El-B</div>
<div id= "c" class="c" style="left:131;top:60;">El-C</div>
<div id= "d" class="c" style="left:0;top:53;">El-D</div>
<div id= "e" class="c" style="left:53;top:83;">El-E</div>
<div id= "f" class="c" style="left:106;top:113;">El-F</div>
</span>

</body>
</html>

0 comments on commit d1fd313

Please sign in to comment.