@throws Exception if the test fails
()
| 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 |
no test coverage detected