Skip to content

Commit 686836f

Browse files
committed
some getDomProperty fixes
1 parent 4795cc4 commit 686836f

4 files changed

Lines changed: 138 additions & 3 deletions

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.concurrent.Callable;
2828

2929
import org.htmlunit.ScriptResult;
30+
import org.htmlunit.corejs.javascript.ScriptRuntime;
3031
import org.htmlunit.corejs.javascript.Scriptable;
3132
import org.htmlunit.corejs.javascript.ScriptableObject;
3233
import org.htmlunit.html.DisabledElement;
@@ -46,6 +47,7 @@
4647
import org.htmlunit.html.HtmlSubmitInput;
4748
import org.htmlunit.html.HtmlTextArea;
4849
import org.htmlunit.html.impl.SelectableTextInput;
50+
import org.htmlunit.javascript.HtmlUnitScriptable;
4951
import org.htmlunit.javascript.host.html.HTMLElement;
5052
import org.htmlunit.javascript.host.html.HTMLInputElement;
5153
import org.openqa.selenium.By;
@@ -384,8 +386,16 @@ public String getDomProperty(final String name) {
384386
assertElementNotStale();
385387

386388
final String lowerName = name.toLowerCase();
387-
final String value = element_.getAttribute(lowerName);
388389

390+
final HtmlUnitScriptable scriptable = element_.getScriptableObject();
391+
if (scriptable != null) {
392+
if (!ScriptableObject.hasProperty(scriptable, lowerName)) {
393+
return null;
394+
}
395+
return ScriptRuntime.toCharSequence(ScriptableObject.getProperty(scriptable, lowerName)).toString();
396+
}
397+
398+
// js disabled, fallback to some hacks
389399
if ("disabled".equals(lowerName)) {
390400
if (element_ instanceof DisabledElement) {
391401
return trueOrFalse(((DisabledElement) element_).isDisabled());
@@ -401,6 +411,7 @@ else if (element_ instanceof HtmlRadioButtonInput) {
401411
}
402412
}
403413

414+
final String value = element_.getAttribute(lowerName);
404415
if (ATTRIBUTE_NOT_DEFINED == value) {
405416
return null;
406417
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,46 @@ public void radioClicked() throws Exception {
201201
elem.click();
202202
assertEquals("true", elem.getAttribute("checked"));
203203
}
204+
205+
@Test
206+
public void inputTextValue() throws Exception {
207+
final String html = "<html>\n"
208+
+ "<head>\n"
209+
+ "</head>\n"
210+
+ "<body>\n"
211+
+ " <fieldset>\n"
212+
+ " <input type='text' id='textBx' name='text' value='world'/>\n"
213+
+ " <input type='text' id='textBx2' name='text'/>\n"
214+
+ " </fieldset>\n"
215+
+ "</body>\n"
216+
+ "</html>\n";
217+
218+
final WebDriver driver = loadPage2(html);
219+
220+
WebElement elem = driver.findElement(By.id("textBx"));
221+
assertEquals("world", elem.getAttribute("value"));
222+
223+
elem = driver.findElement(By.id("textBx2"));
224+
assertEquals("", elem.getAttribute("value"));
225+
}
226+
227+
@Test
228+
public void inputTextValueTyped() throws Exception {
229+
final String html = "<html>\n"
230+
+ "<head>\n"
231+
+ "</head>\n"
232+
+ "<body>\n"
233+
+ " <fieldset>\n"
234+
+ " <input type='text' id='textBx' name='text' value='world'/>\n"
235+
+ " </fieldset>\n"
236+
+ "</body>\n"
237+
+ "</html>\n";
238+
239+
final WebDriver driver = loadPage2(html);
240+
241+
final WebElement elem = driver.findElement(By.id("textBx"));
242+
assertEquals("world", elem.getAttribute("value"));
243+
elem.sendKeys("hello");
244+
assertEquals("worldhello", elem.getAttribute("value"));
245+
}
204246
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,46 @@ public void radioClicked() throws Exception {
198198
elem.click();
199199
assertNull(elem.getDomAttribute("checked"));
200200
}
201+
202+
@Test
203+
public void inputTextValue() throws Exception {
204+
final String html = "<html>\n"
205+
+ "<head>\n"
206+
+ "</head>\n"
207+
+ "<body>\n"
208+
+ " <fieldset>\n"
209+
+ " <input type='text' id='textBx' name='text' value='world'/>\n"
210+
+ " <input type='text' id='textBx2' name='text'/>\n"
211+
+ " </fieldset>\n"
212+
+ "</body>\n"
213+
+ "</html>\n";
214+
215+
final WebDriver driver = loadPage2(html);
216+
217+
WebElement elem = driver.findElement(By.id("textBx"));
218+
assertEquals("world", elem.getDomAttribute("value"));
219+
220+
elem = driver.findElement(By.id("textBx2"));
221+
assertNull(elem.getDomAttribute("value"));
222+
}
223+
224+
@Test
225+
public void inputTextValueTyped() throws Exception {
226+
final String html = "<html>\n"
227+
+ "<head>\n"
228+
+ "</head>\n"
229+
+ "<body>\n"
230+
+ " <fieldset>\n"
231+
+ " <input type='text' id='textBx' name='text' value='world'/>\n"
232+
+ " </fieldset>\n"
233+
+ "</body>\n"
234+
+ "</html>\n";
235+
236+
final WebDriver driver = loadPage2(html);
237+
238+
final WebElement elem = driver.findElement(By.id("textBx"));
239+
assertEquals("world", elem.getDomAttribute("value"));
240+
elem.sendKeys("hello");
241+
assertEquals("world", elem.getDomAttribute("value"));
242+
}
201243
}

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.openqa.selenium.WebElement;
2525
import org.openqa.selenium.htmlunit.junit.BrowserRunner;
2626
import org.openqa.selenium.htmlunit.junit.BrowserRunner.Alerts;
27-
import org.openqa.selenium.htmlunit.junit.BrowserRunner.HtmlUnitNYI;
2827

2928
/**
3029
* Separate test class for the HtmlUnitWebElement.getDomProperty(String) method.
@@ -55,7 +54,6 @@ public void domProperty() throws Exception {
5554
@Test
5655
@Alerts(DEFAULT = "null",
5756
IE = "true")
58-
@HtmlUnitNYI(IE = "null")
5957
public void unsupportedAttribute() throws Exception {
6058
final String html = "<html>\n"
6159
+ "<head>\n"
@@ -178,4 +176,46 @@ public void radioClicked() throws Exception {
178176
elem.click();
179177
assertEquals("true", elem.getDomProperty("checked"));
180178
}
179+
180+
@Test
181+
public void inputTextValue() throws Exception {
182+
final String html = "<html>\n"
183+
+ "<head>\n"
184+
+ "</head>\n"
185+
+ "<body>\n"
186+
+ " <fieldset>\n"
187+
+ " <input type='text' id='textBx' name='text' value='world'/>\n"
188+
+ " <input type='text' id='textBx2' name='text'/>\n"
189+
+ " </fieldset>\n"
190+
+ "</body>\n"
191+
+ "</html>\n";
192+
193+
final WebDriver driver = loadPage2(html);
194+
195+
WebElement elem = driver.findElement(By.id("textBx"));
196+
assertEquals("world", elem.getDomProperty("value"));
197+
198+
elem = driver.findElement(By.id("textBx2"));
199+
assertEquals("", elem.getDomProperty("value"));
200+
}
201+
202+
@Test
203+
public void inputTextValueTyped() throws Exception {
204+
final String html = "<html>\n"
205+
+ "<head>\n"
206+
+ "</head>\n"
207+
+ "<body>\n"
208+
+ " <fieldset>\n"
209+
+ " <input type='text' id='textBx' name='text' value='world'/>\n"
210+
+ " </fieldset>\n"
211+
+ "</body>\n"
212+
+ "</html>\n";
213+
214+
final WebDriver driver = loadPage2(html);
215+
216+
final WebElement elem = driver.findElement(By.id("textBx"));
217+
assertEquals("world", elem.getDomProperty("value"));
218+
elem.sendKeys("hello");
219+
assertEquals("worldhello", elem.getDomProperty("value"));
220+
}
181221
}

0 commit comments

Comments
 (0)