1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import java.net.URL; 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.Collections; 44 import java.util.List; 45 46 import com.gargoylesoftware.base.testing.EventCatcher; 47 import com.gargoylesoftware.htmlunit.CollectingAlertHandler; 48 import com.gargoylesoftware.htmlunit.ConfirmHandler; 49 import com.gargoylesoftware.htmlunit.MockWebConnection; 50 import com.gargoylesoftware.htmlunit.Page; 51 import com.gargoylesoftware.htmlunit.PromptHandler; 52 import com.gargoylesoftware.htmlunit.StatusHandler; 53 import com.gargoylesoftware.htmlunit.WebClient; 54 import com.gargoylesoftware.htmlunit.WebTestCase; 55 import com.gargoylesoftware.htmlunit.WebWindow; 56 import com.gargoylesoftware.htmlunit.WebWindowEvent; 57 import com.gargoylesoftware.htmlunit.WebWindowNotFoundException; 58 import com.gargoylesoftware.htmlunit.html.HtmlAnchor; 59 import com.gargoylesoftware.htmlunit.html.HtmlButton; 60 import com.gargoylesoftware.htmlunit.html.HtmlInlineFrame; 61 import com.gargoylesoftware.htmlunit.html.HtmlPage; 62 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; 63 64 77 public class WindowTest extends WebTestCase { 78 82 public WindowTest( final String name ) { 83 super(name); 84 } 85 86 87 90 public void testSetLocation() throws Exception { 91 final WebClient webClient = new WebClient(); 92 final MockWebConnection webConnection = new MockWebConnection( webClient ); 93 94 final String firstContent 95 = "<html><head><title>First</title></head><body>" 96 + "<form name='form1'>" 97 + " <a id='link' onClick='location=\"http://second\"; return false;'>Click me</a>" 98 + "</form>" 99 + "</body></html>"; 100 final String secondContent 101 = "<html><head><title>Second</title></head><body></body></html>"; 102 103 webConnection.setResponse(URL_FIRST, firstContent); 104 webConnection.setResponse(URL_SECOND, secondContent); 105 webClient.setWebConnection( webConnection ); 106 107 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 108 assertEquals( "First", firstPage.getTitleText() ); 109 110 final HtmlAnchor anchor = (HtmlAnchor)firstPage.getHtmlElementById("link"); 111 final HtmlPage secondPage = (HtmlPage)anchor.click(); 112 assertNotNull("secondPage", secondPage); 113 assertEquals( "Second", secondPage.getTitleText() ); 114 assertSame( webClient.getCurrentWindow(), secondPage.getEnclosingWindow() ); 115 } 116 117 118 121 public void testOpenWindow() throws Exception { 122 final WebClient webClient = new WebClient(); 123 final MockWebConnection webConnection = new MockWebConnection( webClient ); 124 125 final List collectedAlerts = new ArrayList(); 126 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 127 128 final String firstContent 129 = "<html><head><title>First</title></head><body>" 130 + "<form name='form1'>" 131 + " <a id='link' onClick='open(\"http://second\", \"MyNewWindow\").focus(); " 132 + "return false;'>Click me</a>" 133 + "</form>" 134 + "</body></html>"; 135 final String secondContent 136 = "<html><head><title>Second</title></head><body>" 137 + "<script>alert(self.name)</script>" 138 + "</body></html>"; 139 140 final EventCatcher eventCatcher = new EventCatcher(); 141 eventCatcher.listenTo( webClient ); 142 143 webConnection.setResponse(URL_FIRST, firstContent); 144 webConnection.setResponse(URL_SECOND, secondContent); 145 webClient.setWebConnection( webConnection ); 146 147 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 148 assertEquals( "First", firstPage.getTitleText() ); 149 150 final HtmlAnchor anchor = (HtmlAnchor)firstPage.getHtmlElementById("link"); 151 final HtmlPage secondPage = (HtmlPage)anchor.click(); 152 assertNotSame( firstPage, secondPage ); 153 154 assertEquals( 3, eventCatcher.getEventCount() ); 156 157 final WebWindow firstWebWindow 158 = (WebWindow)((WebWindowEvent)eventCatcher.getEventAt(0)).getSource(); 159 final WebWindow secondWebWindow 160 = (WebWindow)((WebWindowEvent)eventCatcher.getEventAt(2)).getSource(); 161 assertSame( webClient.getCurrentWindow(), secondWebWindow); 162 assertEquals( "MyNewWindow", secondWebWindow.getName() ); 163 164 assertEquals( "First", ((HtmlPage)firstWebWindow.getEnclosedPage()).getTitleText()); 165 assertEquals( "Second", ((HtmlPage)secondWebWindow.getEnclosedPage()).getTitleText()); 166 167 final WebWindowEvent changedEvent = (WebWindowEvent)eventCatcher.getEventAt(2); 168 assertNull( changedEvent.getOldPage() ); 169 assertEquals( "Second", ((HtmlPage)changedEvent.getNewPage()).getTitleText() ); 170 171 assertEquals( 172 Collections.singletonList("MyNewWindow"), 173 collectedAlerts); 174 } 175 176 177 180 public void testOpenWindow_base() throws Exception { 181 final WebClient webClient = new WebClient(); 182 final MockWebConnection webConnection = new MockWebConnection( webClient ); 183 184 final List collectedAlerts = new ArrayList(); 185 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 186 187 final String firstContent 188 = "<html><head><title>First</title><base target='MyNewWindow'></head><body>" 189 + "<form name='form1'>" 190 + " <a id='link' HREF='http://second'>Click me</a>" 191 + "</form>" 192 + "</body></html>"; 193 final String secondContent 194 = "<html><head><title>Second</title></head><body>" 195 + "<script>alert(self.name)</script>" 196 + "</body></html>"; 197 198 webConnection.setResponse(URL_FIRST, firstContent); 199 webConnection.setResponse(URL_SECOND, secondContent); 200 webClient.setWebConnection( webConnection ); 201 202 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 203 assertEquals( "First", firstPage.getTitleText() ); 204 final WebWindow firstWebWindow = firstPage.getEnclosingWindow(); 205 assertEquals( firstWebWindow, firstWebWindow.getTopWindow() ); 206 207 final HtmlAnchor anchor = (HtmlAnchor)firstPage.getHtmlElementById("link"); 208 final HtmlPage secondPage = (HtmlPage)anchor.click(); 209 assertEquals( "Second", secondPage.getTitleText() ); 210 assertNotSame( firstPage, secondPage ); 211 212 final WebWindow secondWebWindow = secondPage.getEnclosingWindow(); 213 assertNotSame( firstWebWindow, secondWebWindow ); 214 assertEquals( "MyNewWindow", secondWebWindow.getName() ); 215 assertEquals( secondWebWindow, secondWebWindow.getTopWindow() ); 216 217 assertEquals( 218 Collections.singletonList("MyNewWindow"), 219 collectedAlerts); 220 } 221 222 223 228 public void testOpenWindow_blank() throws Exception { 229 final WebClient webClient = new WebClient(); 230 final MockWebConnection webConnection = new MockWebConnection( webClient ); 231 232 final List collectedAlerts = new ArrayList(); 233 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 234 235 final String firstContent 236 = "<html><head><title>First</title></head><body>" 237 + " <iframe name='secondFrame' id='secondFrame' SRC='http://second' />" 238 + "</body></html>"; 239 final String secondContent 240 = "<html><head><title>Second</title></head><body>" 241 + " <a id='link' " 242 + "onClick='open(\"http://third\", \"_blank\").focus(); '>" 243 + "Click me</a>" 244 + "</body></html>"; 245 final String thirdContent 246 = "<html><head><title>Third</title></head><body>" 247 + "</body></html>"; 248 249 webConnection.setResponse(URL_FIRST, firstContent); 250 webConnection.setResponse(URL_SECOND, secondContent); 251 webConnection.setResponse(URL_THIRD, thirdContent); 252 webClient.setWebConnection( webConnection ); 253 254 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 255 assertEquals( "First", firstPage.getTitleText() ); 256 final WebWindow firstWindow = firstPage.getEnclosingWindow(); 257 258 final HtmlInlineFrame secondFrame = 259 (HtmlInlineFrame)firstPage.getHtmlElementById("secondFrame"); 260 final HtmlPage secondPage = (HtmlPage)secondFrame.getEnclosedPage(); 261 assertEquals( "Second", secondPage.getTitleText() ); 262 try { 263 assertEquals(secondFrame.getEnclosedWindow(), 264 webClient.getWebWindowByName( "secondFrame" )); 265 } 267 catch (final WebWindowNotFoundException exception) { 268 fail( "Expected secondFrame would be found before click." ); 269 } 270 final HtmlAnchor anchor = (HtmlAnchor)secondPage.getHtmlElementById("link"); 271 final HtmlPage thirdPage = (HtmlPage)anchor.click(); 272 assertEquals( "Third", thirdPage.getTitleText() ); 273 final WebWindow thirdWindow = thirdPage.getEnclosingWindow(); 274 assertNotSame( firstWindow, thirdWindow ); 275 276 assertEquals( "", thirdWindow.getName() ); 277 278 assertEquals( thirdWindow, thirdWindow.getTopWindow() ); 279 try { 280 assertEquals(secondFrame.getEnclosedWindow(), 281 webClient.getWebWindowByName( "secondFrame" )); 282 } 284 catch (final WebWindowNotFoundException exception) { 285 fail( "Expected secondFrame would be found after click." ); 286 } 287 288 assertEquals( 289 Collections.EMPTY_LIST, 290 collectedAlerts); 291 } 292 293 294 299 public void testOpenWindow_self() throws Exception { 300 final WebClient webClient = new WebClient(); 301 final MockWebConnection webConnection = new MockWebConnection( webClient ); 302 303 final String firstContent 304 = "<html><head><title>First</title></head><body>" 305 + "<form name='form1'>" 306 + " <a id='link' onClick='open(\"http://second\", \"_self\"); " 307 + "return false;'>Click me</a>" 308 + "</form>" 309 + "</body></html>"; 310 final String secondContent 311 = "<html><head><title>Second</title></head><body></body></html>"; 312 313 final EventCatcher eventCatcher = new EventCatcher(); 314 315 webConnection.setResponse(URL_FIRST, firstContent); 316 webConnection.setResponse(URL_SECOND, secondContent); 317 webClient.setWebConnection( webConnection ); 318 319 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 320 assertEquals( "First", firstPage.getTitleText() ); 321 322 eventCatcher.listenTo( webClient ); 323 324 final WebWindow firstWebWindow = firstPage.getEnclosingWindow(); 325 326 final HtmlAnchor anchor = (HtmlAnchor)firstPage.getHtmlElementById("link"); 327 final HtmlPage secondPage = (HtmlPage)anchor.click(); 328 assertEquals( "First", firstPage.getTitleText() ); 329 assertEquals( "Second", secondPage.getTitleText() ); 330 331 assertEquals( 1, eventCatcher.getEventCount() ); 332 333 final WebWindow secondWebWindow 334 = (WebWindow)((WebWindowEvent)eventCatcher.getEventAt(0)).getSource(); 335 assertSame( webClient.getCurrentWindow(), firstWebWindow); 336 assertSame( firstWebWindow, secondWebWindow ); 337 } 338 339 340 345 public void testOpenWindow_top() throws Exception { 346 final WebClient webClient = new WebClient(); 347 final MockWebConnection webConnection = new MockWebConnection( webClient ); 348 349 final String firstContent 350 = "<html><head><title>First</title></head><body>" 351 + " <iframe name='secondFrame' id='secondFrame' SRC='http://second' />" 352 + "</body></html>"; 353 final String secondContent 354 = "<html><head><title>Second</title></head><body>" 355 + " <iframe name='thirdFrame' id='thirdFrame' SRC='http://third' />" 356 + "</body></html>"; 357 final String thirdContent 358 = "<html><head><title>Third</title></head><body>" 359 + " <a id='link' onClick='open(\"http://fourth\", \"_top\"); " 360 + "return false;'>Click me</a>" 361 + "</body></html>"; 362 final String fourthContent 363 = "<html><head><title>Fourth</title></head><body></body></html>"; 364 365 webConnection.setResponse(URL_FIRST, firstContent); 366 webConnection.setResponse(URL_SECOND, secondContent); 367 webConnection.setResponse(URL_THIRD, thirdContent); 368 webConnection.setResponse(new URL("http://fourth"), fourthContent); 369 webClient.setWebConnection( webConnection ); 370 371 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 372 assertEquals( "First", firstPage.getTitleText() ); 373 374 final WebWindow firstWebWindow = firstPage.getEnclosingWindow(); 375 assertEquals( "First", firstPage.getTitleText() ); 376 final HtmlInlineFrame secondFrame = 377 (HtmlInlineFrame)firstPage.getHtmlElementById("secondFrame"); 378 final HtmlPage secondPage = (HtmlPage)secondFrame.getEnclosedPage(); 379 assertEquals( "Second", secondPage.getTitleText() ); 380 final HtmlInlineFrame thirdFrame = 381 (HtmlInlineFrame)secondPage.getHtmlElementById("thirdFrame"); 382 final HtmlPage thirdPage = (HtmlPage)thirdFrame.getEnclosedPage(); 383 assertEquals( "Third", thirdPage.getTitleText() ); 384 385 assertSame( webClient.getCurrentWindow(), firstWebWindow); 386 assertNotSame( firstWebWindow, secondPage ); 387 388 final HtmlAnchor anchor = 389 (HtmlAnchor)thirdPage.getHtmlElementById("link"); 390 final HtmlPage fourthPage = (HtmlPage)anchor.click(); 391 final WebWindow fourthWebWindow = fourthPage.getEnclosingWindow(); 392 assertSame( firstWebWindow, fourthWebWindow ); 393 assertSame( fourthWebWindow, fourthWebWindow.getTopWindow() ); 394 try { 395 webClient.getWebWindowByName( "secondFrame" ); 396 fail( "Did not expect secondFrame to still exist after click." ); 397 } 398 catch (final WebWindowNotFoundException exception) { 399 } 401 try { 402 webClient.getWebWindowByName( "thirdFrame" ); 403 fail( "Did not expect thirdFrame to still exist after click." ); 404 } 405 catch (final WebWindowNotFoundException exception) { 406 } 408 } 409 410 411 416 public void testOpenWindow_parent() throws Exception { 417 final WebClient webClient = new WebClient(); 418 final MockWebConnection webConnection = new MockWebConnection( webClient ); 419 420 final String firstContent 421 = "<html><head><title>First</title></head><body>" 422 + " <iframe name='secondFrame' id='secondFrame' SRC='http://second' />" 423 + "</body></html>"; 424 final String secondContent 425 = "<html><head><title>Second</title></head><body>" 426 + " <iframe name='thirdFrame' id='thirdFrame' SRC='http://third' />" 427 + "</body></html>"; 428 final String thirdContent 429 = "<html><head><title>Third</title></head><body>" 430 + " <a id='link' onClick='open(\"http://fourth\", \"_parent\"); " 431 + "return false;'>Click me</a>" 432 + "</body></html>"; 433 final String fourthContent 434 = "<html><head><title>Fourth</title></head><body></body></html>"; 435 436 webConnection.setResponse(URL_FIRST, firstContent); 437 webConnection.setResponse(URL_SECOND, secondContent); 438 webConnection.setResponse(URL_THIRD, thirdContent); 439 webConnection.setResponse(new URL("http://fourth"), fourthContent); 440 webClient.setWebConnection( webConnection ); 441 442 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 443 assertEquals( "First", firstPage.getTitleText() ); 444 445 final WebWindow firstWebWindow = firstPage.getEnclosingWindow(); 446 assertEquals( "First", firstPage.getTitleText() ); 447 final HtmlInlineFrame secondFrame = 448 (HtmlInlineFrame)firstPage.getHtmlElementById("secondFrame"); 449 final HtmlPage secondPage = (HtmlPage)secondFrame.getEnclosedPage(); 450 assertEquals( "Second", secondPage.getTitleText() ); 451 final HtmlInlineFrame thirdFrame = 452 (HtmlInlineFrame)secondPage.getHtmlElementById("thirdFrame"); 453 final HtmlPage thirdPage = (HtmlPage)thirdFrame.getEnclosedPage(); 454 assertEquals( "Third", thirdPage.getTitleText() ); 455 456 457 assertSame( webClient.getCurrentWindow(), firstWebWindow); 458 assertNotSame( firstWebWindow, secondFrame ); 459 460 final HtmlAnchor anchor = 461 (HtmlAnchor)thirdPage.getHtmlElementById("link"); 462 final HtmlPage fourthPage = (HtmlPage)anchor.click(); 463 final WebWindow fourthWebWindow = fourthPage.getEnclosingWindow(); 464 assertSame( secondFrame.getEnclosedWindow(), fourthWebWindow ); 465 try { 466 final WebWindow namedWindow = webClient.getWebWindowByName( "secondFrame" ); 467 assertSame( namedWindow.getEnclosedPage(), fourthPage); 468 } 470 catch (final WebWindowNotFoundException exception) { 471 fail( "Expected secondFrame would be found after click." ); 472 } 473 try { 474 webClient.getWebWindowByName( "thirdFrame" ); 475 fail( "Did not expect thirdFrame to still exist after click." ); 476 } 477 catch (final WebWindowNotFoundException exception) { 478 } 480 } 481 482 483 487 public void testAlert_NoAlertHandler() throws Exception { 488 final String firstContent 489 = "<html><head><title>First</title><script>function doTest(){alert('foo')}</script></head>" 490 + "<body onload='doTest()'></body></html>"; 491 492 getLog().warn("Warning for no alert handler expected next"); 493 final HtmlPage firstPage = loadPage(firstContent); 494 assertEquals( "First", firstPage.getTitleText() ); 495 } 496 497 498 501 public void testParentAndTop() throws Exception { 502 503 final String firstContent 504 = "<html><head><title>First</title></head><body>" 505 + " <iframe name='left' SRC='http://second' />" 506 + "</body></html>"; 507 final String secondContent 508 = "<html><head><title>Second</title></head><body>" 509 + " <iframe name='innermost' SRC='http://third' />" 510 + "</body></html>"; 511 final String thirdContent 512 = "<html><head><title>Third</title><script>" 513 + "function doAlert() {\n" 514 + " alert(parent != this);\n" 515 + " alert(top != this);\n" 516 + " alert(parent != top);\n" 517 + " alert(parent.parent == top);\n" 518 + " alert(parent.frames[0] == this);\n" 519 + " alert(top.frames[0] == parent);\n" 520 + "}\n" 521 + "</script></head>" 522 + "<body><a id='clickme' onClick='doAlert()'>foo</a></body></html>"; 523 524 final WebClient webClient = new WebClient(); 525 final List collectedAlerts = new ArrayList(); 526 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 527 528 final MockWebConnection webConnection = new MockWebConnection( webClient ); 529 webConnection.setResponse(URL_FIRST, firstContent); 530 webConnection.setResponse(URL_SECOND, secondContent); 531 webConnection.setResponse(URL_THIRD, thirdContent); 532 533 webClient.setWebConnection( webConnection ); 534 535 final HtmlPage firstPage = (HtmlPage) webClient.getPage(URL_FIRST); 536 assertEquals( "First", firstPage.getTitleText() ); 537 538 final WebWindow innermostWebWindow = webClient.getWebWindowByName("innermost"); 539 final HtmlPage innermostPage = (HtmlPage)innermostWebWindow.getEnclosedPage(); 540 ((HtmlAnchor)innermostPage.getHtmlElementById("clickme")).click(); 541 542 assertNotSame(innermostWebWindow.getParentWindow(), innermostWebWindow); 543 assertNotSame(innermostWebWindow.getTopWindow(), innermostWebWindow); 544 assertNotSame(innermostWebWindow.getParentWindow(), 545 innermostWebWindow.getTopWindow()); 546 assertSame(innermostWebWindow.getParentWindow().getParentWindow(), 547 innermostWebWindow.getTopWindow()); 548 549 assertEquals( 550 Arrays.asList( new String[] {"true", "true", "true", "true", "true", "true"} ), 551 collectedAlerts); 552 } 553 554 555 558 public void testConfirm() throws Exception { 559 final WebClient webClient = new WebClient(); 560 final MockWebConnection webConnection = new MockWebConnection( webClient ); 561 final List collectedAlerts = new ArrayList(); 562 final List collectedConfirms = new ArrayList(); 563 564 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); 565 webClient.setConfirmHandler( new ConfirmHandler() { 566 public boolean handleConfirm( final Page page, final String message ) { 567 collectedConfirms.add(message); 568 return true; 569 } 570 } ); 571 572 final String firstContent 573 = "<html><head><title>First</title><script>function doTest(){alert(confirm('foo'))}</script>" 574 + "</head><body onload='doTest()'></body></html>"; 575 576 webConnection.setResponse(URL_FIRST, firstContent); 577 webClient.setWebConnection( webConnection ); 578 579 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 580 assertEquals( "First", firstPage.getTitleText() ); 581 582 assertEquals( Collections.singletonList("foo"), collectedConfirms ); 583 assertEquals( Collections.singletonList("true"), collectedAlerts ); 584 } 585 586 587 590 public void testConfirm_noConfirmHandler() throws Exception { 591 final WebClient webClient = new WebClient(); 592 final MockWebConnection webConnection = new MockWebConnection( webClient ); 593 final List collectedAlerts = new ArrayList(); 594 final List collectedConfirms = new ArrayList(); 595 596 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); 597 598 final String firstContent 599 = "<html><head><title>First</title><script>function doTest(){alert(confirm('foo'))}</script>" 600 + "</head><body onload='doTest()'></body></html>"; 601 602 webConnection.setResponse(URL_FIRST, firstContent); 603 webClient.setWebConnection( webConnection ); 604 605 getLog().warn("Warning for no confirm handler expected next"); 606 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 607 assertEquals( "First", firstPage.getTitleText() ); 608 609 assertEquals( Collections.EMPTY_LIST, collectedConfirms ); 610 assertEquals( Collections.singletonList("false"), collectedAlerts ); 611 } 612 613 614 617 public void testPrompt() throws Exception { 618 final WebClient webClient = new WebClient(); 619 final MockWebConnection webConnection = new MockWebConnection( webClient ); 620 final List collectedAlerts = new ArrayList(); 621 final List collectedPrompts = new ArrayList(); 622 623 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); 624 webClient.setPromptHandler( new PromptHandler() { 625 public String handlePrompt( final Page page, final String message ) { 626 collectedPrompts.add(message); 627 return "Flintstone"; 628 } 629 } ); 630 631 final String firstContent 632 = "<html><head><title>First</title><script>function doTest(){alert(prompt('foo'))}</script>" 633 + "</head><body onload='doTest()'></body></html>"; 634 635 webConnection.setResponse(URL_FIRST, firstContent); 636 webClient.setWebConnection( webConnection ); 637 638 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 639 assertEquals( "First", firstPage.getTitleText() ); 640 641 assertEquals( Collections.singletonList("foo"), collectedPrompts ); 642 assertEquals( Collections.singletonList("Flintstone"), collectedAlerts ); 643 } 644 645 646 649 public void testPrompt_noPromptHandler() throws Exception { 650 final WebClient webClient = new WebClient(); 651 final MockWebConnection webConnection = new MockWebConnection( webClient ); 652 final List collectedAlerts = new ArrayList(); 653 final List collectedPrompts = new ArrayList(); 654 655 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); 656 657 final String firstContent 658 = "<html><head><title>First</title><script>function doTest(){alert(prompt('foo'))}</script>" 659 + "</head><body onload='doTest()'></body></html>"; 660 661 webConnection.setResponse(URL_FIRST, firstContent); 662 webClient.setWebConnection( webConnection ); 663 getLog().warn("Warning for no prompt handler expected next"); 664 665 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 666 assertEquals( "First", firstPage.getTitleText() ); 667 668 assertEquals( Collections.EMPTY_LIST, collectedPrompts ); 669 assertEquals( Collections.singletonList("null"), collectedAlerts ); 670 } 671 672 673 676 public void testOpener() throws Exception { 677 final WebClient webClient = new WebClient(); 678 final MockWebConnection webConnection = new MockWebConnection( webClient ); 679 final List collectedAlerts = new ArrayList(); 680 681 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); 682 683 final String firstContent 684 = "<html><head><title>First</title><script>" 685 + "function runtest() {\n" 686 + " alert(window.opener)\n" 687 + " alert('one')\n" 688 + " open('http://second', 'foo')" 689 + "}\n" 690 + "function callAlert( text ) {\n" 691 + " alert(text)" 692 + "}\n" 693 + "</script></head><body onload='runtest()'>" 694 + "</body></html>"; 695 final String secondContent 696 = "<html><head><title>Second</title><script>" 697 + "function runtest() {\n" 698 + " opener.callAlert('two')\n" 699 + " document.form1.submit()\n" 700 + "}\n" 701 + "</script></head><body onload='runtest()'>" 702 + "<form name='form1' action='http://third'><input type='submit'></form>" 703 + "</body></html>"; 704 final String thirdContent 705 = "<html><head><title>Third</title><script>" 706 + "function runtest() {\n" 707 + " opener.callAlert('three')" 708 + "}\n" 709 + "</script></head><body onload='runtest()'>" 710 + "</body></html>"; 711 712 webConnection.setResponse(URL_FIRST, firstContent); 713 webConnection.setResponse(URL_SECOND, secondContent); 714 webConnection.setResponse(URL_THIRD, thirdContent); 715 webClient.setWebConnection( webConnection ); 716 717 final HtmlPage firstPage = ( HtmlPage )webClient.getPage(URL_FIRST); 718 assertEquals( "First", firstPage.getTitleText() ); 719 720 final List expectedAlerts = Arrays.asList( new String[]{ "null", "one", "two", "three" } ); 721 assertEquals( expectedAlerts, collectedAlerts ); 722 } 723 724 725 728 public void testSetTimeout() throws Exception { 729 final String content 730 = "<html><body><script language='JavaScript'>window.setTimeout('alert(\"Yo!\")',1);" 731 + "</script></body></html>"; 732 733 final List collectedAlerts = Collections.synchronizedList(new ArrayList()); 734 loadPage(content, collectedAlerts); 735 736 final int waitTime = 50; 737 final int maxTime = 1000; 738 for( int time = 0; time < maxTime; time+=waitTime ) { 739 if(!collectedAlerts.isEmpty()) { 740 assertEquals( Collections.singletonList("Yo!"), collectedAlerts ); 741 return; 742 } 743 Thread.sleep(waitTime); 744 } 745 fail("No alerts written within "+maxTime+"ms"); 746 } 747 748 749 753 public void testSetAndClearInterval() throws Exception { 754 final String content 755 = "<html><body>\n" 756 + "<script>\n" 757 + "window.setInterval('alert(\"Yo!\")', 500);" 758 + "function foo() { alert('Yo2'); }\n" 759 + "var i = window.setInterval(foo, 500);" 760 + "window.clearInterval(i);" 761 + "</script></body></html>"; 762 763 final List collectedAlerts = Collections.synchronizedList(new ArrayList()); 764 loadPage(content, collectedAlerts); 765 766 } 768 769 774 public void testSetTimeoutStopped() throws Exception { 775 final String firstContent 776 = "<html><head>" 777 + "<script language='JavaScript'>window.setTimeout('alert(\"Yo!\")', 1000);</script>" 778 + "</head><body onload='document.location.replace(\"http://second\")'></body></html>"; 779 final String secondContent = "<html><head><title>Second</title></head><body></body></html>"; 780 781 final WebClient webClient = new WebClient(); 782 final MockWebConnection webConnection = new MockWebConnection( webClient ); 783 final List collectedAlerts = Collections.synchronizedList(new ArrayList()); 784 785 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); 786 787 webConnection.setResponse(URL_FIRST, firstContent); 788 webConnection.setResponse(URL_SECOND, secondContent); 789 webClient.setWebConnection( webConnection ); 790 791 final HtmlPage page = (HtmlPage) webClient.getPage(URL_FIRST); 792 assertEquals("Second", page.getTitleText()); 793 794 Thread.sleep(2000); assertEquals(Collections.EMPTY_LIST, collectedAlerts); 796 } 797 798 801 public void testClearTimeout() throws Exception { 802 final String content = 803 "<html>\n" 804 + "<head>\n" 805 + " <title>test</title>\n" 806 + " <script>\n" 807 + " function test() {\n" 808 + " var id = setTimeout('doAlert()', 2000);\n" 809 + " clearTimeout(id);\n" 810 + " }\n" 811 + " function doAlert() {\n" 812 + " alert('blah');\n" 813 + " }\n" 814 + " </script>\n" 815 + "</head>\n" 816 + "<body onload='test()'>\n" 817 + "</body>\n" 818 + "</html>"; 819 820 final List collectedAlerts = Collections.synchronizedList(new ArrayList()); 821 loadPage(content, collectedAlerts); 822 Thread.sleep(2200); 823 assertEquals(Collections.EMPTY_LIST, collectedAlerts); 824 } 825 826 829 public void testAboutURL() throws Exception { 830 final WebClient webClient = new WebClient(); 831 final MockWebConnection webConnection = 832 new MockWebConnection(webClient); 833 final String firstContent = 834 "<html><body><script language='JavaScript'>" 835 + "w2 = window.open('about:blank', 'AboutBlank');" 836 + "w2.document.open();" 837 + "w2.document.write('<html><head><title>hello</title></head><body></body></html>');" 838 + "w2.document.close();" 839 + "</script></body></html>"; 840 webConnection.setResponse(URL_FIRST, firstContent); 841 webClient.setWebConnection(webConnection); 842 843 webClient.getPage(URL_FIRST); 844 final WebWindow webWindow = webClient.getWebWindowByName("AboutBlank"); 845 assertNotNull(webWindow); 846 847 } 850 851 855 public void testWindowFrames() throws Exception { 856 final String firstContent = 857 "<html><body><script language='JavaScript'>" 858 + "if (typeof top.frames['anyXXXname'] == 'undefined') {" 859 + "alert('one')};" 860 + "</script></body></html>"; 861 862 final List collectedAlerts = new ArrayList(); 863 loadPage(firstContent, collectedAlerts); 864 assertEquals(1, collectedAlerts.size()); 865 } 866 867 871 public void testWindowFramesLive() throws Exception { 872 873 final String content = 874 "<html>" 875 + "<script>" 876 + "var oFrames = window.frames;" 877 + "alert(oFrames.length);" 878 + "function test()" 879 + "{" 880 + " alert(oFrames.length);" 881 + " alert(window.frames.length);" 882 + " alert(oFrames == window.frames);" 883 + "}" 884 + "</script>" 885 + "<frameset rows='50,*' onload='test()'>" 886 + "<frame SRC='about:blank'/>" 887 + "<frame SRC='about:blank'/>" 888 + "</frameset>" 889 + "</html>"; 890 891 final List expectedAlerts = Arrays.asList( new String[]{ "0", "2", "2", "true" } ); 892 893 final List collectedAlerts = new ArrayList(); 894 loadPage(content, collectedAlerts); 895 896 897 assertEquals(expectedAlerts, collectedAlerts); 898 } 899 900 905 public void testJavascriptVariableFromWindow() throws Exception { 906 final String firstContent = 907 "<html><head><title>first</title></head><body><script>\n" 908 + "myVariable = 'foo';\n" 909 + "alert(window.myVariable);\n" 910 + "</script></body></head>"; 911 912 final List collectedAlerts = new ArrayList(); 913 final HtmlPage page = loadPage(firstContent, collectedAlerts); 914 assertEquals( Collections.singletonList("foo"), collectedAlerts ); 915 assertEquals( "first", page.getTitleText() ); 916 } 917 918 923 public void testJavascriptVariableFromTopAndParentFrame() throws Exception { 924 final WebClient webClient = new WebClient(); 925 final MockWebConnection webConnection = new MockWebConnection( webClient ); 926 final List collectedAlerts = new ArrayList(); 927 928 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); 929 930 final String firstContent 931 = "<html><head><title>First</title></head><body><script>" 932 + "myVariable = 'first'" 933 + " </script><iframe name='left' SRC='http://second' />" 934 + "</body></html>"; 935 webConnection.setResponse(URL_FIRST, firstContent); 936 937 final String secondContent 938 = "<html><head><title>Second</title></head><body><script>" 939 + "myVariable = 'second'" 940 + " </script><iframe name='innermost' SRC='http://third' />" 941 + "</body></html>"; 942 webConnection.setResponse(URL_SECOND, secondContent); 943 944 final String thirdContent 945 = "<html><head><title>Third</title><script>" 946 + "myVariable = 'third';\n" 947 + "function doTest() {\n" 948 + "alert('parent.myVariable = ' + parent.myVariable);\n" 949 + "alert('top.myVariable = ' + top.myVariable);\n" 950 + "}\n" 951 + "</script></head>" 952 + "<body onload='doTest()'></body></html>"; 953 954 webConnection.setResponse(URL_THIRD, thirdContent); 955 956 webClient.setWebConnection( webConnection ); 957 958 final HtmlPage page = (HtmlPage)webClient.getPage(URL_FIRST); 959 assertEquals( "First", page.getTitleText() ); 960 961 final List expectedAlerts = Arrays.asList( new String[]{ 962 "parent.myVariable = second", 963 "top.myVariable = first", 964 } ); 965 assertEquals( expectedAlerts, collectedAlerts ); 966 } 967 972 public void testJavascriptVariableFromNamedFrame() throws Exception { 973 final WebClient webClient = new WebClient(); 974 final MockWebConnection webConnection = new MockWebConnection(webClient); 975 final List collectedAlerts = new ArrayList(); 976 977 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); 978 979 final String firstContent 980 = "<html><head><title>first</title></head>" 981 + "<frameset cols='20%,80%'>" 982 + " <frameset rows='30%,70%'>" 983 + " <frame SRC='http://second' name='second'>" 984 + " <frame SRC='http://third' name='third'>" 985 + " </frameset>" 986 + " <frame SRC='http://fourth' name='fourth'>" 987 + "</frameset></html>"; 988 webConnection.setResponse( URL_FIRST, firstContent); 989 990 final String secondContent 991 = "<html><head><title>second</title></head><body><script>" 992 + "myVariable = 'second';\n" 993 + "</script><p>second</p></body></html>"; 994 webConnection.setResponse( URL_SECOND, secondContent); 995 996 final String thirdContent 997 = "<html><head><title>third</title></head><body><script>" 998 + "myVariable = 'third';\n" 999 + "</script><p>third</p></body></html>"; 1000 webConnection.setResponse( URL_THIRD, thirdContent); 1001 1002 final String fourthContent 1003 = "<html><head><title>fourth</title></head><body onload='doTest()'><script>\n" 1004 + "myVariable = 'fourth';\n" 1005 + "function doTest() {\n" 1006 + "alert('parent.second.myVariable = ' + parent.second.myVariable);\n" 1007 + "alert('parent.third.myVariable = ' + parent.third.myVariable);\n" 1008 + "}\n" 1009 + "</script></body></html>"; 1010 webConnection.setResponse( new URL("http://fourth"), fourthContent); 1011 1012 webClient.setWebConnection(webConnection); 1013 1014 final HtmlPage page = (HtmlPage)webClient.getPage(URL_FIRST); 1015 assertEquals( "first", page.getTitleText() ); 1016 1017 final List expectedAlerts = Arrays.asList( new String[]{ 1018 "parent.second.myVariable = second", 1019 "parent.third.myVariable = third", 1020 } ); 1021 assertEquals( expectedAlerts, collectedAlerts ); 1022 } 1023 1024 1028 public void testJavascriptVariableFromWindow_NotFound() throws Exception { 1029 final String firstContent = 1030 "<html><head><title>first</title></head><body><script>\n" 1031 + "myVariable = 'foo';\n" 1032 + "alert(window.myOtherVariable == null);\n" 1033 + "</script></body></head>"; 1034 1035 final List collectedAlerts = new ArrayList(); 1036 final HtmlPage page = loadPage(firstContent, collectedAlerts); 1037 assertEquals( Collections.singletonList("true"), collectedAlerts ); 1038 assertEquals( "first", page.getTitleText() ); 1039 } 1040 1041 1045 public void testGetFrameByName() throws Exception { 1046 final WebClient webClient = new WebClient(); 1047 final MockWebConnection webConnection = 1048 new MockWebConnection(webClient); 1049 final List collectedAlerts = new ArrayList(); 1050 1051 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); 1052 1053 final String firstContent 1054 = "<html><head><title>first</title></head>" 1055 + "<frameset cols='20%,80%'>" 1056 + " <frameset rows='30%,70%'>" 1057 + " <frame SRC='http://second' name='second'>" 1058 + " <frame SRC='http://third' name='third'>" 1059 + " </frameset>" 1060 + " <frame SRC='http://fourth' name='fourth'>" 1061 + "</frameset></html>"; 1062 webConnection.setResponse( URL_FIRST, firstContent); 1063 1064 final String secondContent 1065 = "<html><head><title>second</title></head><body><p>second</p></body></html>"; 1066 webConnection.setResponse( URL_SECOND, secondContent); 1067 1068 final String thirdContent 1069 = "<html><head><title>third</title></head><body><p>third</p></body></html>"; 1070 webConnection.setResponse( URL_THIRD, thirdContent); 1071 1072 final String fourthContent 1073 = "<html><head><title>fourth</title></head><body onload='doTest()'><script>\n" 1074 + "function doTest() {\n" 1075 + "alert('fourth-second='+parent.second.document.location);\n" 1076 + "alert('fourth-third='+parent.third.document.location);\n" 1077 + "}\n" 1078 + "</script></body></html>"; 1079 webConnection.setResponse( new URL("http://fourth"), fourthContent); 1080 1081 webClient.setWebConnection(webConnection); 1082 1083 final HtmlPage page = (HtmlPage)webClient.getPage(URL_FIRST); 1084 assertEquals( "first", page.getTitleText() ); 1085 1086 final List expectedAlerts = Arrays.asList( new String[]{ 1087 "fourth-second=http://second", 1088 "fourth-third=http://third", 1089 } ); 1090 assertEquals( expectedAlerts, collectedAlerts ); 1091 } 1092 1093 1096 public void testSetOpenerLocationHrefRelative() throws Exception { 1097 if (true) { 1098 notImplemented(); 1099 return; 1100 } 1101 final WebClient webClient = new WebClient(); 1102 final MockWebConnection webConnection = new MockWebConnection( webClient ); 1103 1104 final String aContent 1105 = "<html><head><title>A</title></head><body>" 1106 + "<button id='clickme' onClick='window.open(\"b/b.html\");'>Click me</a>" 1107 + "</body></html>"; 1108 final String bContent 1109 = "<html><head><title>B</title></head><body>" 1110 + "<button id='clickme' onClick='opener.location.href=\"../c.html\";'>Click me</a>" 1111 + "</body></html>"; 1112 final String cContent 1113 = "<html><head><title>C</title></head><body></body></html>"; 1114 final String failContent 1115 = "<html><head><title>FAILURE!!!</title></head><body></body></html>"; 1116 1117 webConnection.setResponse(new URL("http://opener/test/a.html"), aContent); 1118 webConnection.setResponse(new URL("http://opener/test/b/b.html"), bContent); 1119 webConnection.setResponse(new URL("http://opener/test/c.html"), cContent); 1120 webConnection.setResponse(new URL("http://opener/c.html"), failContent); 1121 1122 webClient.setWebConnection( webConnection ); 1123 1124 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( 1125 new URL("http://opener/test/a.html")); 1126 assertEquals( "A", firstPage.getTitleText() ); 1127 1128 final HtmlButton buttonA = (HtmlButton)firstPage.getHtmlElementById("clickme"); 1129 final HtmlPage secondPage = (HtmlPage)buttonA.click(); 1130 assertNotNull("B", secondPage); 1131 assertEquals( "B", secondPage.getTitleText() ); 1132 1133 final HtmlButton buttonB = (HtmlButton)secondPage.getHtmlElementById("clickme"); 1134 final HtmlPage thirdPage = (HtmlPage)buttonB.click(); 1135 assertNotNull("C", thirdPage); 1136 assertEquals( "C", thirdPage.getTitleText() ); 1137 } 1138 1139 1143 public void testClose() throws Exception { 1144 1145 final WebClient webClient = new WebClient(); 1146 final MockWebConnection webConnection = new MockWebConnection(webClient); 1147 1148 final List collectedAlerts = new ArrayList(); 1149 webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); 1150 1151 final String firstContent 1152 = "<html><head><title>First</title></head><body>" 1153 + "<a HREF='" + URL_SECOND + "' id='link' target='_blank'>Link</a>" 1154 + "</body></html>"; 1155 final String secondContent 1156 = "<html><head><title>Second</title></head><body>" 1157 + "<h1>Second</h1><form>" 1158 + "<input type='submit' name='action' value='Close' id='button' " 1159 + "onclick='window.close(); return false;'>" 1160 + "</form></body></html>"; 1161 1162 webConnection.setResponse(URL_FIRST, firstContent); 1163 webConnection.setResponse(URL_SECOND, secondContent); 1164 webClient.setWebConnection(webConnection); 1165 1166 final HtmlPage firstPage = (HtmlPage) webClient.getPage(URL_FIRST); 1167 assertEquals("First", firstPage.getTitleText()); 1168 assertEquals(1, webClient.getWebWindows().size()); 1169 final WebWindow firstWindow = firstPage.getEnclosingWindow(); 1170 1171 final HtmlPage secondPage = (HtmlPage) ((HtmlAnchor) firstPage.getHtmlElementById("link")).click(); 1172 assertEquals("Second", secondPage.getTitleText()); 1173 assertEquals(2, webClient.getWebWindows().size()); 1174 final WebWindow secondWindow = secondPage.getEnclosingWindow(); 1175 1176 assertNotSame(firstWindow, secondWindow); 1177 1178 final EventCatcher eventCatcher = new EventCatcher(); 1179 eventCatcher.listenTo(webClient); 1180 ((HtmlSubmitInput) secondPage.getHtmlElementById("button")).click(); 1181 1182 final List expectedEvents = Arrays.asList(new Object[]{ 1183 new WebWindowEvent(secondWindow, WebWindowEvent.CLOSE, secondPage, null) 1184 }); 1185 assertEquals(expectedEvents, eventCatcher.getEvents()); 1186 1187 assertEquals(1, webClient.getWebWindows().size()); 1188 assertEquals(firstWindow, webClient.getCurrentWindow()); 1189 1190 assertEquals(Collections.EMPTY_LIST, collectedAlerts); 1191 } 1192 1193 1197 public void testFramesLengthZero() throws Exception { 1198 final String content 1199 = "<html><head><title>foo</title><script>" 1200 + "alert(window.frames.length)" 1201 + "</script></head><body>" 1202 + "</body></html>"; 1203 final List collectedAlerts = new ArrayList(); 1204 loadPage(content, collectedAlerts); 1205 1206 final List expectedAlerts = Arrays.asList( new String[]{ 1207 "0" 1208 }); 1209 assertEquals( expectedAlerts, collectedAlerts ); 1210 } 1211 1212 1217 public void testFramesLengthAndFrameAccess() throws Exception { 1218 1219 final String content = 1220 "<html>" 1221 + "<script>" 1222 + "function test()" 1223 + "{" 1224 + " alert(window.frames.length);" 1225 + " alert(window.frames[0].name);" 1226 + " alert(window.frames.frame2.name);" 1227 + "}" 1228 + "</script>" 1229 + "<frameset rows='50,*' onload='test()'>" 1230 + "<frame name='frame1' SRC='about:blank'/>" 1231 + "<frame name='frame2' SRC='about:blank'/>" 1232 + "</frameset>" 1233 + "</html>"; 1234 1235 final List collectedAlerts = new ArrayList(); 1236 loadPage(content, collectedAlerts); 1237 1238 final List expectedAlerts = Arrays.asList( new String[]{ "2", "frame1", "frame2"} ); 1239 assertEquals(expectedAlerts, collectedAlerts); 1240 } 1241 1242 1247 public void testMoveTo() throws Exception { 1248 final String content 1249 = "<html><head><title>foo</title><script>" 1250 + "window.moveTo(10, 20)" 1251 + "</script></head><body>" 1252 + "</body></html>"; 1253 loadPage(content); 1254 } 1255 1256 1261 public void testMoveBy() throws Exception { 1262 final String content 1263 = "<html><head><title>foo</title><script>" 1264 + "window.moveBy(10, 20)" 1265 + "</script></head><body>" 1266 + "</body></html>"; 1267 loadPage(content); 1268 } 1269 1270 1275 public void testResizeTo() throws Exception { 1276 final String content = "<html><head><title>foo</title><script>\n" 1277 + "window.resizeTo(10, 20);\n" 1278 + "window.resizeTo(-10, 20);\n" 1279 + "</script></head><body></body></html>"; 1280 loadPage(content); 1281 } 1282 1283 1288 public void testScroll() throws Exception { 1289 final String content 1290 = "<html><head><title>foo</title><script>" 1291 + "window.scroll(10, 20);" 1292 + "</script></head><body>" 1293 + "</body></html>"; 1294 loadPage(content); 1295 } 1296 1297 1302 public void testScrollBy() throws Exception { 1303 final String content 1304 = "<html><head><title>foo</title><script>" 1305 + "window.scrollBy(10, 20);" 1306 + "</script></head><body>" 1307 + "</body></html>"; 1308 loadPage(content); 1309 } 1310 1311 1316 public void testScrollByLines() throws Exception { 1317 final String content 1318 = "<html><head><title>foo</title><script>" 1319 + "window.scrollByLines(2);" 1320 + "</script></head><body>" 1321 + "</body></html>"; 1322 loadPage(content); 1323 } 1324 1325 1330 public void testScrollByPages() throws Exception { 1331 final String content 1332 = "<html><head><title>foo</title><script>" 1333 + "window.scrollByPages(2);" 1334 + "</script></head><body>" 1335 + "</body></html>"; 1336 loadPage(content); 1337 } 1338 1339 1344 public void testScrollTo() throws Exception { 1345 final String content 1346 = "<html><head><title>foo</title><script>" 1347 + "window.scrollTo(10, 20);" 1348 + "</script></head><body>" 1349 + "</body></html>"; 1350 loadPage(content); 1351 } 1352 1353 1361 public void testElementByNameFromWindow() throws Exception { 1362 final String content = "<html>\n" + 1363 "<head><title>test</title>\n" + 1364 "<script>\n" + 1365 " function test() {\n" + 1366 " alert(window.form1.name);\n" + 1367 " alert(form2.name);\n" + 1368 " alert(window.input1.length);\n" + 1369 " alert(input2[1].value);\n" + 1370 " }\n" + 1371 "</script>\n" + 1372 "</head>\n" + 1373 "<body onload='test()'>\n" + 1374 "<form name='form1'></form>\n" + 1375 "<form name='form2'></form>\n" + 1376 "<input type='text' name='input1' value='1'/>\n" + 1377 "<input type='text' name='input1' value='2'/>\n" + 1378 "<input type='text' name='input2' value='3'/>\n" + 1379 "<input type='text' name='input2' value='4'/>\n" + 1380 "</body>\n" + 1381 "</html>\n"; 1382 final List collectedAlerts = new ArrayList(); 1383 final List expectedAlerts = Arrays.asList( new String[]{"form1", "form2", "2", "4"} ); 1384 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 1385 loadPage(content, collectedAlerts); 1386 assertEquals(expectedAlerts, collectedAlerts); 1387 } 1388 1389 1394 public void testElementByIdFromWindow() throws Exception { 1395 final String content = "<html>\n" + 1396 "<head><title>test</title>\n" + 1397 "<script>\n" + 1398 " function test() {\n" + 1399 " alert(window.form1Id.name);\n" + 1400 " alert(form2Id.name);\n" + 1401 " alert(window.input1Id.value);\n" + 1402 " alert(myDiv.tagName);\n" + 1403 " }\n" + 1404 "</script>\n" + 1405 "</head>\n" + 1406 "<body onload='test()'>\n" + 1407 "<div id='myDiv'>\n" + 1408 "<form name='form1' id='form1Id'></form>\n" + 1409 "</div>\n" + 1410 "<form name='form2' id='form2Id'></form>\n" + 1411 "<input type='text' name='input1' id='input1Id' value='1'/>\n" + 1412 "</form>\n" + 1413 "</body>\n" + 1414 "</html>\n"; 1415 final List collectedAlerts = new ArrayList(); 1416 final List expectedAlerts = Arrays.asList( new String[]{"form1", "form2", "1", "DIV"} ); 1417 loadPage(content, collectedAlerts); 1418 assertEquals(expectedAlerts, collectedAlerts); 1419 } 1420 1421 1425 public void testExecScript() throws Exception { 1426 final String content = "<html>\n" 1427 + "<head><title>test</title>\n" 1428 + "<script>\n" 1429 + " function test()\n" 1430 + " {\n" 1431 + " window.execScript('alert(\"JavaScript\")', 'JavaScript');\n" 1432 + " window.execScript('alert(\"JScript\")', 'JScript');\n" 1433 + " window.execScript('alert(\"VBScript\")', 'VBScript');\n" 1434 + " try {\n" 1435 + " window.execScript('alert(\"BadLanguage\")', 'BadLanguage');\n" 1436 + " }\n" 1437 + " catch(e) {\n" 1438 + " alert(e.message);\n" 1439 + " }\n" 1440 + " }\n" 1441 + "</script>\n" 1442 + "</head>\n" 1443 + "<body onload='test()'>\n" 1444 + " <div id='div1'>blah</div>\n" 1445 + "</body>\n" 1446 + "</html>\n"; 1447 final List expectedAlerts = Arrays.asList( new String[]{"JavaScript", "JScript", "Invalid class string"} ); 1448 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 1449 final List collectedAlerts = new ArrayList(); 1450 loadPage(content, collectedAlerts); 1451 assertEquals(expectedAlerts, collectedAlerts); 1452 } 1453 1454 1457 public void testOnLoadFunction() throws Exception { 1458 final String content = "<html>\n" 1459 + "<head><title>test</title>\n" 1460 + "<script>\n" 1461 + " function test()\n" 1462 + " {\n" 1463 + " alert('test');\n" 1464 + " }\n" 1465 + "</script>\n" 1466 + "</head>\n" 1467 + "<body onload='test()'>\n" 1468 + "<script>\n" 1469 + " var oldOnLoad = window.onload;;\n" 1470 + " window.onload = test2;\n" 1471 + " function test2()\n" 1472 + " {\n" 1473 + " alert('test2');\n" 1474 + " oldOnLoad();\n" 1475 + " }\n" 1476 + "</script>\n" 1477 + "</body>\n" 1478 + "</html>\n"; 1479 final List expectedAlerts = Arrays.asList( new String[]{"test2", "test"} ); 1480 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 1481 final List collectedAlerts = new ArrayList(); 1482 loadPage(content, collectedAlerts); 1483 assertEquals(expectedAlerts, collectedAlerts); 1484 } 1485 1486 1489 public void testStatus() throws Exception { 1490 final WebClient webClient = new WebClient(); 1491 final MockWebConnection webConnection = new MockWebConnection( webClient ); 1492 1493 final String firstContent 1494 = "<html><head><title>First</title><script>" 1495 + "function doTest() {\n" 1496 + " alert(window.status);\n" 1497 + " window.status = 'newStatus';\n" 1498 + " alert(window.status);\n" 1499 + "}\n" 1500 + "</script></head><body onload='doTest()'>" 1501 + "</body></html>"; 1502 1503 final URL url = URL_FIRST; 1504 webConnection.setResponse(url, firstContent); 1505 webClient.setWebConnection( webConnection ); 1506 1507 final List collectedAlerts = new ArrayList(); 1508 webClient.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 1509 1510 final List collectedStatus = new ArrayList(); 1511 webClient.setStatusHandler( new StatusHandler() { 1512 public void statusMessageChanged( final Page page, final String message ) { 1513 collectedStatus.add(message); 1514 } 1515 }); 1516 final HtmlPage firstPage = ( HtmlPage )webClient.getPage( URL_FIRST ); 1517 assertEquals( "First", firstPage.getTitleText() ); 1518 1519 final List expectedAlerts = Arrays.asList( new String[] { "", "newStatus"} ); 1520 assertEquals( "alerts", expectedAlerts, collectedAlerts ); 1521 1522 final List expectedStatus = Arrays.asList( new String[] { "newStatus"} ); 1523 assertEquals( "status", expectedStatus, collectedStatus ); 1524 } 1525 1526 1527 1532 public void testWindowName() throws Exception { 1533 final String windowName = "main"; 1534 final String content = "<html>\n" 1535 + "<head><title>window.name test</title></head>\n" 1536 + "<body>\n" 1537 + "<script>\n" 1538 + "alert('window.name before: ' + window.name);\n" 1539 + "window.name = '" + windowName + "';\n" 1540 + "alert('window.name after: ' + window.name);\n" 1541 + "</script>\n" 1542 + "</body>\n" 1543 + "</html>\n"; 1544 final List expectedAlerts = 1545 Arrays.asList( new String[]{"window.name before: ", "window.name after: " + windowName} ); 1546 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 1547 final List collectedAlerts = new ArrayList(); 1548 loadPage(content, collectedAlerts); 1549 assertEquals(expectedAlerts, collectedAlerts); 1550 } 1551} 1552 | Popular Tags |