Skip to content

Commit 979a5a8

Browse files
committed
fix checkd attribute for radio and checkbox, more tests added
1 parent f3b6107 commit 979a5a8

5 files changed

Lines changed: 328 additions & 29 deletions

File tree

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>org.seleniumhq.selenium</groupId>
77
<artifactId>htmlunit3-driver</artifactId>
8-
<version>4.8.1</version>
8+
<version>4.8.3-SNAPSHOT</version>
99

1010
<name>htmlunit-driver</name>
1111
<description>WebDriver compatible driver for HtmlUnit headless browser</description>
@@ -18,8 +18,8 @@
1818
<maven.compiler.source>8</maven.compiler.source>
1919
<maven.compiler.target>8</maven.compiler.target>
2020

21-
<selenium.version>4.8.1</selenium.version>
22-
<htmlunit.version>3.0.0</htmlunit.version>
21+
<selenium.version>4.8.3</selenium.version>
22+
<htmlunit.version>3.1.0-SNAPSHOT</htmlunit.version>
2323

2424
<jetty.version>9.4.51.v20230217</jetty.version>
2525

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

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@
2626
import java.util.List;
2727
import java.util.concurrent.Callable;
2828

29+
import org.htmlunit.ScriptResult;
30+
import org.htmlunit.corejs.javascript.Scriptable;
31+
import org.htmlunit.corejs.javascript.ScriptableObject;
32+
import org.htmlunit.html.DisabledElement;
33+
import org.htmlunit.html.DomElement;
34+
import org.htmlunit.html.DomNode;
35+
import org.htmlunit.html.HtmlButton;
36+
import org.htmlunit.html.HtmlCheckBoxInput;
37+
import org.htmlunit.html.HtmlElement;
38+
import org.htmlunit.html.HtmlFileInput;
39+
import org.htmlunit.html.HtmlForm;
40+
import org.htmlunit.html.HtmlImageInput;
41+
import org.htmlunit.html.HtmlInput;
42+
import org.htmlunit.html.HtmlOption;
43+
import org.htmlunit.html.HtmlPage;
44+
import org.htmlunit.html.HtmlRadioButtonInput;
45+
import org.htmlunit.html.HtmlSelect;
46+
import org.htmlunit.html.HtmlSubmitInput;
47+
import org.htmlunit.html.HtmlTextArea;
48+
import org.htmlunit.html.impl.SelectableTextInput;
49+
import org.htmlunit.javascript.host.html.HTMLElement;
50+
import org.htmlunit.javascript.host.html.HTMLInputElement;
2951
import org.openqa.selenium.By;
3052
import org.openqa.selenium.Dimension;
3153
import org.openqa.selenium.ElementNotInteractableException;
@@ -47,28 +69,6 @@
4769
import org.w3c.dom.Attr;
4870
import org.w3c.dom.NamedNodeMap;
4971

