Skip to content

Commit

Permalink
Replace the deprecated WebElement.getAttribute(String) with getDomAtt…
Browse files Browse the repository at this point in the history
…ribute(String) and getDomProperty(String) at many places in the test suite
  • Loading branch information
rbri committed Feb 9, 2025
1 parent 8d49d46 commit 378e7da
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 124 deletions.
22 changes: 16 additions & 6 deletions src/test/java/org/htmlunit/html/HtmlResetInput2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,19 @@ public void reset() throws Exception {
final WebDriver webDriver = loadPage2(html);

final WebElement textfield = webDriver.findElement(By.id("textfield"));
assertEquals(getExpectedAlerts()[0], textfield.getAttribute("value"));

assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
assertEquals(getExpectedAlerts()[0], textfield.getDomProperty("value"));

textfield.sendKeys("newValue");
assertEquals(getExpectedAlerts()[1], textfield.getAttribute("value"));
assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
assertEquals(getExpectedAlerts()[1], textfield.getDomProperty("value"));

final WebElement reset = webDriver.findElement(By.name("resetBtn"));
reset.click();
assertEquals(getExpectedAlerts()[2], textfield.getAttribute("value"));

assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
assertEquals(getExpectedAlerts()[2], textfield.getDomProperty("value"));
}

/**
Expand All @@ -291,13 +297,17 @@ public void onclickDisables() throws Exception {
final WebDriver webDriver = loadPage2(html);

final WebElement textfield = webDriver.findElement(By.id("textfield"));
assertEquals(getExpectedAlerts()[0], textfield.getAttribute("value"));
assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
assertEquals(getExpectedAlerts()[0], textfield.getDomProperty("value"));

textfield.sendKeys("newValue");
assertEquals(getExpectedAlerts()[1], textfield.getAttribute("value"));
assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
assertEquals(getExpectedAlerts()[1], textfield.getDomProperty("value"));

final WebElement reset = webDriver.findElement(By.name("resetBtn"));
reset.click();
assertEquals(getExpectedAlerts()[2], textfield.getAttribute("value"));
assertEquals(getExpectedAlerts()[0], textfield.getDomAttribute("value"));
assertEquals(getExpectedAlerts()[2], textfield.getDomProperty("value"));
}

/**
Expand Down
23 changes: 11 additions & 12 deletions src/test/java/org/htmlunit/javascript/host/dom/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2581,25 +2581,24 @@ public void createStyleSheet() throws Exception {
* @throws Exception if the test fails
*/
@Test
@Alerts("#document-fragment_null_11_null_0_")
@Alerts({"#document-fragment", "null", "11", "null", "0"})
public void createDocumentFragment() throws Exception {
final String html = "<html><head><title>foo</title><script>\n"
final String html = "<html><head>\n"
+ "<title>foo</title><script>\n"
+ LOG_TEXTAREA_FUNCTION
+ " function test() {\n"
+ " var fragment = document.createDocumentFragment();\n"
+ " var textarea = document.getElementById('myTextarea');\n"
+ " textarea.value += fragment.nodeName + '_';\n"
+ " textarea.value += fragment.nodeValue + '_';\n"
+ " textarea.value += fragment.nodeType + '_';\n"
+ " textarea.value += fragment.parentNode + '_';\n"
+ " textarea.value += fragment.childNodes.length + '_';\n"
+ " log(fragment.nodeName);\n"
+ " log(fragment.nodeValue);\n"
+ " log(fragment.nodeType);\n"
+ " log(fragment.parentNode);\n"
+ " log(fragment.childNodes.length);\n"
+ " }\n"
+ "</script></head><body onload='test()'>\n"
+ "<textarea id='myTextarea' cols='40'></textarea>\n"
+ LOG_TEXTAREA
+ "</body></html>";

final WebDriver driver = loadPage2(html);
final String expected = getExpectedAlerts()[0];
assertEquals(expected, driver.findElement(By.id("myTextarea")).getAttribute("value"));
loadPageVerifyTextArea2(html);
}

