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 45 import com.gargoylesoftware.htmlunit.KeyValuePair; 46 import com.gargoylesoftware.htmlunit.MockWebConnection; 47 import com.gargoylesoftware.htmlunit.SubmitMethod; 48 import com.gargoylesoftware.htmlunit.WebTestCase; 49 50 57 public class HtmlSelectTest extends WebTestCase { 58 59 64 public HtmlSelectTest(final String name) { 65 super(name); 66 } 67 68 73 public void testSelect() throws Exception { 74 75 final String htmlContent = "<html><head><title>foo</title></head><body>" 76 + "<form id='form1'><select name='select1'>" 77 + "<option value='option1'>Option1</option>" 78 + "<option value='option2' selected='selected'>Option2</option>" 79 + "<option value='option3'>Option3</option>" 80 + "</select>" 81 + "<input type='submit' name='button' value='foo'/>" 82 + "</form></body></html>"; 83 final HtmlPage page = loadPage(htmlContent); 84 final MockWebConnection webConnection = getMockConnection(page); 85 86 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 87 88 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0); 89 final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button"); 90 91 assertCollectionsEqual(Arrays.asList(new Object[] {select, button}), form.getAllSubmittableElements(button)); 93 94 final HtmlPage secondPage = (HtmlPage) button.click(); 96 97 final List expectedParameters = new ArrayList(); 98 expectedParameters.add(new KeyValuePair("select1", "option2")); 99 expectedParameters.add(new KeyValuePair("button", "foo")); 100 101 assertEquals("url", URL_GARGOYLE, secondPage.getWebResponse().getUrl()); 102 assertEquals("method", SubmitMethod.GET, webConnection.getLastMethod()); 103 assertEquals("parameters", expectedParameters, webConnection.getLastParameters()); 104 assertNotNull(secondPage); 105 } 106 107 112 public void testSelect_MultipleSelectNoneSelected() throws Exception { 113 114 final String htmlContent = "<html><head><title>foo</title></head><body>" 115 + "<form id='form1'><select name='select1' multiple>" 116 + "<option value='option1'>Option1</option>" 117 + "<option value='option2'>Option2</option>" 118 + "<option value='option3'>Option3</option>" 119 + "</select>" 120 + "<input type='submit' name='button' value='foo'/>" 121 + "</form></body></html>"; 122 final HtmlPage page = loadPage(htmlContent); 123 final MockWebConnection webConnection = getMockConnection(page); 124 125 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 126 127 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0); 128 assertNotNull(select); 129 130 final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button"); 131 132 final HtmlPage secondPage = (HtmlPage) button.click(); 134 135 final List expectedParameters = new ArrayList(); 136 expectedParameters.add(new KeyValuePair("button", "foo")); 137 138 assertEquals("url", URL_GARGOYLE, secondPage.getWebResponse().getUrl()); 139 assertEquals("method", SubmitMethod.GET, webConnection.getLastMethod()); 140 assertEquals("parameters", expectedParameters, webConnection.getLastParameters()); 141 assertNotNull(secondPage); 142 } 143 144 149 public void testSelect_ChangeSelectedOption_SingleSelect() throws Exception { 150 151 final String htmlContent = "<html><head><title>foo</title></head><body>" 152 + "<form id='form1'><select name='select1'>" 153 + "<option value='option1' selected='selected'>Option1</option>" 154 + "<option value='option2'>Option2</option>" 155 + "<option value='option3'>Option3</option>" 156 + "</select>" 157 + "<input type='submit' name='button' value='foo'/>" 158 + "</form></body></html>"; 159 final HtmlPage page = loadPage(htmlContent); 160 final MockWebConnection webConnection = getMockConnection(page); 161 162 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 163 164 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0); 165 final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button"); 166 167 select.setSelectedAttribute("option3", true); 169 170 final HtmlPage secondPage = (HtmlPage) button.click(); 172 173 final List expectedParameters = new ArrayList(); 174 expectedParameters.add(new KeyValuePair("select1", "option3")); 175 expectedParameters.add(new KeyValuePair("button", "foo")); 176 177 assertEquals("url", URL_GARGOYLE, secondPage.getWebResponse().getUrl()); 178 assertEquals("method", SubmitMethod.GET, webConnection.getLastMethod()); 179 assertEquals("parameters", expectedParameters, webConnection.getLastParameters()); 180 assertNotNull(secondPage); 181 } 182 183 188 public void testSelect_ChangeSelectedOption_MultipleSelect() throws Exception { 189 190 final String htmlContent = "<html><head><title>foo</title></head><body>" 191 + "<form id='form1'><select name='select1' multiple='multiple'>" 192 + "<option value='option1' selected='selected'>Option1</option>" 193 + "<option value='option2'>Option2</option>" 194 + "<option value='option3'>Option3</option>" 195 + "</select>" 196 + "<input type='submit' name='button' value='foo'/>" 197 + "</form></body></html>"; 198 final HtmlPage page = loadPage(htmlContent); 199 final MockWebConnection webConnection = getMockConnection(page); 200 201 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 202 203 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0); 204 final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button"); 205 206 select.setSelectedAttribute("option3", true); 208 select.setSelectedAttribute("option2", true); 209 210 final HtmlPage secondPage = (HtmlPage) button.click(); 212 213 final List expectedParameters = new ArrayList(); 214 expectedParameters.add(new KeyValuePair("select1", "option1")); 215 expectedParameters.add(new KeyValuePair("select1", "option2")); 216 expectedParameters.add(new KeyValuePair("select1", "option3")); 217 expectedParameters.add(new KeyValuePair("button", "foo")); 218 219 assertEquals("url", URL_GARGOYLE, secondPage.getWebResponse().getUrl()); 220 assertEquals("method", SubmitMethod.GET, webConnection.getLastMethod()); 221 assertEquals("parameters", expectedParameters, webConnection.getLastParameters()); 222 assertNotNull(secondPage); 223 } 224 225 230 public void testSelect_MultipleSelectMultipleSelected() throws Exception { 231 232 final String htmlContent = "<html><head><title>foo</title></head><body>" 233 + "<form id='form1'><select name='select1' multiple>" 234 + "<option value='option1' selected='selected'>Option1</option>" 235 + "<option value='option2'>Option2</option>" 236 + "<option value='option3' selected='selected'>Option3</option>" 237 + "</select>" 238 + "<input type='submit' name='button' value='foo'/>" 239 + "</form></body></html>"; 240 final HtmlPage page = loadPage(htmlContent); 241 242 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 243 244 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0); 245 final List expected = new ArrayList(); 246 expected.add(select.getOptionByValue("option1")); 247 expected.add(select.getOptionByValue("option3")); 248 249 assertEquals(expected, select.getSelectedOptions()); 250 } 251 252 258 public void testSelect_SingleSelectMultipleSelected() throws Exception { 259 260 final String htmlContent = "<html><head><title>foo</title></head><body>" 261 + "<form id='form1'><select name='select1'>" 262 + "<option value='option1' selected='selected'>Option1</option>" 263 + "<option value='option2'>Option2</option>" 264 + "<option value='option3' selected='selected'>Option3</option>" 265 + "</select>" 266 + "<input type='submit' name='button' value='foo'/>" 267 + "</form></body></html>"; 268 final HtmlPage page = loadPage(htmlContent); 269 270 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 271 272 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0); 273 final List expected = new ArrayList(); 274 expected.add(select.getOptionByValue("option3")); 275 276 assertEquals(expected, select.getSelectedOptions()); 277 } 278 279 285 public void testSelect_SingleSelectNoneSelected() throws Exception { 286 287 final String htmlContent = "<html><head><title>foo</title></head><body>" 288 + "<form id='form1'><select name='select1'>" 289 + "<option value='option1'>Option1</option>" 290 + "<option value='option2'>Option2</option>" 291 + "<option value='option3'>Option3</option>" 292 + "</select>" 293 + "<input type='submit' name='button' value='foo'/>" 294 + "</form></body></html>"; 295 final HtmlPage page = loadPage(htmlContent); 296 297 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 298 299 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0); 300 final List expected = new ArrayList(); 301 expected.add(select.getOptionByValue("option1")); 302 303 assertEquals(expected, select.getSelectedOptions()); 304 } 305 306 311 public void testSelect_SingleSelectNoneSelectedButSizeGreaterThanOne() throws Exception { 312 313 final String htmlContent = "<html><head><title>foo</title></head><body>" 314 + "<form>" 315 + "<select name='select1' size='2' id='mySelect'>" 316 + "<option value='option1'>Option1</option>" 317 + "<option value='option2'>Option2</option>" 318 + "<option value='option3'>Option3</option>" 319 + "</select>" 320 + "</form></body></html>"; 321 322 final HtmlPage page = loadPage(htmlContent); 323 324 final HtmlSelect select = (HtmlSelect) page.getHtmlElementById("mySelect"); 325 326 assertEquals(Collections.EMPTY_LIST, select.getSelectedOptions()); 327 } 328 329 334 public void testSetSelected_IllegalValue() throws Exception { 335 336 final String htmlContent = "<html><head><title>foo</title></head><body>" 337 + "<form id='form1'><select name='select1'>" 338 + "<option value='option1' selected='selected'>Option1</option>" 339 + "<option value='option2'>Option2</option>" 340 + "<option value='option3'>Option3</option>" 341 + "</select>" 342 + "<input type='submit' name='button' value='foo'/>" 343 + "</form></body></html>"; 344 final HtmlPage page = loadPage(htmlContent); 345 final MockWebConnection webConnection = getMockConnection(page); 346 347 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 348 349 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0); 350 final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("button"); 351 352 try { 354 select.setSelectedAttribute("missingOption", true); 355 fail("Expected IllegalArgumentException"); 356 } 357 catch (final IllegalArgumentException e) { 358 } 360 361 select.fakeSelectedAttribute("newOption"); 362 363 final HtmlPage secondPage = (HtmlPage) button.click(); 365 366 final List expectedParameters = new ArrayList(); 367 expectedParameters.add(new KeyValuePair("select1", "newOption")); 368 expectedParameters.add(new KeyValuePair("button", "foo")); 369 370 assertEquals("url", URL_GARGOYLE, secondPage.getWebResponse().getUrl()); 371 assertEquals("method", SubmitMethod.GET, webConnection.getLastMethod()); 372 assertEquals("parameters", expectedParameters, webConnection.getLastParameters()); 373 assertNotNull(secondPage); 374 } 375 376 379 public void testGetAllOptions() throws Exception { 380 381 final String htmlContent = "<html><head><title>foo</title></head><body>" 382 + "<form id='form1'><select name='select1'>" 383 + "<option value='option1' selected='selected'>Option1</option>" 384 + "<option value='option2'>Option2</option>" 385 + "<optgroup label='group1'>" 386 + " <option value='option3'>Option3</option>" 387 + "</optgroup>" 388 + "</select>" 389 + "<input type='submit' name='button' value='foo'/>" 390 + "</form></body></html>"; 391 final HtmlPage page = loadPage(htmlContent); 392 393 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 394 395 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0); 396 397 final List expectedOptions = new ArrayList(); 398 expectedOptions.add(select.getOptionByValue("option1")); 399 expectedOptions.add(select.getOptionByValue("option2")); 400 expectedOptions.add(select.getOptionByValue("option3")); 401 402 assertEquals(expectedOptions, select.getAllOptions()); 403 } 404 405 408 public void testSelect_OptionMultiple_NoValueOnAttribute() throws Exception { 409 410 final String htmlContent = "<html><head><title>foo</title></head><body>" 411 + "<form id='form1'><select name='select1' id='select1' multiple>" 412 + "<option value='option1'>Option1</option>" 413 + "<option value='option2' >Option2</option>" 414 + "<option value='option3'>Option3</option>" 415 + "</select>" 416 + "<input type='submit' name='button' value='foo'/>" 417 + "</form></body></html>"; 418 final HtmlPage page = loadPage(htmlContent); 419 420 final HtmlSelect select = (HtmlSelect) page.getHtmlElementById("select1"); 421 assertTrue(select.isMultipleSelectEnabled()); 422 } 423 424 427 public void testGetOptionByValue_TwoOptionsWithSameValue() throws Exception { 428 429 final String htmlContent = "<html><head><title>foo</title></head><body><form id='form1'>" 430 + "<select name='select1'>" 431 + " <option value='option1'>s1o1</option>" 432 + " <option value='option2'>s1o2</option>" 433 + "</select>" 434 + "<select name='select2'>" 435 + " <option value='option1'>s2o1</option>" 436 + " <option value='option2'>s2o2</option>" 437 + "</select>" 438 + "<input type='submit' name='button' value='foo'/>" 439 + "</form></body></html>"; 440 final HtmlPage page = loadPage(htmlContent); 441 442 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 443 444 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select2").get(0); 445 assertEquals("s2o2", select.getOptionByValue("option2").asText()); 446 } 447 448 451 public void testSelect_SetSelected_OnChangeHandler() throws Exception { 452 453 final String htmlContent = "<html><head><title>foo</title></head><body>" 454 + "<form id='form1'><select name='select1' onChange='alert(\"changing\")'>" 455 + "<option value='option1' selected='selected'>Option1</option>" 456 + "<option value='option2'>Option2</option>" 457 + "<option value='option3'>Option3</option>" 458 + "</select>" 459 + "<input type='submit' name='button' value='foo'/>" 460 + "</form></body></html>"; 461 final List collectedAlerts = new ArrayList(); 462 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 463 464 final HtmlForm form = (HtmlForm) page.getHtmlElementById("form1"); 465 466 final HtmlSelect select = (HtmlSelect) form.getSelectsByName("select1").get(0); 467 468 select.setSelectedAttribute("option3", true); 470 471 final List expectedAlerts = Collections.singletonList("changing"); 472 assertEquals(expectedAlerts, collectedAlerts); 473 } 474 475 478 public void testSetSelectionOnOptionWithNoName() throws Exception { 479 480 final String htmlContent = "<html><body><form name='form' method='GET' action='action.html'>" 481 + "<select name='select' multiple size='5'>" 482 + "<option value='1'>111</option>" 483 + "<option id='option2'>222</option>" 484 + "</select>" 485 + "</form></body></html>"; 486 final List collectedAlerts = new ArrayList(); 487 final HtmlPage page = loadPage(htmlContent, collectedAlerts); 488 489 final HtmlOption option = (HtmlOption) page.getHtmlElementById("option2"); 490 option.setSelected(true); 491 } 492 493 private void checkOptions(final HtmlSelect select) { 494 final List options = select.getAllOptions(); 495 if (options.isEmpty()) { 496 assertNull(select.getFirstChild()); 497 assertNull(select.getLastChild()); 498 } 499 else { 500 assertEquals(options.get(0), select.getFirstChild()); 501 assertEquals(options.get(options.size() - 1), select.getLastChild()); 502 } 503 } 504 505 506 public void testRemoveOptionsFromSelect() throws Exception { 507 508 final String htmlContent = "<html><body><form name='form' method='GET' action='action.html'>" 509 + "<select name='select' id='theSelect'>" 510 + "<option value='a'>111</option>" 511 + "<option value='b'>222</option>" 512 + "<option value='c'>333</option>" 513 + "<option value='d'>444</option>" 514 + "</select>" 515 + "</form></body></html>"; 516 final HtmlPage page = loadPage(htmlContent); 517 518 final HtmlSelect theSelect = (HtmlSelect) page.getHtmlElementById("theSelect"); 519 assertNotNull(theSelect); 520 521 assertEquals(4, theSelect.getAllOptions().size()); 522 assertEquals("a", theSelect.getOption(0).getValue()); 523 assertEquals("b", theSelect.getOption(1).getValue()); 524 assertEquals("c", theSelect.getOption(2).getValue()); 525 assertEquals("d", theSelect.getOption(3).getValue()); 526 527 theSelect.getOption(1).remove(); 529 checkOptions(theSelect); 530 assertEquals(3, theSelect.getAllOptions().size()); 531 assertEquals("a", theSelect.getOption(0).getValue()); 532 assertEquals("c", theSelect.getOption(1).getValue()); 533 assertEquals("d", theSelect.getOption(2).getValue()); 534 535 theSelect.getOption(2).remove(); 537 checkOptions(theSelect); 538 assertEquals(2, theSelect.getAllOptions().size()); 539 assertEquals("a", theSelect.getOption(0).getValue()); 540 assertEquals("c", theSelect.getOption(1).getValue()); 541 542 theSelect.getOption(0).remove(); 544 checkOptions(theSelect); 545 assertEquals(1, theSelect.getAllOptions().size()); 546 assertEquals("c", theSelect.getOption(0).getValue()); 547 548 theSelect.getOption(0).remove(); 550 checkOptions(theSelect); 551 assertEquals(0, theSelect.getAllOptions().size()); 552 } 553 554 555 public void testEditOptions() throws Exception { 556 557 final String htmlContent = "<html><body><form name='form' method='GET' action='action.html'>" 558 + "<select name='select' id='theSelect'>" 559 + "<option value='a'>111</option>" 560 + "<option value='b'>222</option>" 561 + "<option value='c'>333</option>" 562 + "</select>" 563 + "</form></body></html>"; 564 final HtmlPage page = loadPage(htmlContent); 565 566 final HtmlSelect theSelect = (HtmlSelect) page.getHtmlElementById("theSelect"); 567 568 assertNotNull(theSelect); 569 assertEquals(3, theSelect.getAllOptions().size()); 570 571 appendOption(theSelect, "d"); 572 assertEquals(4, theSelect.getAllOptions().size()); 573 assertEquals("d", theSelect.getOption(3).getValue()); 574 575 theSelect.setOptionSize(1); 576 assertEquals(1, theSelect.getAllOptions().size()); 577 assertEquals("a", theSelect.getOption(0).getValue()); 578 579 appendOption(theSelect, "x"); 580 assertEquals(2, theSelect.getAllOptions().size()); 581 assertEquals("x", theSelect.getOption(1).getValue()); 582 583 } 584 585 590 public void testAsTextWhenNothingSelected() throws Exception { 591 final String htmlContent = "<html><head><title>foo</title></head><body>" 592 + "<form>" 593 + "<select name='select1' size='1' id='mySelect'>" 594 + "</select>" 595 + "</form></body></html>"; 596 597 final HtmlPage page = loadPage(htmlContent); 598 599 final HtmlSelect select = (HtmlSelect) page.getHtmlElementById("mySelect"); 600 601 assertEquals("", select.asText()); 602 } 603 604 void appendOption(final HtmlSelect select, final String value) { 605 final HtmlOption option = new HtmlOption(select.getPage(), null); 606 option.setValueAttribute(value); 607 option.setLabelAttribute(value); 608 select.appendOption(option); 609 } 610 611 } 612 | Popular Tags |