Skip to content

Commit 3c77974

Browse files
committed
more unit tests (using #137 as inspiration) and fix for #139
1 parent 9f07982 commit 3c77974

3 files changed

Lines changed: 162 additions & 8 deletions

File tree

src/main/java/org/openqa/selenium/htmlunit/HtmlUnitWebElement.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,24 @@ public String getAttribute(final String name) {
290290
return trueOrNull(((HtmlInput) element_).isChecked());
291291
}
292292

293-
if ("href".equals(lowerName) || "src".equals(lowerName)) {
293+
if ("href".equals(lowerName)) {
294+
final String href = element_.getAttribute(name);
295+
if (ATTRIBUTE_NOT_DEFINED == href) {
296+
return null;
297+
}
298+
final HtmlPage page = (HtmlPage) element_.getPage();
299+
try {
300+
return page.getFullyQualifiedUrl(href.trim()).toString();
301+
}
302+
catch (final MalformedURLException e) {
303+
return null;
304+
}
305+
}
306+
307+
if ("src".equals(lowerName)) {
294308
final String link = element_.getAttribute(name);
295309
if (ATTRIBUTE_NOT_DEFINED == link) {
296-
return null;
310+
return "";
297311
}
298312
final HtmlPage page = (HtmlPage) element_.getPage();
299313
try {
@@ -305,9 +319,6 @@ public String getAttribute(final String name) {
305319
}
306320

307321
if ("value".equals(lowerName)) {
308-
if (element_ instanceof HtmlFileInput) {
309-
return ((HTMLInputElement) element_.getScriptableObject()).getValue();
310-
}
311322
if (element_ instanceof HtmlInput) {
312323
return ((HtmlInput) element_).getValue();
313324
}

src/test/java/org/openqa/selenium/htmlunit/HtmlUnitWebElementAttributeTest.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public void valueOption() throws Exception {
9393
+ " <option id='o1' value='one'>option one</option>\n"
9494
+ " <option id='o2'>option two</option>\n"
9595
+ " <option id='o3'>option three \n second line</option>\n"
96+
+ " <option id='o4' value='4'>option four</option>\n"
9697
+ " </select>\n"
9798
+ "</form>\n"
9899
+ "</body>\n"
@@ -108,6 +109,9 @@ public void valueOption() throws Exception {
108109

109110
elem = driver.findElement(By.id("o3"));
110111
assertEquals("option three second line", elem.getAttribute("value"));
112+
113+
elem = driver.findElement(By.id("o4"));
114+
assertEquals("4", elem.getAttribute("value"));
111115
}
112116

113117
@Test
@@ -243,4 +247,94 @@ public void inputTextValueTyped() throws Exception {
243247
elem.sendKeys("hello");
244248
assertEquals("worldhello", elem.getAttribute("value"));
245249
}
250+
251+
@Test
252+
public void inputTextAreaValueTyped() throws Exception {
253+
final String html = "<html>\n"
254+
+ "<head>\n"
255+
+ "</head>\n"
256+
+ "<body>\n"
257+
+ " <fieldset>\n"
258+
+ " <textarea id='textBx' name='text' >world</textarea>\n"
259+
+ " </fieldset>\n"
260+
+ "</body>\n"
261+
+ "</html>\n";
262+
263+
final WebDriver driver = loadPage2(html);
264+
265+
final WebElement elem = driver.findElement(By.id("textBx"));
266+
assertEquals("world", elem.getAttribute("value"));
267+
elem.sendKeys("hello");
268+
assertEquals("worldhello", elem.getAttribute("value"));
269+
}
270+
271+
@Test
272+
public void inputFileValue() throws Exception {
273+
final String html = "<html>\n"
274+
+ "<head>\n"
275+
+ "</head>\n"
276+
+ "<body>\n"
277+
+ " <fieldset>\n"
278+
+ " <input type='file' id='myFile1' name='myFile' accept='image/png'/>\n"
279+
+ " <input type='file' id='myFile2' name='myFile' accept='image/png' value='secret.txt' />\n"
280+
+ " </fieldset>\n"
281+
+ "</body>\n"
282+
+ "</html>\n";
283+
284+
final WebDriver driver = loadPage2(html);
285+
286+
WebElement elem = driver.findElement(By.id("myFile1"));
287+
assertEquals("", elem.getAttribute("value"));
288+
289+
elem = driver.findElement(By.id("myFile2"));
290+
assertEquals("", elem.getAttribute("value"));
291+
}
292+
293+
@Test
294+
public void anchor() throws Exception {
295+
final String html = "<html>\n"
296+
+ "<head>\n"
297+
+ "</head>\n"
298+
+ "<body>\n"
299+
+ " <a id='a1'>empty</a>"
300+
+ " <a id='a2' href='test'>empty</a>"
301+
+ " <a id='a3' href='https://www.htmlunit.org/href'>empty</a>"
302+
+ "</body>\n"
303+
+ "</html>\n";
304+
305+
final WebDriver driver = loadPage2(html);
306+
307+
WebElement elem = driver.findElement(By.id("a1"));
308+
assertNull(elem.getAttribute("href"));
309+
310+
elem = driver.findElement(By.id("a2"));
311+
assertEquals(URL_FIRST.toExternalForm() + "test", elem.getAttribute("href"));
312+
313+
elem = driver.findElement(By.id("a3"));
314+
assertEquals("https://www.htmlunit.org/href", elem.getAttribute("href"));
315+
}
316+
317+
@Test
318+
public void src() throws Exception {
319+
final String html = "<html>\n"
320+
+ "<head>\n"
321+
+ "</head>\n"
322+
+ "<body>\n"
323+
+ " <input id='img1' type='image'>"
324+
+ " <input id='img2' type='image' src='test'>"
325+
+ " <input id='img3' type='image' src='https://www.htmlunit.org/src'>"
326+
+ "</body>\n"
327+
+ "</html>\n";
328+
329+
final WebDriver driver = loadPage2(html);
330+
331+
WebElement elem = driver.findElement(By.id("img1"));
332+
assertEquals("", elem.getAttribute("src"));
333+
334+
elem = driver.findElement(By.id("img2"));
335+
assertEquals(URL_FIRST.toExternalForm() + "test", elem.getAttribute("src"));
336+
337+
elem = driver.findElement(By.id("img3"));
338+
assertEquals("https://www.htmlunit.org/src", elem.getAttribute("src"));
339+
}
246340
}

src/test/java/org/openqa/selenium/htmlunit/by/FindByIdTest.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Test;
2323
import org.junit.runner.RunWith;
2424
import org.openqa.selenium.By;
25+
import org.openqa.selenium.NoSuchElementException;
2526
import org.openqa.selenium.WebDriver;
2627
import org.openqa.selenium.WebElement;
2728
import org.openqa.selenium.htmlunit.WebDriverTestCase;
@@ -50,6 +51,35 @@ public void elementById() throws Exception {
5051
assertEquals("TestDiv", element.getText());
5152
}
5253

54+
@Test(expected = NoSuchElementException.class)
55+
public void elementByIdNotFound() throws Exception {
56+
final String html = "<html>\n"
57+
+ "<head>\n"
58+
+ "</head>\n"
59+
+ "<body>\n"
60+
+ " <div id='testDivId'>TestDiv</div>\n"
61+
+ "</body>\n"
62+
+ "</html>\n";
63+
64+
final WebDriver driver = loadPage2(html);
65+
driver.findElement(By.id("unknown"));
66+
}
67+
68+
@Test
69+
public void elementsByIdNothingFound() throws Exception {
70+
final String html = "<html>\n"
71+
+ "<head>\n"
72+
+ "</head>\n"
73+
+ "<body>\n"
74+
+ " <div id='testDivId'>TestDiv</div>\n"
75+
+ "</body>\n"
76+
+ "</html>\n";
77+
78+
final WebDriver driver = loadPage2(html);
79+
final List<WebElement> elements = driver.findElements(By.id("unknown"));
80+
assertEquals(0, elements.size());
81+
}
82+
5383
@Test
5484
public void elementsById() throws Exception {
5585
final String html = "<html>\n"
@@ -76,6 +106,26 @@ public void relativeElementById() throws Exception {
76106
+ " <div id='testDivId'>TestDiv</div>\n"
77107
+ " <div id='testDivId2'>TestDiv2</div>\n"
78108
+ " </div>\n"
109+
+ " <div id='testDivId'>TestDiv Clone</div>\n"
110+
+ "</body>\n"
111+
+ "</html>\n";
112+
113+
final WebDriver driver = loadPage2(html);
114+
final WebElement ctx = driver.findElement(By.id("ctx"));
115+
final WebElement element = ctx.findElement(By.id("testDivId"));
116+
assertEquals("TestDiv", element.getText());
117+
}
118+
119+
@Test(expected = NoSuchElementException.class)
120+
public void relativeElementByIdNotFound() throws Exception {
121+
final String html = "<html>\n"
122+
+ "<head>\n"
123+
+ "</head>\n"
124+
+ "<body>\n"
125+
+ " <div id='ctx'>\n"
126+
+ " <div id='testDivId2'>TestDiv2</div>\n"
127+
+ " </div>\n"
128+
+ " <div id='testDivId'>TestDiv Clone</div>\n"
79129
+ "</body>\n"
80130
+ "</html>\n";
81131

@@ -92,16 +142,15 @@ public void relativeElementsById() throws Exception {
92142
+ "</head>\n"
93143
+ "<body>\n"
94144
+ " <div id='ctx'>\n"
95-
+ " <div id='testDivId'>TestDiv</div>\n"
96145
+ " <div id='testDivId2'>TestDiv2</div>\n"
97146
+ " </div>\n"
147+
+ " <div id='testDivId'>TestDiv</div>\n"
98148
+ "</body>\n"
99149
+ "</html>\n";
100150

101151
final WebDriver driver = loadPage2(html);
102152
final WebElement ctx = driver.findElement(By.id("ctx"));
103153
final List<WebElement> elements = ctx.findElements(By.id("testDivId"));
104-
assertEquals(1, elements.size());
105-
assertEquals("TestDiv", elements.get(0).getText());
154+
assertEquals(0, elements.size());
106155
}
107156
}

0 commit comments

Comments
 (0)