/**
Expand Down
41 changes: 16 additions & 25 deletions src/test/java/org/htmlunit/javascript/host/dom/EventNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,15 @@ public void fireEvent_initFromTemplate() throws Exception {
* @throws Exception if the test fails
*/
@Test
@Alerts("mousedown span,mouseup span,click span,mousedown text,focus text,mouseup text,"
+ "click text,mousedown image,focus image,mouseup image,click image,mousedown textarea,focus textarea,"
+ "mouseup textarea,click textarea,")
@Alerts({"mousedown span", "mouseup span", "click span",
"mousedown text", "focus text", "mouseup text", "click text",
"mousedown image", "focus image", "mouseup image", "click image",
"mousedown textarea", "focus textarea", "mouseup textarea", "click textarea"})
public void clickEvents() throws Exception {
final String html = "<html>\n"
+ "<head>\n"
+ " <script>\n"
+ " function log(text) {\n"
+ " var textarea = document.getElementById('myTextarea');\n"
+ " textarea.value += text + ',';\n"
+ " }\n"
+ LOG_TEXTAREA_FUNCTION
+ " </script>\n"
+ "</head><body>\n"
+ " <span id='testSpan' onfocus=\"log('will not be triggered')\" onmousedown=\"log('mousedown span')\""
Expand All @@ -112,31 +110,28 @@ public void clickEvents() throws Exception {
+ " onmousedown=\"log('mousedown textarea')\" onclick=\"log('click textarea')\""
+ " onmouseup=\"log('mouseup textarea')\" onfocus=\"log('focus textarea')\"></textarea>\n"
+ " </form>\n"
+ " <textarea id='myTextarea' cols='80' rows='10'></textarea>\n"
+ LOG_TEXTAREA
+ "</body></html>";

final WebDriver driver = loadPage2(html);
driver.findElement(By.id("testSpan")).click();
driver.findElement(By.id("testInput")).click();
driver.findElement(By.id("testImage")).click();
driver.findElement(By.id("testTextarea")).click();
final String expected = getExpectedAlerts()[0];
assertEquals(expected, driver.findElement(By.id("myTextarea")).getAttribute("value"));

verifyTextArea2(driver, getExpectedAlerts());
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts("mousedown label,mouseup label,click label,focus text,click text,")
@Alerts({"mousedown label", "mouseup label", "click label", "focus text", "click text"})
public void clickEventsLabel() throws Exception {
final String html = "<html>\n"
+ "<head>\n"
+ " <script>\n"
+ " function log(text) {\n"
+ " var textarea = document.getElementById('myTextarea');\n"
+ " textarea.value += text + ',';\n"
+ " }\n"
+ LOG_TEXTAREA_FUNCTION
+ " </script>\n"
+ "</head><body>\n"
+ " <label id='testLabel' for='testInput'"
Expand All @@ -146,37 +141,34 @@ public void clickEventsLabel() throws Exception {
+ " <input type='text' id='testInput' onmousedown=\"log('mousedown text')\""
+ " onclick=\"log('click text')\" onmouseup=\"log('mouseup text')\" onfocus=\"log('focus text')\">\n"
+ " </form>\n"
+ " <textarea id='myTextarea' cols='80' rows='10'></textarea>\n"
+ LOG_TEXTAREA
+ "</body></html>";

final WebDriver driver = loadPage2(html);
driver.findElement(By.id("testLabel")).click();

final String expected = getExpectedAlerts()[0];
assertEquals(expected, driver.findElement(By.id("myTextarea")).getAttribute("value"));
verifyTextArea2(driver, getExpectedAlerts());
}

/**
* Test event order.
* @throws Exception if the test fails
*/
@Test
@Alerts({"focus", "keydown", "keypress", "keyup", "change", "blur"})
public void eventOrder() throws Exception {
final String html = "<html>\n"
+ "<head>\n"
+ " <script>\n"
+ " function log(text) {\n"
+ " var textarea = document.getElementById('myTextarea');\n"
+ " textarea.value += text + ',';\n"
+ " }\n"
+ LOG_TEXTAREA_FUNCTION
+ " </script>\n"
+ "</head><body>\n"
+ "<form>\n"
+ " <input name='foo' id='foo' onfocus=\"log('focus')\" onblur=\"log('blur')\" onchange=\"log('change')\""
+ " onkeydown=\"log('keydown')\" onkeypress=\"log('keypress')\" onkeyup=\"log('keyup')\">\n"
+ " <input name='other' id='other'>\n"
+ "</form>\n"
+ " <textarea id='myTextarea' cols='80'></textarea>\n"
+ LOG_TEXTAREA
+ "</body></html>";

final WebDriver webDriver = loadPage2(html);
Expand All @@ -185,7 +177,6 @@ public void eventOrder() throws Exception {
textField.sendKeys("a");
webDriver.findElement(By.id("other")).click();

final String expected = "focus,keydown,keypress,keyup,change,blur,";
assertEquals(expected, webDriver.findElement(By.id("myTextarea")).getAttribute("value"));
verifyTextArea2(webDriver, getExpectedAlerts());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,18 +394,20 @@ public void attributeValueAddRemove() throws Exception {
*/
@Test
@Alerts("[object HTMLHeadingElement]-attributes")
@BuggyWebDriver(FF = "[object HTMLInputElement]-attributes\n"
+ "[object HTMLInputElement]-attributes\n"
+ "[object HTMLInputElement]-attributes\n"
+ "[object HTMLInputElement]-attributes\n"
+ "[object HTMLHeadingElement]-attributes",
FF_ESR = "[object HTMLInputElement]-attributes\n"
+ "[object HTMLInputElement]-attributes\n"
+ "[object HTMLInputElement]-attributes\n"
+ "[object HTMLInputElement]-attributes\n"
+ "[object HTMLHeadingElement]-attributes")
@BuggyWebDriver(
FF = {"[object HTMLInputElement]-attributesn",
"[object HTMLInputElement]-attributes",
"[object HTMLInputElement]-attributes",
"[object HTMLInputElement]-attributes",
"[object HTMLHeadingElement]-attributes"},
FF_ESR = {"[object HTMLInputElement]-attributes",
"[object HTMLInputElement]-attributes",
"[object HTMLInputElement]-attributes",
"[object HTMLInputElement]-attributes",
"[object HTMLHeadingElement]-attributes"})
public void attributeValue2() throws Exception {
final String html = "<html><head><script>\n"
+ LOG_TEXTAREA_FUNCTION
+ " function makeRed() {\n"
+ " document.getElementById('headline').setAttribute('style', 'color: red');\n"
+ " }\n"
Expand All @@ -414,10 +416,6 @@ public void attributeValue2() throws Exception {
+ " log(mutation.target + '-' + mutation.type);\n"
+ " }\n"

+ " function log(x) {\n"
+ " document.getElementById('log').value += x + '\\n';\n"
+ " }\n"

+ " function test() {\n"
+ " var mobs = new MutationObserver(function(mutations) {\n"
+ " mutations.forEach(print)\n"
Expand All @@ -439,13 +437,12 @@ public void attributeValue2() throws Exception {
+ " <h1 id='headline' style='font-style: italic'>Some headline</h1>\n"
+ " <input id='id1' type='button' onclick='makeRed()' value='Make Red'>\n"
+ " </div>\n"
+ " <textarea id='log' cols='80' rows='40'></textarea>\n"
+ LOG_TEXTAREA
+ "</body></html>\n";
final WebDriver driver = loadPage2(html);
driver.findElement(By.id("id1")).click();

final String text = driver.findElement(By.id("log")).getAttribute("value").trim().replaceAll("\r", "");
assertEquals(String.join("\n", getExpectedAlerts()), text);
verifyTextArea2(driver, getExpectedAlerts());
}

/**
Expand Down
Loading

0 comments on commit 378e7da

Please sign in to comment.