1 38 package com.gargoylesoftware.htmlunit; 39 40 import java.net.ConnectException; 41 import java.net.SocketException; 42 import java.net.URL; 43 import java.net.URLConnection; 44 import java.util.ArrayList; 45 import java.util.Arrays; 46 import java.util.Collections; 47 import java.util.List; 48 49 import com.gargoylesoftware.htmlunit.html.HTMLParser; 50 import com.gargoylesoftware.htmlunit.html.HtmlElement; 51 import com.gargoylesoftware.htmlunit.html.HtmlNoScript; 52 import com.gargoylesoftware.htmlunit.html.HtmlPage; 53 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath; 54 55 61 public class HTMLParserTest extends WebTestCase { 62 63 67 public HTMLParserTest( final String name ) { 68 super(name); 69 } 70 71 76 public void testSimpleHTMLString() throws Exception { 77 final WebClient webClient = new WebClient(); 78 final WebResponse webResponse = new StringWebResponse( 79 "<html><head><title>TITLE</title><noscript>TEST</noscript></head><body></body></html>"); 80 81 final HtmlPage page = HTMLParser.parse(webResponse, webClient.getCurrentWindow()); 82 83 HtmlUnitXPath xpath = new HtmlUnitXPath("//noscript"); 84 final String stringVal = xpath.stringValueOf(page); 85 86 assertEquals("TEST", stringVal); 87 88 xpath = new HtmlUnitXPath("//*[./text() = 'TEST']"); 89 final HtmlElement node = (HtmlElement)xpath.selectSingleNode(page); 90 91 assertEquals(node.getTagName(), HtmlNoScript.TAG_NAME); 92 } 93 94 98 public void testBadlyFormedHTML() throws Exception { 99 final String content 100 = "<html><head><title>first</title>" 101 + "<script>" 102 + "function test()" 103 + "{" 104 + " alert(document.getElementById('myInput').form.id);\n" 105 + "}" 106 + "</script>" 107 + "</head>" 108 + "<body onload='test()'>" 109 + "<table>" 110 + "<form name='myForm' action='foo' id='myForm'>" 111 + "<tr><td>" 112 + "<input type='text' name='myInput' id='myInput'/>" 113 + "</td></tr>" 114 + "</form>" 115 + "</table>" 116 + "</body></html>"; 117 118 final List collectedAlerts = new ArrayList(); 119 final List expectedAlerts = Arrays.asList(new String[]{"myForm"}); 120 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 121 122 loadPage(content, collectedAlerts); 123 124 assertEquals( expectedAlerts, collectedAlerts ); 125 } 126 127 131 public void testUnknownTagInHead() throws Exception { 132 if (true) { 133 notImplemented(); 134 return; 135 } 136 137 final String content 141 = "<html><head><mainA3>" 142 + "<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>" 143 + "<title>first</title>" 144 + "<script>" 145 + "function test()" 146 + "{" 147 + " alert(document.title);\n" 148 + "}" 149 + "</script>" 150 + "</head>" 151 + "<body onload='test()'>" 152 + "</body></html>"; 153 154 final List collectedAlerts = new ArrayList(); 155 final List expectedAlerts = Arrays.asList(new String[]{"first"}); 156 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 157 158 final HtmlPage page = loadPage(content, collectedAlerts); 159 System.out.println(page.asXml()); 160 161 assertEquals( expectedAlerts, collectedAlerts ); 162 } 163 164 170 public static void testHtmlUnitHomePage() throws Exception { 171 172 final URL htmlUnitSite = new URL("http://htmlunit.sourceforge.net"); 173 try { 174 final URLConnection connection = htmlUnitSite.openConnection(); 175 connection.connect(); 176 } 177 catch (final ConnectException e) { 178 182 System.out.println("Connection could not be made to " + htmlUnitSite.toExternalForm()); 183 return; 184 } 185 catch (final SocketException e) { 186 189 System.out.println("Connection could not be made to " + htmlUnitSite.toExternalForm()); 190 return; 191 } 192 193 final WebClient webClient = new WebClient(); 194 final WebResponse webResponse = new HttpWebConnection(webClient).getResponse( 195 htmlUnitSite, 196 SubmitMethod.GET, 197 Collections.EMPTY_LIST, 198 Collections.EMPTY_MAP 199 ); 200 201 final HtmlPage page = HTMLParser.parse(webResponse, webClient.getCurrentWindow()); 202 203 HtmlUnitXPath xpath = new HtmlUnitXPath("//div[@id='footer']/div[@class='xright']"); 205 final String stringVal = xpath.stringValueOf(page).trim(); 206 assertEquals("\u00A9 2002-2005, Gargoyle Software Inc.", stringVal); 207 208 218 } 219 } 220 | Popular Tags |