MCPcopy Create free account
hub / github.com/HtmlUnit/htmlunit / replace

Method replace

src/test/java/org/htmlunit/html/DomNodeTest.java:159–194  ·  view source on GitHub ↗

@throws Exception if the test fails

()

Source from the content-addressed store, hash-verified

157 * @throws Exception if the test fails
158 */
159 @Test
160 public void replace() throws Exception {
161 final String content = DOCTYPE_HTML
162 + "<html><head></head><body>\n"
163 + "<br><div id='tag'></div><br><div id='tag2'/></body></html>";
164 final HtmlPage page = loadPage(content);
165
166 final DomNode node = page.getElementById("tag");
167
168 final DomNode previousSibling = node.getPreviousSibling();
169 final DomNode nextSibling = node.getNextSibling();
170 final DomNode parent = node.getParentNode();
171
172 // position among parent's children
173 final int position = readPositionAmongParentChildren(node);
174
175 final DomNode newNode = new DomText(page, "test");
176 node.replace(newNode);
177 assertSame("previous sibling", previousSibling, newNode.getPreviousSibling());
178 assertSame("next sibling", nextSibling, newNode.getNextSibling());
179 assertSame("parent", parent, newNode.getParentNode());
180 assertSame(newNode, previousSibling.getNextSibling());
181 assertSame(newNode, nextSibling.getPreviousSibling());
182 assertEquals(position, readPositionAmongParentChildren(newNode));
183
184 final AttributesImpl attributes = new AttributesImpl();
185 attributes.addAttribute(null, "id", "id", null, "tag2"); // with the same id as the node to replace
186 final DomNode node2 = page.getHtmlElementById("tag2");
187 assertEquals("div", node2.getNodeName());
188
189 final DomNode node3 = page.getWebClient().getPageCreator().getHtmlParser()
190 .getFactory(HtmlSpan.TAG_NAME)
191 .createElement(page, HtmlSpan.TAG_NAME, attributes);
192 node2.replace(node3);
193 assertEquals("span", page.getHtmlElementById("tag2").getTagName());
194 }
195
196 /**
197 * @throws Exception if the test fails

Callers 15

checkLinesMethod · 0.45
loadFilePageEncodedMethod · 0.45
loadContentMethod · 0.45
doGetMethod · 0.45
runWithMethod · 0.45
testTestsMethod · 0.45
processMethod · 0.45
getDescriptionMethod · 0.45
saveMethod · 0.45
assertEdgeDriverMethod · 0.45
assertVersionMethod · 0.45

Calls 15

getElementByIdMethod · 0.95
getPreviousSiblingMethod · 0.95
getNextSiblingMethod · 0.95
getParentNodeMethod · 0.95
replaceMethod · 0.95
getHtmlElementByIdMethod · 0.95
assertSameMethod · 0.80
getPageCreatorMethod · 0.80
createElementMethod · 0.65
getFactoryMethod · 0.65
getHtmlParserMethod · 0.65

Tested by

no test coverage detected