1 38 package com.gargoylesoftware.htmlunit; 39 40 import com.gargoylesoftware.htmlunit.html.HtmlForm; 41 import com.gargoylesoftware.htmlunit.html.HtmlPage; 42 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; 43 import java.net.MalformedURLException; 44 import java.net.URL; 45 import junit.textui.TestRunner; 46 47 56 public class SanityCheck extends WebTestCase { 57 private static final BrowserVersion BROWSER_VERSION = BrowserVersion.MOZILLA_1_0; 58 59 63 public SanityCheck( final String name ) { 64 super( name ); 65 } 66 67 68 73 public static void main( final String args[] ) throws Exception { 74 new MainTestSuite("foo").enableAllLogging(); 75 TestRunner.run( SanityCheck.class ); 76 System.exit( 0 ); 77 } 78 79 80 84 public void testYahooMail() throws Exception { 85 final WebClient webClient = new WebClient(BROWSER_VERSION); 86 assertInstanceOf(webClient.getPage( new URL( "http://mail.yahoo.com/" ) ), HtmlPage.class); 87 } 88 89 90 94 public void testYahoo() throws Exception { 95 final WebClient webClient = new WebClient(BROWSER_VERSION); 96 assertInstanceOf( webClient.getPage( new URL( "http://yahoo.com/" ) ), HtmlPage.class ); 97 } 98 99 103 public void testYahoo_Spanish() throws Exception { 104 final WebClient webClient = new WebClient(BROWSER_VERSION); 105 assertInstanceOf( 106 webClient.getPage( new URL( "http://edit.europe.yahoo.com/config/mail?.intl=es" ) ), HtmlPage.class ); 107 } 108 109 110 114 public void testIBM() throws Exception { 115 final WebClient webClient = new WebClient(BROWSER_VERSION); 116 webClient.setRedirectEnabled( true ); 117 final HtmlPage page = (HtmlPage)webClient.getPage( new URL( "http://www.ibm.com/" ) ); 118 assertEquals( "http://www.ibm.com/us/", page.getWebResponse().getUrl().toExternalForm() ); 119 } 120 121 122 126 public void testAlphaWorks() throws Exception { 127 final WebClient webClient = new WebClient(BROWSER_VERSION); 128 assertInstanceOf(webClient.getPage(new URL( "http://www.alphaworks.ibm.com" ) ), HtmlPage.class); 129 } 130 131 132 136 public void testCNN() throws Exception { 137 final WebClient webClient = new WebClient(BROWSER_VERSION); 138 assertInstanceOf( webClient.getPage( new URL( "http://www.cnn.com" ) ), HtmlPage.class); 139 } 140 141 142 146 public void testToyotaCanada() throws Exception { 147 final WebClient webClient = new WebClient(BROWSER_VERSION); 148 assertInstanceOf(webClient.getPage( new URL( "http://www.toyota.ca" ) ), HtmlPage.class); 149 } 150 151 152 156 public void testSourceForge_secure() throws Exception { 157 try { 158 final WebClient webClient = new WebClient(BROWSER_VERSION); 159 webClient.setPrintContentOnFailingStatusCode(true); 160 assertInstanceOf( 161 webClient.getPage( new URL( "https://sourceforge.net/projects/htmlunit/" ) ), 162 HtmlPage.class ); 163 } 164 catch( final MalformedURLException e ) { 165 System.out.println("Skipping https test: "+getName()); 166 } 167 } 168 169 170 174 public void testYahooLogin_secure() throws Exception { 175 try { 176 final WebClient webClient = new WebClient(BROWSER_VERSION); 177 final HtmlPage page = (HtmlPage)webClient.getPage( new URL( "https://login.yahoo.com/" ) ); 178 final HtmlForm form = page.getFormByName("login_form"); 179 assertNotNull(form); 180 } 181 catch( final MalformedURLException e ) { 182 System.out.println("Skipping https test: "+getName()); 183 } 184 } 185 186 187 191 public void testAmazonCanada() throws Exception { 192 final WebClient webClient = new WebClient(BROWSER_VERSION); 193 assertInstanceOf( webClient.getPage( new URL( "http://www.amazon.ca/" ) ), HtmlPage.class ); 194 } 195 196 197 201 public void testCnnAfterHours() throws Exception { 202 final WebClient webClient = new WebClient(BROWSER_VERSION); 203 assertInstanceOf( webClient.getPage( new URL( "http://money.cnn.com/markets/afterhours/" ) ), HtmlPage.class ); 204 } 205 206 207 211 public void testHtmlUnitHomepage() throws Exception { 212 final WebClient webClient = new WebClient(BROWSER_VERSION); 213 assertInstanceOf( webClient.getPage( new URL( "http://htmlunit.sourceforge.net" ) ), HtmlPage.class ); 214 } 215 216 217 221 public void testAdobeAcrobatReaderDownloadStep2() throws Exception { 222 final WebClient webClient = new WebClient(); 223 assertInstanceOf( webClient.getPage( new URL( "http://www.adobe.com/products/acrobat/readstep2.html" ) ), 224 HtmlPage.class ); 225 } 226 227 228 231 public void setUp() { 232 System.out.println(); 233 System.out.println( "=====================================" ); 234 System.out.println( "== Starting test: " + getName() ); 235 System.out.println( "=====================================" ); 236 } 237 238 239 private URL getPrintEnvUrl() throws MalformedURLException { 240 return new URL("http://htmlunit.sourceforge.net/cgi-bin/printenv"); 241 } 242 243 244 249 public void testGetMethodWithParameters() throws Exception { 250 final WebClient webClient = new WebClient(); 251 final HtmlPage firstPage = (HtmlPage)webClient.getPage(getPrintEnvUrl()); 252 253 assertEquals("GET", firstPage.getHtmlElementById("REQUEST_METHOD").asText()); 254 255 final HtmlForm form = firstPage.getFormByName("form1"); 256 assertEquals("get", form.getMethodAttribute()); 257 258 final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button1"); 259 final HtmlPage secondPage = (HtmlPage)button.click(); 260 assertEquals("GET", secondPage.getHtmlElementById("REQUEST_METHOD").asText()); 261 assertEquals("textfield1=*&button1=PushMe", 262 secondPage.getHtmlElementById("QUERY_STRING").asText()); 263 assertEquals("", secondPage.getHtmlElementById("CONTENT").asText()); 264 } 265 266 267 272 public void testPostMethodWithDuplicateParameters() throws Exception { 273 final WebClient webClient = new WebClient(); 274 final HtmlPage firstPage = (HtmlPage)webClient.getPage(getPrintEnvUrl()); 275 276 assertEquals("GET", firstPage.getHtmlElementById("REQUEST_METHOD").asText()); 277 278 final HtmlForm form = firstPage.getFormByName("form1"); 279 form.setMethodAttribute("post"); 280 281 final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button1"); 282 button.setAttributeValue("name", "textfield1"); 283 284 final HtmlPage secondPage = (HtmlPage)button.click(); 285 assertEquals("POST", secondPage.getHtmlElementById("REQUEST_METHOD").asText()); 286 assertEquals("", secondPage.getHtmlElementById("QUERY_STRING").asText()); 287 assertEquals("textfield1=*&textfield1=PushMe", 288 secondPage.getHtmlElementById("CONTENT").asText()); 289 } 290 291 296 public void testPostMethodWithParameters() throws Exception { 297 final WebClient webClient = new WebClient(); 298 final HtmlPage firstPage = (HtmlPage)webClient.getPage(getPrintEnvUrl()); 299 300 assertEquals("GET", firstPage.getHtmlElementById("REQUEST_METHOD").asText()); 301 302 final HtmlForm form = firstPage.getFormByName("form1"); 303 form.setMethodAttribute("post"); 304 305 final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button1"); 306 final HtmlPage secondPage = (HtmlPage)button.click(); 307 assertEquals("POST", secondPage.getHtmlElementById("REQUEST_METHOD").asText()); 308 assertEquals("", secondPage.getHtmlElementById("QUERY_STRING").asText()); 309 assertEquals("textfield1=*&button1=PushMe", 310 secondPage.getHtmlElementById("CONTENT").asText()); 311 } 312 } 313 314 | Popular Tags |