50-
import org.htmlunit.ScriptResult;
51-
import org.htmlunit.html.DisabledElement;
52-
import org.htmlunit.html.DomElement;
53-
import org.htmlunit.html.DomNode;
54-
import org.htmlunit.html.HtmlButton;
55-
import org.htmlunit.html.HtmlElement;
56-
import org.htmlunit.html.HtmlFileInput;
57-
import org.htmlunit.html.HtmlForm;
58-
import org.htmlunit.html.HtmlImageInput;
59-
import org.htmlunit.html.HtmlInput;
60-
import org.htmlunit.html.HtmlOption;
61-
import org.htmlunit.html.HtmlPage;
62-
import org.htmlunit.html.HtmlSelect;
63-
import org.htmlunit.html.HtmlSubmitInput;
64-
import org.htmlunit.html.HtmlTextArea;
65-
import org.htmlunit.html.impl.SelectableTextInput;
66-
import org.htmlunit.javascript.host.html.HTMLElement;
67-
import org.htmlunit.javascript.host.html.HTMLInputElement;
68-
69-
import org.htmlunit.corejs.javascript.Scriptable;
70-
import org.htmlunit.corejs.javascript.ScriptableObject;
71-
7272
/**
7373
*
7474
* @author Alexei Barantsev
@@ -385,16 +385,26 @@ public String getDomProperty(final String name) {
385385

386386
final String lowerName = name.toLowerCase();
387387
final String value = element_.getAttribute(lowerName);
388-
if (ATTRIBUTE_NOT_DEFINED == value) {
389-
return null;
390-
}
391388

392389
if ("disabled".equals(lowerName)) {
393390
if (element_ instanceof DisabledElement) {
394-
return trueOrNull(((DisabledElement) element_).isDisabled());
391+
return trueOrFalse(((DisabledElement) element_).isDisabled());
392+
}
393+
}
394+
395+
if ("checked".equals(lowerName)) {
396+
if (element_ instanceof HtmlCheckBoxInput) {
397+
return trueOrFalse(((HtmlCheckBoxInput) element_).isChecked());
398+
}
399+
else if (element_ instanceof HtmlRadioButtonInput) {
400+
return trueOrFalse(((HtmlRadioButtonInput) element_).isChecked());
395401
}
396402
}
397403

404+
if (ATTRIBUTE_NOT_DEFINED == value) {
405+
return null;
406+
}
407+
398408
if (ATTRIBUTE_VALUE_EMPTY == value) {
399409
return null;
400410
}
@@ -418,13 +428,26 @@ public String getDomAttribute(final String name) {
418428
}
419429
}
420430

431+
if ("checked".equals(lowerName)) {
432+
if (element_ instanceof HtmlCheckBoxInput) {
433+
return trueOrNull(((HtmlCheckBoxInput) element_).isChecked());
434+
}
435+
else if (element_ instanceof HtmlRadioButtonInput) {
436+
return trueOrNull(((HtmlRadioButtonInput) element_).isChecked());
437+
}
438+
}
439+
421440
return value;
422441
}
423442

424443
private static String trueOrNull(final boolean condition) {
425444
return condition ? "true" : null;
426445
}
427446

447+
private static String trueOrFalse(final boolean condition) {
448+
return condition ? "true" : "false";
449+
}
450+
428451
@Override
429452
public boolean isSelected() {
430453
assertElementNotStale();

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,96 @@ public void valueOption() throws Exception {
109109
elem = driver.findElement(By.id("o3"));
110110
assertEquals("option three second line", elem.getAttribute("value"));
111111
}
112+
113+
@Test
114+
public void checkbox() throws Exception {
115+
final String html = "<html>\n"
116+
+ "<head>\n"
117+
+ "</head>\n"
118+
+ "<body>\n"
119+
+ " <fieldset>\n"
120+
+ " <input type='checkbox' id='chkBx' name='chbox' value='dis' checked='checked' />checked"
121+
+ " <input type='checkbox' id='chkBx2' name='chbox2' value='dis'/> not checked"
122+
+ " <input type='checkbox' id='chkBx3' name='chbox3' value='dis' checked='false' />\n"
123+
+ " </fieldset>\n"
124+
+ "</body>\n"
125+
+ "</html>\n";
126+
127+
final WebDriver driver = loadPage2(html);
128+
129+
WebElement elem = driver.findElement(By.id("chkBx"));
130+
assertEquals("true", elem.getAttribute("checked"));
131+
132+
elem = driver.findElement(By.id("chkBx2"));
133+
assertNull(elem.getAttribute("checked"));
134+
135+
elem = driver.findElement(By.id("chkBx3"));
136+
assertEquals("true", elem.getAttribute("checked"));
137+
}
138+
139+
@Test
140+
public void checkboxClicked() throws Exception {
141+
final String html = "<html>\n"
142+
+ "<head>\n"
143+
+ "</head>\n"
144+
+ "<body>\n"
145+
+ " <fieldset>\n"
146+
+ " <input type='checkbox' id='chkBx' name='chbox' value='dis' />\n"
147+
+ " </fieldset>\n"
148+
+ "</body>\n"
149+
+ "</html>\n";
150+
151+
final WebDriver driver = loadPage2(html);
152+
153+
final WebElement elem = driver.findElement(By.id("chkBx"));
154+
assertNull(elem.getAttribute("checked"));
155+
elem.click();
156+
assertEquals("true", elem.getAttribute("checked"));
157+
}
158+
159+
@Test
160+
public void radio() throws Exception {
161+
final String html = "<html>\n"
162+
+ "<head>\n"
163+
+ "</head>\n"
164+
+ "<body>\n"
165+
+ " <fieldset>\n"
166+
+ " <input type='radio' id='radioBx' name='radio' value='dis' checked='checked' />checked"
167+
+ " <input type='radio' id='radioBx2' name='radio2' value='dis'/> not checked"
168+
+ " <input type='radio' id='radioBx3' name='radio3' value='dis' checked='false' />\n"
169+
+ " </fieldset>\n"
170+
+ "</body>\n"
171+
+ "</html>\n";
172+
173+
final WebDriver driver = loadPage2(html);
174+
175+
WebElement elem = driver.findElement(By.id("radioBx"));
176+
assertEquals("true", elem.getAttribute("checked"));
177+
178+
elem = driver.findElement(By.id("radioBx2"));
179+
assertNull(elem.getAttribute("checked"));
180+
181+
elem = driver.findElement(By.id("radioBx3"));
182+
assertEquals("true", elem.getAttribute("checked"));
183+
}
184+
185+
@Test
186+
public void radioClicked() throws Exception {
187+
final String html = "<html>\n"
188+
+ "<head>\n"
189+
+ "</head>\n"
190+
+ "<body>\n"
191+
+ " <fieldset>\n"
192+
+ " <input type='radio' id='radioBx' name='radio' value='dis' />\n"
193+
+ " </fieldset>\n"
194+
+ "</body>\n"
195+
+ "</html>\n";
196+
197+
final WebDriver driver = loadPage2(html);
198+
199+
final WebElement elem = driver.findElement(By.id("radioBx"));
200+
assertNull(elem.getAttribute("checked"));
201+
elem.click();
202+
assertEquals("true", elem.getAttribute("checked"));
203+
}
112204
}

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,96 @@ public void notDisabled() throws Exception {
106106
final WebElement elem = driver.findElement(By.id("chkBx"));
107107
assertNull(elem.getDomAttribute("disabled"));
108108
}
109+
110+
@Test
111+
public void checkbox() throws Exception {
112+
final String html = "<html>\n"
113+
+ "<head>\n"
114+
+ "</head>\n"
115+
+ "<body>\n"
116+
+ " <fieldset>\n"
117+
+ " <input type='checkbox' id='chkBx' name='chbox' value='dis' checked='checked' />checked"
118+
+ " <input type='checkbox' id='chkBx2' name='chbox2' value='dis'/> not checked"
119+
+ " <input type='checkbox' id='chkBx3' name='chbox3' value='dis' checked='false' />\n"
120+
+ " </fieldset>\n"
121+
+ "</body>\n"
122+
+ "</html>\n";
123+
124+
final WebDriver driver = loadPage2(html);
125+
126+
WebElement elem = driver.findElement(By.id("chkBx"));
127+
assertEquals("true", elem.getDomAttribute("checked"));
128+
129+
elem = driver.findElement(By.id("chkBx2"));
130+
assertNull(elem.getDomAttribute("checked"));
131+
132+
elem = driver.findElement(By.id("chkBx3"));
133+
assertEquals("true", elem.getDomAttribute("checked"));
134+
}
135+
136+
@Test
137+
public void checkboxClicked() throws Exception {
138+
final String html = "<html>\n"
139+
+ "<head>\n"
140+
+ "</head>\n"
141+
+ "<body>\n"
142+
+ " <fieldset>\n"
143+
+ " <input type='checkbox' id='chkBx' name='chbox' value='dis' />\n"
144+
+ " </fieldset>\n"
145+
+ "</body>\n"
146+
+ "</html>\n";
147+
148+
final WebDriver driver = loadPage2(html);
149+
150+
final WebElement elem = driver.findElement(By.id("chkBx"));
151+
assertNull(elem.getDomAttribute("checked"));
152+
elem.click();
153+
assertNull(elem.getDomAttribute("checked"));
154+
}
155+
156+
@Test
157+
public void radio() throws Exception {
158+
final String html = "<html>\n"
159+
+ "<head>\n"
160+
+ "</head>\n"
161+
+ "<body>\n"
162+
+ " <fieldset>\n"
163+
+ " <input type='radio' id='radioBx' name='radio' value='dis' checked='checked' />checked"
164+
+ " <input type='radio' id='radioBx2' name='radio2' value='dis'/> not checked"
165+
+ " <input type='radio' id='radioBx3' name='radio3' value='dis' checked='false' />\n"
166+
+ " </fieldset>\n"
167+
+ "</body>\n"
168+
+ "</html>\n";
169+
170+
final WebDriver driver = loadPage2(html);
171+
172+
WebElement elem = driver.findElement(By.id("radioBx"));
173+
assertEquals("true", elem.getDomAttribute("checked"));
174+
175+
elem = driver.findElement(By.id("radioBx2"));
176+
assertNull(elem.getDomAttribute("checked"));
177+
178+
elem = driver.findElement(By.id("radioBx3"));
179+
assertEquals("true", elem.getDomAttribute("checked"));
180+
}
181+
182+
@Test
183+
public void radioClicked() throws Exception {
184+
final String html = "<html>\n"
185+
+ "<head>\n"
186+
+ "</head>\n"
187+
+ "<body>\n"
188+
+ " <fieldset>\n"
189+
+ " <input type='radio' id='radioBx' name='radio' value='dis' />\n"
190+
+ " </fieldset>\n"
191+
+ "</body>\n"
192+
+ "</html>\n";
193+
194+
final WebDriver driver = loadPage2(html);
195+
196+
final WebElement elem = driver.findElement(By.id("radioBx"));
197+
assertNull(elem.getDomAttribute("checked"));
198+
elem.click();
199+
assertNull(elem.getDomAttribute("checked"));
200+
}
109201
}

0 commit comments

Comments
 (0)