Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Nov 23, 2024
1 parent 736d2b8 commit 85ed8b1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
50 changes: 50 additions & 0 deletions src/test/java/org/htmlunit/html/HtmlPage3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,54 @@ public void onLoadHandler_idChange() throws Exception {

loadPageVerifyTitle2(html);
}

/**
* Tests getElementById() of child element after appendChild(), removeChild(), then appendChild()
* of the parent element.
*
* @throws Exception if the test fails
*/
@Test
@Alerts("[object HTMLTableRowElement]")
public void getElementById_AfterAppendRemoveAppendChild() throws Exception {
final String content = "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var table = document.createElement('table');\n"
+ " var tr = document.createElement('tr');\n"
+ " tr.id = 'myTR';\n"
+ " table.appendChild(tr);\n"
+ " document.body.appendChild(table);\n"
+ " document.body.removeChild(table);\n"
+ " document.body.appendChild(table);\n"
+ " log(document.getElementById('myTR'));\n"
+ " }\n"
+ "</script></head>\n"
+ "<body onload='test()'>\n"
+ "</body></html>";
loadPageVerifyTitle2(content);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts("null")
public void getElementById_AfterAppendingToNewlyCreatedElement() throws Exception {
final String content = "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var table = document.createElement('table');\n"
+ " var tr = document.createElement('tr');\n"
+ " tr.id = 'myTR';\n"
+ " table.appendChild(tr);\n"
+ " log(document.getElementById('myTR'));\n"
+ " }\n"
+ "</script></head>\n"
+ "<body onload='test()'>\n"
+ "</body></html>";
loadPageVerifyTitle2(content);
}
}
48 changes: 1 addition & 47 deletions src/test/java/org/htmlunit/html/HtmlPageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1748,52 +1748,6 @@ public void title_EmptyXmlTagExpanded() throws Exception {
assertTrue(page.asXml().indexOf("</title>") != -1);
}

/**
* Tests getElementById() of child element after appendChild(), removeChild(), then appendChild()
* of the parent element.
*
* @throws Exception if the test fails
*/
@Test
public void getElementById_AfterAppendRemoveAppendChild() throws Exception {
final String content = "<html><head><title>foo</title><script>\n"
+ " function test() {\n"
+ " var table = document.createElement('table');\n"
+ " var tr = document.createElement('tr');\n"
+ " tr.id = 'myTR';\n"
+ " table.appendChild(tr);\n"
+ " document.body.appendChild(table);\n"
+ " document.body.removeChild(table);\n"
+ " document.body.appendChild(table);\n"
+ " alert(document.getElementById('myTR'));\n"
+ " }\n"
+ "</script></head><body onload='test()'>\n"
+ "</body></html>";
final List<String> collectedAlerts = new ArrayList<>();
loadPage(content, collectedAlerts);
assertFalse("null".equals(collectedAlerts.get(0)));
}

/**
* @throws Exception if the test fails
*/
@Test
public void getElementById_AfterAppendingToNewlyCreatedElement() throws Exception {
final String content = "<html><head><title>foo</title><script>\n"
+ " function test() {\n"
+ " var table = document.createElement('table');\n"
+ " var tr = document.createElement('tr');\n"
+ " tr.id = 'myTR';\n"
+ " table.appendChild(tr);\n"
+ " alert(document.getElementById('myTR'));\n"
+ " }\n"
+ "</script></head><body onload='test()'>\n"
+ "</body></html>";
final List<String> collectedAlerts = new ArrayList<>();
loadPage(content, collectedAlerts);
assertTrue("null".equals(collectedAlerts.get(0)));
}

/**
* @throws Exception if the test fails
*/
Expand Down Expand Up @@ -1905,7 +1859,7 @@ public void asNormalizedText() throws Exception {
+ "</table></body></html>";

final HtmlPage page = loadPage(htmlContent);
page.asNormalizedText();
assertEquals("test\na", page.asNormalizedText());
}

/**
Expand Down

0 comments on commit 85ed8b1

Please sign in to comment.