Skip to content

Commit 7f11d4e

Browse files
committed
Java: Escape spaces in the ID when translating "find elements by ID" to the W3C protocol.
Add tests for finding elements by IDs that contain spaces or other CSS special characters.
1 parent 3ab6364 commit 7f11d4e

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

common/src/web/nestedElements.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
<div id="test_id_div">
66
<p id="test_id">inside</p>
77
</div>
8+
<div id="test_special_chars">
9+
<p id="white space">space</p>
10+
<p id="css#.chars">css escapes</p>
11+
</div>
12+
<div id="test_special_chars_2">
13+
<p id="white space">second copy for testing plural findElements</p>
14+
<p id="css#.chars">second copy</p>
15+
</div>
16+
817
<form method="get" action="resultPage.html" name="form1" style="display: block">
918
Here's a checkbox: <input type="checkbox" id="checky" name="checky" value="furrfu"/><br/>
1019
<select name="selectomatic" id="1">
@@ -152,4 +161,4 @@
152161
<span class="oneother">But not me</span>
153162
</div>
154163
</body>
155-
</html>
164+
</html>

java/client/src/org/openqa/selenium/remote/http/W3CHttpCommandCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private Map<String, String> asElement(Object id) {
342342
}
343343

344344
private String cssEscape(String using) {
345-
using = using.replaceAll("(['\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])", "\\\\$1");
345+
using = using.replaceAll("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])", "\\\\$1");
346346
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
347347
using = "\\" + Integer.toString(30 + Integer.parseInt(using.substring(0,1))) + " " + using.substring(1);
348348
}

java/client/test/org/openqa/selenium/ChildrenFindingTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import static org.junit.Assert.assertNotNull;
2424
import static org.junit.Assert.assertThat;
2525
import static org.junit.Assert.fail;
26+
import static org.openqa.selenium.testing.Driver.CHROME;
27+
import static org.openqa.selenium.testing.Driver.IE;
2628
import static org.openqa.selenium.testing.Driver.REMOTE;
2729

2830
import org.junit.Test;
@@ -122,6 +124,19 @@ public void testFindElementByIdWhenMultipleMatchesExist() {
122124
assertThat(child.getText(), is("inside"));
123125
}
124126

127+
@Ignore(
128+
value = {CHROME, IE},
129+
reason = "Need to recompile drivers with atoms from 6c55320d3f0eb23de56270a55c74602fc8d63c8a")
130+
@Test
131+
public void testFindElementByIdWhenIdContainsNonAlphanumericCharacters() {
132+
driver.get(pages.nestedPage);
133+
WebElement element = driver.findElement(By.id("test_special_chars"));
134+
WebElement childWithSpaces = element.findElement(By.id("white space"));
135+
assertThat(childWithSpaces.getText(), is("space"));
136+
WebElement childWithCssChars = element.findElement(By.id("css#.chars"));
137+
assertThat(childWithCssChars.getText(), is("css escapes"));
138+
}
139+
125140
@Test
126141
public void testFindElementByIdWhenNoMatchInContext() {
127142
driver.get(pages.nestedPage);
@@ -142,6 +157,19 @@ public void testFindElementsById() {
142157
assertThat(children.size(), is(2));
143158
}
144159

160+
@Ignore(
161+
value = {CHROME, IE},
162+
reason = "Need to recompile drivers with atoms from 6c55320d3f0eb23de56270a55c74602fc8d63c8a")
163+
@Test
164+
public void testFindElementsByIdWithNonAlphanumericCharacters() {
165+
driver.get(pages.nestedPage);
166+
WebElement element = driver.findElement(By.id("test_special_chars"));
167+
List<WebElement> children = element.findElements(By.id("white space"));
168+
assertThat(children.size(), is(1));
169+
List<WebElement> children2 = element.findElements(By.id("css#.chars"));
170+
assertThat(children2.size(), is(1));
171+
}
172+
145173
@Test
146174
public void testFindElementByLinkText() {
147175
driver.get(pages.nestedPage);

java/client/test/org/openqa/selenium/ElementFindingTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ public void testShouldBeAbleToFindASingleElementByNumericId() {
6262
assertThat(element.getAttribute("id"), is("2"));
6363
}
6464

65+
@Ignore(
66+
value = {CHROME, IE},
67+
reason = "Need to recompile drivers with atoms from 6c55320d3f0eb23de56270a55c74602fc8d63c8a")
68+
@Test
69+
public void testShouldBeAbleToFindASingleElementByIdWithNonAlphanumericCharacters() {
70+
driver.get(pages.nestedPage);
71+
WebElement element = driver.findElement(By.id("white space"));
72+
assertThat(element.getText(), is("space"));
73+
WebElement element2 = driver.findElement(By.id("css#.chars"));
74+
assertThat(element2.getText(), is("css escapes"));
75+
}
76+
6577
@Test
6678
public void testShouldBeAbleToFindMultipleElementsById() {
6779
driver.get(pages.nestedPage);
@@ -76,6 +88,18 @@ public void testShouldBeAbleToFindMultipleElementsByNumericId() {
7688
assertThat(elements.size(), is(8));
7789
}
7890

91+
@Ignore(
92+
value = {CHROME, IE},
93+
reason = "Need to recompile drivers with atoms from 6c55320d3f0eb23de56270a55c74602fc8d63c8a")
94+
@Test
95+
public void testShouldBeAbleToFindMultipleElementsByIdWithNonAlphanumericCharacters() {
96+
driver.get(pages.nestedPage);
97+
List<WebElement> elements = driver.findElements(By.id("white space"));
98+
assertThat(elements.size(), is(2));
99+
List<WebElement> elements2 = driver.findElements(By.id("css#.chars"));
100+
assertThat(elements2.size(), is(2));
101+
}
102+
79103
// By.id negative
80104

81105
@Test(expected = NoSuchElementException.class)

0 commit comments

Comments
 (0)