1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.ArrayList; 41 import java.util.Collections; 42 import java.util.List; 43 44 import com.gargoylesoftware.htmlunit.CollectingAlertHandler; 45 import com.gargoylesoftware.htmlunit.MockWebConnection; 46 import com.gargoylesoftware.htmlunit.SubmitMethod; 47 import com.gargoylesoftware.htmlunit.WebClient; 48 import com.gargoylesoftware.htmlunit.WebTestCase; 49 50 56 public class HtmlAnchorTest extends WebTestCase { 57 58 63 public HtmlAnchorTest( final String name ) { 64 super( name ); 65 } 66 67 68 71 public void testClick() throws Exception { 72 73 final String htmlContent 74 = "<html><head><title>foo</title></head><body>" 75 + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>" 76 + "<a HREF='http://www.foo2.com' id='a2'>link to foo2</a>" 77 + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>" 78 + "</body></html>"; 79 80 final HtmlPage page = loadPage(htmlContent); 81 final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" ); 82 83 final HtmlPage secondPage = ( HtmlPage )anchor.click(); 85 86 final List expectedParameters = Collections.EMPTY_LIST; 87 final MockWebConnection webConnection = getMockConnection(secondPage); 88 89 assertEquals( "url", "http://www.foo2.com", 90 secondPage.getWebResponse().getUrl().toExternalForm() ); 91 assertEquals( "method", SubmitMethod.GET, webConnection.getLastMethod() ); 92 assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() ); 93 assertNotNull( secondPage ); 94 } 95 98 public void testClickAnchorName() throws Exception { 99 final String htmlContent 100 = "<html><head><title>foo</title></head><body>" 101 + "<a HREF='#clickedAnchor' id='a1'>link to foo1</a>" 102 + "</body></html>"; 103 final WebClient client = new WebClient(); 104 105 final MockWebConnection webConnection = new MockWebConnection( client ); 106 webConnection.setDefaultResponse( htmlContent ); 107 client.setWebConnection( webConnection ); 108 109 final HtmlPage page = ( HtmlPage )client.getPage( 110 URL_GARGOYLE ); 111 112 final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a1" ); 113 114 final HtmlPage secondPage = ( HtmlPage )anchor.click(); 116 117 assertEquals("url", URL_GARGOYLE.toExternalForm(), 119 secondPage.getWebResponse().getUrl().toExternalForm()); 120 } 121 122 123 126 public void testClick_onClickHandler() 127 throws Exception { 128 129 final String firstContent 130 = "<html><head><title>First</title></head><body>" 131 + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>" 132 + "<a HREF='http://second' id='a2' " 133 + "onClick='alert(\"clicked\")'>link to foo2</a>" 134 + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>" 135 + "</body></html>"; 136 final String secondContent 137 = "<html><head><title>Second</title></head><body></body></html>"; 138 139 final WebClient client = new WebClient(); 140 final List collectedAlerts = new ArrayList(); 141 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 142 143 final MockWebConnection webConnection = new MockWebConnection( client ); 144 webConnection.setResponse( 145 URL_FIRST, firstContent, 200, "OK", "text/html", 146 Collections.EMPTY_LIST ); 147 webConnection.setResponse( 148 URL_SECOND, secondContent, 200, "OK", "text/html", 149 Collections.EMPTY_LIST ); 150 client.setWebConnection( webConnection ); 151 152 final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST); 153 final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" ); 154 155 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 156 157 final HtmlPage secondPage = ( HtmlPage )anchor.click(); 158 159 assertEquals( Collections.singletonList("clicked"), collectedAlerts ); 160 assertEquals( "Second", secondPage.getTitleText() ); 161 } 162 163 164 167 public void testClick_onClickHandler_returnFalse() 168 throws Exception { 169 170 final String firstContent 171 = "<html><head><title>First</title></head><body>" 172 + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>" 173 + "<a HREF='http://second' id='a2' " 174 + "onClick='alert(\"clicked\");return false;'>link to foo2</a>" 175 + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>" 176 + "</body></html>"; 177 final String secondContent 178 = "<html><head><title>Second</title></head><body></body></html>"; 179 180 final WebClient client = new WebClient(); 181 final List collectedAlerts = new ArrayList(); 182 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 183 184 final MockWebConnection webConnection = new MockWebConnection( client ); 185 webConnection.setResponse( 186 URL_FIRST, firstContent, 200, "OK", "text/html", 187 Collections.EMPTY_LIST ); 188 webConnection.setResponse( 189 URL_SECOND, secondContent, 200, "OK", "text/html", 190 Collections.EMPTY_LIST ); 191 client.setWebConnection( webConnection ); 192 193 final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST); 194 final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" ); 195 196 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 197 198 final HtmlPage secondPage = ( HtmlPage )anchor.click(); 199 200 assertEquals( Collections.singletonList("clicked"), collectedAlerts ); 201 assertSame( page, secondPage ); 202 } 203 204 205 208 public void testClick_onClickHandler_javascriptDisabled() throws Exception { 209 210 final String htmlContent 211 = "<html><head><title>foo</title></head><body>" 212 + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>" 213 + "<a HREF='http://www.foo2.com' id='a2' " 214 + "onClick='alert(\"clicked\")'>link to foo2</a>" 215 + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>" 216 + "</body></html>"; 217 final WebClient client = new WebClient(); 218 client.setJavaScriptEnabled(false); 219 220 final List collectedAlerts = new ArrayList(); 221 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 222 223 final MockWebConnection webConnection = new MockWebConnection( client ); 224 webConnection.setDefaultResponse( htmlContent ); 225 client.setWebConnection( webConnection ); 226 227 final HtmlPage page = ( HtmlPage )client.getPage(URL_GARGOYLE); 228 final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" ); 229 230 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 231 232 final HtmlPage secondPage = ( HtmlPage )anchor.click(); 233 234 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 235 final List expectedParameters = Collections.EMPTY_LIST; 236 237 assertEquals( "url", "http://www.foo2.com", 238 secondPage.getWebResponse().getUrl().toExternalForm() ); 239 assertEquals( "method", SubmitMethod.GET, webConnection.getLastMethod() ); 240 assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() ); 241 assertNotNull( secondPage ); 242 } 243 244 245 248 public void testClick_javascriptUrl() throws Exception { 249 250 final String htmlContent 251 = "<html><head><title>foo</title></head><body>" 252 + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>" 253 + "<a HREF='javascript:alert(\"clicked\")' id='a2'>link to foo2</a>" 254 + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>" 255 + "</body></html>"; 256 final List collectedAlerts = new ArrayList(); 257 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 258 259 final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" ); 260 261 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 262 263 final HtmlPage secondPage = ( HtmlPage )anchor.click(); 264 265 assertEquals( Collections.singletonList("clicked"), collectedAlerts ); 266 assertSame( page, secondPage ); 267 } 268 269 270 273 public void testClick_javascriptUrl_javascriptDisabled() throws Exception { 274 275 final String htmlContent 276 = "<html><head><title>foo</title></head><body>" 277 + "<a HREF='http://www.foo1.com' id='a1'>link to foo1</a>" 278 + "<a HREF='javascript:alert(\"clicked\")' id='a2'>link to foo2</a>" 279 + "<a HREF='http://www.foo3.com' id='a3'>link to foo3</a>" 280 + "</body></html>"; 281 final WebClient client = new WebClient(); 282 client.setJavaScriptEnabled(false); 283 284 final List collectedAlerts = new ArrayList(); 285 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 286 287 final MockWebConnection webConnection = new MockWebConnection( client ); 288 webConnection.setDefaultResponse( htmlContent ); 289 client.setWebConnection( webConnection ); 290 291 final HtmlPage page = ( HtmlPage )client.getPage(URL_GARGOYLE); 292 final HtmlAnchor anchor = ( HtmlAnchor )page.getHtmlElementById( "a2" ); 293 294 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 295 296 final HtmlPage secondPage = ( HtmlPage )anchor.click(); 297 298 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 299 assertSame( page, secondPage ); 300 } 301 302 303 306 public void testClick_javascriptUrl_InvalidReturn_RegressionTest() throws Exception { 307 308 final String htmlContent 309 = "<html><head><SCRIPT lang=\"JavaScript\">" 310 + "function doSubmit(formName){" 311 + " return false;" 312 + "}" 313 + "</SCRIPT></head><body><form name=\"formName\" method=\"POST\"action=\"../foo\">" 314 + "<a HREF=\".\" id=\"testJavascript\" name=\"testJavascript\"" 315 + "onclick=\"return false;\">Test Link </a>" 316 + "<input type=\"submit\" value=\"Login\" " 317 + "name=\"loginButton\"></form></body></html>"; 318 319 final HtmlPage page = loadPage(htmlContent); 320 final HtmlAnchor testAnchor = page.getAnchorByName("testJavascript"); 321 testAnchor.click(); } 323 } 324 325 | Popular Tags |