1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import com.gargoylesoftware.htmlunit.WebTestCase; 41 42 48 public class HtmlElementTest extends WebTestCase { 49 54 public HtmlElementTest( final String name ) { 55 super( name ); 56 } 57 58 59 62 public void testGetEnclosingForm() throws Exception { 63 final String htmlContent = "" 64 + "<html><head><title>foo</title></head><body>" 65 + "<form id='form1'>" 66 + "<table><tr><td><input type='text' id='foo'/></td></tr></table>" 67 + "</form></body></html>"; 68 final HtmlPage page = loadPage(htmlContent); 69 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 70 71 final HtmlInput input = (HtmlInput) form.getHtmlElementById("foo"); 72 assertSame(form, input.getEnclosingForm()); 73 } 74 75 78 public void testGetEnclosing() throws Exception { 79 final String htmlContent = "" 80 + "<html><head><title>foo</title></head><body>" 81 + "<form id='form1'>" 82 + "<table id='table1'>" 83 + "<tr id='tr1'><td id='td1'>foo</td></tr>" 84 + "<tr id='tr2'><td id='td2'>foo</td></tr>" 85 + "</table>" 86 + "</form></body></html>"; 87 final HtmlPage page = loadPage(htmlContent); 88 89 final HtmlElement td1 = page.getHtmlElementById("td1"); 90 assertEquals("tr1", td1.getEnclosingElement("tr").getId()); 91 assertEquals("tr1", td1.getEnclosingElement("TR").getId()); 92 assertEquals("table1", td1.getEnclosingElement("table").getId()); 93 assertEquals("form1", td1.getEnclosingElement("form").getId()); 94 95 final HtmlElement td2 = page.getHtmlElementById("td2"); 96 assertEquals("tr2", td2.getEnclosingElement("tr").getId()); 97 assertEquals("tr2", td2.getEnclosingElement("TR").getId()); 98 assertEquals("table1", td2.getEnclosingElement("table").getId()); 99 assertEquals("form1", td2.getEnclosingElement("form").getId()); 100 } 101 102 105 public void testAsText_WithComments() throws Exception { 106 final String htmlContent 107 = "<html><head><title>foo</title></head><body>" 108 + "<p id='p1'>foo<!--bar--></p>" 109 + "</body></html>"; 110 final HtmlPage page = loadPage(htmlContent); 111 final HtmlElement element = page.getHtmlElementById("p1"); 112 assertEquals("foo", element.asText()); 113 } 114 115 116 118 public void testConstants() { 119 assertEquals( "", HtmlElement.ATTRIBUTE_NOT_DEFINED ); 120 assertEquals( "", HtmlElement.ATTRIBUTE_VALUE_EMPTY ); 121 assertTrue( "Not the same object", 122 HtmlElement.ATTRIBUTE_NOT_DEFINED != HtmlElement.ATTRIBUTE_VALUE_EMPTY ); 123 } 124 125 } 126 | Popular Tags |