1 38 package com.gargoylesoftware.htmlunit.html; 39 40 import java.util.ArrayList; 41 import java.util.Arrays; 42 import java.util.Collections; 43 import java.util.List; 44 import java.util.ListIterator; 45 46 import com.gargoylesoftware.htmlunit.CollectingAlertHandler; 47 import com.gargoylesoftware.htmlunit.ElementNotFoundException; 48 import com.gargoylesoftware.htmlunit.KeyValuePair; 49 import com.gargoylesoftware.htmlunit.MockWebConnection; 50 import com.gargoylesoftware.htmlunit.SubmitMethod; 51 import com.gargoylesoftware.htmlunit.WebClient; 52 import com.gargoylesoftware.htmlunit.WebTestCase; 53 import com.gargoylesoftware.htmlunit.WebWindow; 54 import com.gargoylesoftware.htmlunit.WebRequestSettings; 55 56 64 public class HtmlFormTest extends WebTestCase { 65 70 public HtmlFormTest( final String name ) { 71 super( name ); 72 } 73 74 75 80 public void testSetSelectedRadioButton_ValueExists() 81 throws Exception { 82 final String htmlContent 83 = "<html><head><title>foo</title></head><body>" 84 + "<form id='form1'>" 85 + "<input type='radio' name='foo' value='1' selected='selected' id='input1'/>" 86 + "<input type='radio' name='foo' value='2' id='input2'/>" 87 + "<input type='radio' name='foo' value='3'/>" 88 + "<input type='submit' name='button' value='foo'/>" 89 + "</form></body></html>"; 90 final HtmlPage page = loadPage(htmlContent); 91 final MockWebConnection webConnection = getMockConnection(page); 92 93 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 94 95 final HtmlSubmitInput pushButton = ( HtmlSubmitInput )form.getInputByName( "button" ); 96 97 form.setCheckedRadioButton( "foo", "2" ); 98 99 assertFalse( ((HtmlRadioButtonInput)page.getHtmlElementById("input1")).isChecked() ); 100 assertTrue( ((HtmlRadioButtonInput)page.getHtmlElementById("input2")).isChecked() ); 101 102 final HtmlPage secondPage = ( HtmlPage )pushButton.click(); 104 105 final List expectedParameters = new ArrayList(); 106 expectedParameters.add( new KeyValuePair( "foo", "2" ) ); 107 expectedParameters.add( new KeyValuePair( "button", "foo" ) ); 108 109 assertEquals( "url", URL_GARGOYLE, secondPage.getWebResponse().getUrl() ); 110 assertEquals( "method", SubmitMethod.GET, webConnection.getLastMethod() ); 111 assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() ); 112 assertNotNull( secondPage ); 113 } 114 115 116 121 public void testSetSelectedRadioButton_ValueDoesNotExist_DoNotForceSelection() 122 throws Exception { 123 final String htmlContent 124 = "<html><head><title>foo</title></head><body>" 125 + "<form id='form1'>" 126 + "<input type='radio' name='foo' value='1' selected='selected'/>" 127 + "<input type='radio' name='foo' value='2'/>" 128 + "<input type='radio' name='foo' value='3'/>" 129 + "<input type='submit' name='button' value='foo'/>" 130 + "</form></body></html>"; 131 final HtmlPage page = loadPage(htmlContent); 132 133 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 134 135 final HtmlInput pushButton = form.getInputByName( "button" ); 136 assertNotNull(pushButton); 137 138 try { 139 form.setCheckedRadioButton( "foo", "4" ); 140 fail( "Expected foo" ); 141 } 142 catch( final ElementNotFoundException e ) { 143 } 145 } 146 147 148 153 public void testSetSelectedRadioButton_ValueDoesNotExist_ForceSelection() 154 throws Exception { 155 final String htmlContent 156 = "<html><head><title>foo</title></head><body>" 157 + "<form id='form1'>" 158 + "<input type='radio' name='foo' value='1' selected='selected'/>" 159 + "<input type='radio' name='foo' value='2'/>" 160 + "<input type='radio' name='foo' value='3'/>" 161 + "<input type='submit' name='button' value='foo'/>" 162 + "</form></body></html>"; 163 final HtmlPage page = loadPage(htmlContent); 164 final MockWebConnection webConnection = getMockConnection(page); 165 166 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 167 168 final HtmlSubmitInput pushButton = ( HtmlSubmitInput )form.getInputByName( "button" ); 169 170 form.fakeCheckedRadioButton( "foo", "4" ); 171 172 final HtmlPage secondPage = ( HtmlPage )pushButton.click(); 174 175 final List expectedParameters = new ArrayList(); 176 expectedParameters.add( new KeyValuePair( "button", "foo" ) ); 177 expectedParameters.add( new KeyValuePair( "foo", "4" ) ); 178 179 assertEquals( "url", URL_GARGOYLE, 180 secondPage.getWebResponse().getUrl() ); 181 assertEquals( "method", SubmitMethod.GET, webConnection.getLastMethod() ); 182 assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() ); 183 assertNotNull( secondPage ); 184 } 185 186 187 190 public void testSubmit_String() 191 throws Exception { 192 final String htmlContent 193 = "<html><head><title>foo</title></head><body>" 194 + "<form id='form1'>" 195 + "<input type='submit' name='button' value='foo'/>" 196 + "</form></body></html>"; 197 final HtmlPage page = loadPage(htmlContent); 198 199 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 200 201 form.submit( "button" ); 203 } 204 205 206 209 public void testSubmit_ExtraParameters() 210 throws Exception { 211 final String htmlContent 212 = "<html><head><title>foo</title></head><body>" 213 + "<form id='form1'>" 214 + " <input type='text' name='textfield' value='*'/>" 215 + " <input type='submit' name='button' value='foo'/>" 216 + "</form></body></html>"; 217 final HtmlPage page = loadPage(htmlContent); 218 final MockWebConnection webConnection = getMockConnection(page); 219 220 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 221 222 final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button"); 223 button.click(); 224 225 final List expectedParameters = Arrays.asList( new Object[]{ 226 new KeyValuePair("textfield", "*"), new KeyValuePair("button", "foo") 227 } ); 228 final List collectedParameters = webConnection.getLastParameters(); 229 230 assertEquals( expectedParameters, collectedParameters ); 231 } 232 233 234 237 public void testSubmit_onSubmitHandler() 238 throws Exception { 239 240 final String firstContent 241 = "<html><head><title>First</title></head><body>" 242 + "<form method='get' action='http://second' onSubmit='alert(\"clicked\")'>" 243 + "<input name='button' type='submit' value='PushMe' id='button'/></form>" 244 + "</body></html>"; 245 final String secondContent = "<html><head><title>Second</title></head><body></body></html>"; 246 247 final WebClient client = new WebClient(); 248 final List collectedAlerts = new ArrayList(); 249 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 250 251 final MockWebConnection webConnection = new MockWebConnection( client ); 252 webConnection.setResponse(URL_FIRST, firstContent); 253 webConnection.setResponse(URL_SECOND, secondContent); 254 255 client.setWebConnection( webConnection ); 256 257 final HtmlPage firstPage = ( HtmlPage )client.getPage(URL_FIRST); 258 final HtmlSubmitInput button = (HtmlSubmitInput)firstPage.getHtmlElementById("button"); 259 260 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 261 final HtmlPage secondPage = (HtmlPage)button.click(); 262 assertEquals( "Second", secondPage.getTitleText() ); 263 264 assertEquals( Collections.singletonList("clicked"), collectedAlerts ); 265 } 266 267 268 271 public void testSubmit_onSubmitHandler_returnFalse() 272 throws Exception { 273 274 final String firstContent 275 = "<html><head><title>First</title></head><body>" 276 + "<form method='get' action='http://second' " 277 + "onSubmit='alert(\"clicked\");return false;'>" 278 + "<input name='button' type='submit' value='PushMe' id='button'/></form>" 279 + "</body></html>"; 280 final String secondContent = "<html><head><title>Second</title></head><body></body></html>"; 281 282 final WebClient client = new WebClient(); 283 final List collectedAlerts = new ArrayList(); 284 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 285 286 final MockWebConnection webConnection = new MockWebConnection( client ); 287 webConnection.setResponse(URL_FIRST, firstContent); 288 webConnection.setResponse(URL_SECOND, secondContent); 289 290 client.setWebConnection( webConnection ); 291 292 final HtmlPage firstPage = ( HtmlPage )client.getPage(URL_FIRST); 293 final HtmlSubmitInput button = (HtmlSubmitInput)firstPage.getHtmlElementById("button"); 294 295 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 296 final HtmlPage secondPage = (HtmlPage)button.click(); 297 assertEquals( firstPage.getTitleText(), secondPage.getTitleText() ); 298 299 assertEquals( Collections.singletonList("clicked"), collectedAlerts ); 300 } 301 302 303 306 public void testSubmit_onSubmitHandler_javascriptDisabled() throws Exception { 307 308 final String firstContent 309 = "<html><head><title>First</title></head><body>" 310 + "<form method='get' action='http://second' onSubmit='alert(\"clicked\")'>" 311 + "<input name='button' type='submit' value='PushMe' id='button'/></form>" 312 + "</body></html>"; 313 final String secondContent = "<html><head><title>Second</title></head><body></body></html>"; 314 315 final WebClient client = new WebClient(); 316 client.setJavaScriptEnabled(false); 317 318 final List collectedAlerts = new ArrayList(); 319 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 320 321 final MockWebConnection webConnection = new MockWebConnection( client ); 322 webConnection.setResponse(URL_FIRST, firstContent); 323 webConnection.setResponse(URL_SECOND, secondContent); 324 325 client.setWebConnection( webConnection ); 326 327 final HtmlPage firstPage = ( HtmlPage )client.getPage(URL_FIRST); 328 final HtmlSubmitInput button = (HtmlSubmitInput)firstPage.getHtmlElementById("button"); 329 330 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 331 final HtmlPage secondPage = (HtmlPage)button.click(); 332 assertEquals( "First", firstPage.getTitleText() ); 333 assertEquals( "Second", secondPage.getTitleText() ); 334 335 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 336 } 337 338 339 342 public void testSubmit_javascriptAction() throws Exception { 343 344 final String firstContent 345 = "<html><head><title>First</title></head><body>" 346 + "<form method='get' action='javascript:alert(\"clicked\")'>" 347 + "<input name='button' type='submit' value='PushMe' id='button'/></form>" 348 + "</body></html>"; 349 final String secondContent = "<html><head><title>Second</title></head><body></body></html>"; 350 351 final WebClient client = new WebClient(); 352 final List collectedAlerts = new ArrayList(); 353 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 354 355 final MockWebConnection webConnection = new MockWebConnection( client ); 356 webConnection.setResponse(URL_FIRST, firstContent); 357 webConnection.setResponse(URL_SECOND, secondContent); 358 359 client.setWebConnection( webConnection ); 360 361 final HtmlPage firstPage = ( HtmlPage )client.getPage(URL_FIRST); 362 final HtmlSubmitInput button = (HtmlSubmitInput)firstPage.getHtmlElementById("button"); 363 364 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 365 final HtmlPage secondPage = (HtmlPage)button.click(); 366 assertEquals( firstPage.getTitleText(), secondPage.getTitleText() ); 367 368 assertEquals( Collections.singletonList("clicked"), collectedAlerts ); 369 } 370 371 372 375 public void testSubmit_javascriptAction_javascriptDisabled() throws Exception { 376 377 final String firstContent 378 = "<html><head><title>First</title></head><body>" 379 + "<form method='get' action='javascript:alert(\"clicked\")'>" 380 + "<input name='button' type='submit' value='PushMe' id='button'/></form>" 381 + "</body></html>"; 382 383 final WebClient client = new WebClient(); 384 client.setJavaScriptEnabled(false); 385 386 final List collectedAlerts = new ArrayList(); 387 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 388 389 final MockWebConnection webConnection = new MockWebConnection( client ); 390 webConnection.setResponse(URL_FIRST, firstContent); 391 392 client.setWebConnection( webConnection ); 393 394 final HtmlPage firstPage = ( HtmlPage )client.getPage(URL_FIRST); 395 final HtmlSubmitInput button = (HtmlSubmitInput)firstPage.getHtmlElementById("button"); 396 397 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 398 final HtmlPage secondPage = (HtmlPage)button.click(); 399 assertSame( firstPage, secondPage ); 400 } 401 402 403 407 public void testSubmitRadioButton() throws Exception { 408 final String htmlContent 409 = "<html><body><form method='POST' action='http://first'>" 410 + "<table><tr> <td ><input type='radio' name='name1' value='foo'> " 411 + "Option 1</td> </tr>" 412 + "<tr> <td ><input type='radio' name='name1' value='bar' checked >" 413 + "Option 2</td> </tr>" 414 + "<tr> <td ><input type='radio' name='name1' value='baz'> Option 3</td> </tr>" 415 + "</table><input type='submit' value='Login' name='loginButton1'></form>" 416 + "</body></html>"; 417 418 final HtmlPage page = loadPage(htmlContent); 419 420 final HtmlSubmitInput loginButton 421 = (HtmlSubmitInput)page.getDocumentElement().getOneHtmlElementByAttribute("input","value","Login"); 422 loginButton.click(); 423 } 424 425 426 429 public void testReset_onResetHandler() 430 throws Exception { 431 432 final String firstContent 433 = "<html><head><title>First</title></head><body>" 434 + "<form method='get' action='http://second' " 435 + "onReset='alert(\"clicked\");'>" 436 + "<input name='button' type='reset' value='PushMe' id='button'/></form>" 437 + "</body></html>"; 438 final String secondContent = "<html><head><title>Second</title></head><body></body></html>"; 439 440 final WebClient client = new WebClient(); 441 final List collectedAlerts = new ArrayList(); 442 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 443 444 final MockWebConnection webConnection = new MockWebConnection( client ); 445 webConnection.setResponse(URL_FIRST, firstContent); 446 webConnection.setResponse(URL_SECOND, secondContent); 447 448 client.setWebConnection( webConnection ); 449 450 final HtmlPage firstPage = ( HtmlPage )client.getPage(URL_FIRST); 451 final HtmlResetInput button = (HtmlResetInput)firstPage.getHtmlElementById("button"); 452 453 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 454 final HtmlPage secondPage = (HtmlPage)button.click(); 455 456 assertSame( firstPage, secondPage ); 457 assertEquals( Collections.singletonList("clicked"), collectedAlerts ); 458 } 459 460 461 470 public void testSubmit_AnchorCausesSubmit_onSubmitHandler_returnFalse() 471 throws Exception { 472 473 final String firstContent 474 = "<html><head><title>First</title></head>" 475 + "<script>function doalert(message){alert(message);}</script>" 476 + "<body><form name='form1' method='get' action='http://second' " 477 + "onSubmit='doalert(\"clicked\");return false;'>" 478 + "<input name='button' type='submit' value='PushMe' id='button'/></form>" 479 + "<a id='link1' HREF='javascript:document.form1.submit()'>Click me</a>" 480 + "</body></html>"; 481 final String secondContent = "<html><head><title>Second</title></head><body></body></html>"; 482 483 final WebClient client = new WebClient(); 484 final List collectedAlerts = new ArrayList(); 485 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 486 487 final MockWebConnection webConnection = new MockWebConnection( client ); 488 webConnection.setResponse(URL_FIRST, firstContent); 489 webConnection.setResponse(URL_SECOND, secondContent); 490 491 client.setWebConnection( webConnection ); 492 493 final HtmlPage firstPage = ( HtmlPage )client.getPage(URL_FIRST); 494 final HtmlAnchor anchor = (HtmlAnchor)firstPage.getHtmlElementById("link1"); 495 496 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 497 final HtmlPage secondPage = (HtmlPage)anchor.click(); 498 assertEquals( "Second", secondPage.getTitleText() ); 499 500 assertEquals( Collections.EMPTY_LIST, collectedAlerts ); 501 } 502 503 504 507 public void testSubmit_NoDefaultValue() 508 throws Exception { 509 final String htmlContent 510 = "<html><head><title>foo</title></head><body>" 511 + "<form id='form1'>" 512 + " <input type='text' name='textfield'/>" 513 + " <input type='submit' name='button' value='foo'/>" 514 + "</form></body></html>"; 515 final HtmlPage page = loadPage(htmlContent); 516 final MockWebConnection webConnection = getMockConnection(page); 517 518 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 519 520 final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button"); 521 button.click(); 522 523 final List expectedParameters = Arrays.asList( new Object[]{ 524 new KeyValuePair("textfield", ""), new KeyValuePair("button", "foo") 525 } ); 526 final List collectedParameters = webConnection.getLastParameters(); 527 528 assertEquals( expectedParameters, collectedParameters ); 529 } 530 531 532 535 public void testGetInputByName_WithinNoScriptTags() throws Exception { 536 final String htmlContent 537 = "<html><head><title>foo</title></head><body>" 538 + "<form id='form1'>" 539 + " <input type='text' name='textfield' value='*'/>" 540 + " <noscript>" 541 + " <input type='submit' name='button' value='foo'/>" 542 + " </noscript>" 543 + "</form></body></html>"; 544 final HtmlPage page = loadPage(htmlContent); 545 546 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 547 548 form.getInputByName("button"); 550 } 551 552 553 556 public void testForSubmit_TwoInputsWithSameName() throws Exception { 557 final String firstContent 558 = "<html><head><title>First</title></head><body>" 559 + "<form id='form1' name='form1' action='http://second'>" 560 + " <input type='hidden' name='foo' value='bar'/>" 561 + " <input type='submit' name='foo' value='bar'/>" 562 + "</form></body></html>"; 563 final String secondContent 564 = "<html><head><title>Second</title></head><body'></body></html>"; 565 final WebClient client = new WebClient(); 566 567 final MockWebConnection webConnection = new MockWebConnection( client ); 568 webConnection.setResponse(URL_FIRST, firstContent); 569 webConnection.setResponse(URL_SECOND, secondContent); 570 client.setWebConnection( webConnection ); 571 572 final HtmlPage firstPage = ( HtmlPage )client.getPage(URL_FIRST); 573 final HtmlForm form = ( HtmlForm )firstPage.getHtmlElementById( "form1" ); 574 575 final HtmlPage secondPage = (HtmlPage) form.submit("foo"); 576 assertEquals( "Second", secondPage.getTitleText() ); 577 } 578 579 582 public void testSubmit_NoNameOnControl() 583 throws Exception { 584 final String htmlContent 585 = "<html><head><title>foo</title></head><body>" 586 + "<form id='form1'>" 587 + " <input type='text' id='textfield' value='blah'/>" 588 + " <input type='submit' name='button' value='foo'/>" 589 + "</form></body></html>"; 590 final HtmlPage page = loadPage(htmlContent); 591 final MockWebConnection webConnection = getMockConnection(page); 592 593 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 594 595 final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button"); 596 button.click(); 597 598 final List expectedParameters = Arrays.asList( new Object[]{ 599 new KeyValuePair("button", "foo") 600 } ); 601 final List collectedParameters = webConnection.getLastParameters(); 602 603 assertEquals( expectedParameters, collectedParameters ); 604 } 605 606 609 public void testSubmit_NoNameOnButton() throws Exception { 610 final String htmlContent 611 = "<html><head><title>foo</title></head><body>" 612 + "<form id='form1'>" 613 + " <input type='text' id='textfield' value='blah' name='textfield' />" 614 + " <button type='submit' id='button' value='Go'>Go</button>" 615 + "</form></body></html>"; 616 final HtmlPage page = loadPage(htmlContent); 617 final MockWebConnection webConnection = getMockConnection(page); 618 619 final HtmlButton button = (HtmlButton) page.getHtmlElementById( "button" ); 620 button.click(); 621 622 final List expectedParameters = Arrays.asList( new Object[]{ 623 new KeyValuePair("textfield", "blah") 624 } ); 625 final List collectedParameters = webConnection.getLastParameters(); 626 627 assertEquals( expectedParameters, collectedParameters ); 628 } 629 632 public void testSubmit_NestedInput() 633 throws Exception { 634 final String htmlContent 635 = "<html><head><title>foo</title></head><body>" 636 + "<form id='form1'>" 637 + " <table><tr><td>" 638 + " <input type='text' name='textfield' value='blah'/>" 639 + " </td><td>" 640 + " <input type='submit' name='button' value='foo'/>" 641 + " </td></tr>" 642 + " </table>" 643 + "</form></body></html>"; 644 final HtmlPage page = loadPage(htmlContent); 645 final MockWebConnection webConnection = getMockConnection(page); 646 647 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 648 649 final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button"); 650 button.click(); 651 652 final List expectedParameters = Arrays.asList( new Object[]{ 653 new KeyValuePair("textfield", "blah"), 654 new KeyValuePair("button", "foo") 655 } ); 656 final List collectedParameters = webConnection.getLastParameters(); 657 658 assertEquals( expectedParameters, collectedParameters ); 659 } 660 661 664 public void testSubmit_IgnoresDisabledControls() 665 throws Exception { 666 final String htmlContent 667 = "<html><head><title>foo</title></head><body>" 668 + "<form id='form1'>" 669 + " <input type='text' name='textfield' value='blah' disabled />" 670 + " <input type='submit' name='button' value='foo'/>" 671 + "</form></body></html>"; 672 final HtmlPage page = loadPage(htmlContent); 673 final MockWebConnection webConnection = getMockConnection(page); 674 675 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 676 677 final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button"); 678 button.click(); 679 680 final List expectedParameters = Arrays.asList( new Object[]{ 681 new KeyValuePair("button", "foo") 682 } ); 683 final List collectedParameters = webConnection.getLastParameters(); 684 685 assertEquals( expectedParameters, collectedParameters ); 686 } 687 688 691 public void testSubmit_CheckboxClicked() throws Exception { 692 final String htmlContent 693 = "<html><head><title>foo</title>" 694 + "<script language='javascript'>" 695 + "function setFormat()" 696 + "{" 697 + " if(document.form1.Format.checked) {" 698 + " document.form1.Format.value='html';" 699 + " } else {" 700 + " document.form1.Format.value='plain';" 701 + " }" 702 + "}" 703 + "</script>" 704 + "</head><body>" 705 + "<form name='form1' id='form1'>" 706 + "<input type=checkbox name=Format value='' onclick='setFormat()'>" 707 + " <input type='submit' name='button' value='foo'/>" 708 + "</form></body></html>"; 709 final HtmlPage page = loadPage(htmlContent); 710 final MockWebConnection webConnection = getMockConnection(page); 711 712 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 713 714 final HtmlCheckBoxInput checkBox = (HtmlCheckBoxInput) form.getInputByName("Format"); 715 716 final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button"); 717 718 final List expectedParameters0 = Arrays.asList( new Object[]{ 719 new KeyValuePair("button", "foo") 720 } ); 721 final List expectedParameters1 = Arrays.asList( new Object[]{ 722 new KeyValuePair("Format", "html"), 723 new KeyValuePair("button", "foo") 724 } ); 725 726 727 button.click(); 728 final List collectedParameters0 = webConnection.getLastParameters(); 729 730 checkBox.click(); 731 button.click(); 732 final List collectedParameters1 = webConnection.getLastParameters(); 733 734 assertEquals( expectedParameters0, collectedParameters0 ); 735 assertEquals( expectedParameters1, collectedParameters1 ); 736 } 737 738 741 public void testGetInputByValue() throws Exception { 742 final String htmlContent 743 = "<html><head><title>foo</title></head><body>" 744 + "<form id='form1'>" 745 + " <input type='submit' name='button' value='xxx'/>" 746 + " <input type='text' name='textfield' value='foo'/>" 747 + " <input type='submit' name='button1' value='foo'/>" 748 + " <input type='reset' name='button2' value='foo'/>" 749 + " <input type='submit' name='button' value='bar'/>" 750 + "</form></body></html>"; 751 final HtmlPage page = loadPage(htmlContent); 752 753 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 754 755 final List allInputsByValue = form.getInputsByValue("foo"); 756 final ListIterator iterator = allInputsByValue.listIterator(); 757 while( iterator.hasNext() ) { 758 final HtmlInput input = (HtmlInput)iterator.next(); 759 iterator.set( input.getNameAttribute() ); 760 } 761 762 final List expectedInputs = Arrays.asList( new String[] { 763 "textfield", "button1", "button2" 764 } ); 765 assertEquals( "Get all", expectedInputs, allInputsByValue ); 766 assertEquals( Collections.EMPTY_LIST, form.getInputsByValue("none-matching")); 767 768 assertEquals("Get first", "button", form.getInputByValue("bar").getNameAttribute() ); 769 try { 770 form.getInputByValue("none-matching"); 771 fail("Expected ElementNotFoundException"); 772 } 773 catch( final ElementNotFoundException e ) { 774 } 776 } 777 778 783 public void testSubmitToTargetWindow() throws Exception { 784 final String firstContent 785 = "<html><head><title>first</title></head><body>" 786 + "<form id='form1' target='window2' action='http://second'>" 787 + " <input type='submit' name='button' value='push me'/>" 788 + "</form></body></html>"; 789 final WebClient client = new WebClient(); 790 791 final MockWebConnection webConnection = new MockWebConnection( client ); 792 webConnection.setResponse(URL_FIRST, firstContent); 793 webConnection.setResponseAsGenericHtml(URL_SECOND, "second"); 794 client.setWebConnection( webConnection ); 795 796 final HtmlPage page = ( HtmlPage )client.getPage(URL_FIRST); 797 final HtmlForm form = ( HtmlForm )page.getHtmlElementById( "form1" ); 798 799 final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button"); 800 final HtmlPage secondPage = (HtmlPage)button.click(); 801 assertEquals("window2", secondPage.getEnclosingWindow().getName()); 802 803 final WebWindow firstWindow = client.getCurrentWindow(); 804 assertEquals("first window name", "", firstWindow.getName() ); 805 assertSame( page, firstWindow.getEnclosedPage() ); 806 } 807 808 811 public void testSubmit_SelectHasNoOptions()throws Exception { 812 813 final String htmlContent 814 = "<html><body><form name='form' method='GET' action='action.html'>" 815 + "<select name='select'>" 816 + "</select>" 817 + "</form></body></html>"; 818 final HtmlPage page = loadPage(htmlContent); 819 final MockWebConnection webConnection = getMockConnection(page); 820 821 final HtmlPage secondPage = ( HtmlPage ) page.getFormByName("form").submit(); 822 823 assertNotNull( secondPage ); 824 assertEquals( "parameters", Collections.EMPTY_LIST, webConnection.getLastParameters() ); 825 } 826 827 830 public void testSubmit_SelectOptionWithoutValueAttribute()throws Exception { 831 832 final String htmlContent 833 = "<html><body><form name='form' method='GET' action='action.html'>" 834 + "<select name='select'>" 835 + " <option>first value</option>" 836 + " <option selected>second value</option>" 837 + "</select>" 838 + "</form></body></html>"; 839 final HtmlPage page = loadPage(htmlContent); 840 final MockWebConnection webConnection = getMockConnection(page); 841 842 final HtmlPage secondPage = ( HtmlPage ) page.getFormByName("form").submit(); 843 844 assertNotNull( secondPage ); 845 846 final List expectedParameters = Arrays.asList( new Object[]{ 847 new KeyValuePair("select", "second value") 848 } ); 849 850 assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() ); 851 } 852 853 857 public void testSubmit_DeepInputs() throws Exception { 858 final String htmlContent 859 = "<html><form method='GET' action=''>" 860 + "<table><tr><td>" 861 + "<input value='NOT_SUBMITTED' name='data' type='text'/>" 862 + "</td></tr></table>" 863 + "<input id='submitButton' name='submit' type='submit'/>" 864 + "</form></html>"; 865 final HtmlPage page = loadPage(htmlContent); 866 final MockWebConnection webConnection = getMockConnection(page); 867 868 final HtmlInput submitButton = (HtmlInput)page.getHtmlElementById("submitButton"); 869 submitButton.click(); 870 871 final List collectedParameters = webConnection.getLastParameters(); 872 final List expectedParameters = Arrays.asList( new Object[] { 873 new KeyValuePair("data", "NOT_SUBMITTED"), 874 new KeyValuePair("submit", "") 875 } ); 876 assertEquals(expectedParameters, collectedParameters); 877 } 878 879 883 public void testSubmit_FormElementOrder() throws Exception { 884 final String htmlContent 885 = "<html><head></head><body><form method='post' action=''>" 886 + "<input type='submit' name='dispatch' value='Save' id='submitButton'>" 887 + "<input type='hidden' name='dispatch' value='TAB'>" 888 + "</form></body></html>"; 889 final WebClient client = new WebClient(); 890 891 final MockWebConnection webConnection = new MockWebConnection( client ); 892 webConnection.setDefaultResponse( htmlContent ); 893 client.setWebConnection( webConnection ); 894 895 final WebRequestSettings settings = new WebRequestSettings(URL_GARGOYLE, SubmitMethod.POST); 896 897 final HtmlPage page = ( HtmlPage )client.getPage(settings); 898 final HtmlInput submitButton = (HtmlInput)page.getHtmlElementById("submitButton"); 899 submitButton.click(); 900 901 final List collectedParameters = webConnection.getLastParameters(); 902 final List expectedParameters = Arrays.asList( new Object[] { 903 new KeyValuePair("dispatch", "Save"), 904 new KeyValuePair("dispatch", "TAB"), 905 } ); 906 assertCollectionsEqual(expectedParameters, collectedParameters); 907 } 908 909 916 public void testJSSubmit_JavaScriptAction() throws Exception { 917 final String htmlContent 918 = "<html><head><title>First</title></head>" 919 + "<body onload='document.getElementById(\"aForm\").submit()'>" 920 + "<form id='aForm' action='javascript:alert(\"clicked\")'" 921 + "</form>" 922 + "</body></html>"; 923 924 final List expectedAlerts = Collections.singletonList("clicked"); 925 createTestPageForRealBrowserIfNeeded(htmlContent, expectedAlerts); 926 927 final List collectedAlerts = new ArrayList(); 928 loadPage(htmlContent, collectedAlerts); 929 930 assertEquals(expectedAlerts, collectedAlerts); 931 } 932 } 933 934 | Popular Tags |