1 38 package com.gargoylesoftware.htmlunit.javascript.host; 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 import com.gargoylesoftware.htmlunit.html.HtmlOption; 50 import com.gargoylesoftware.htmlunit.html.HtmlPage; 51 import com.gargoylesoftware.htmlunit.html.HtmlSelect; 52 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; 53 54 55 63 public class SelectTest extends WebTestCase { 64 68 public SelectTest( final String name ) { 69 super(name); 70 } 71 72 73 76 public void testGetSelectedIndex() throws Exception { 77 final String content 78 = "<html><head><title>foo</title><script>" 79 + "function doTest() {\n" 80 + " alert(document.form1.select1.length);\n" 81 + " alert(document.form1.select1.selectedIndex);\n" 82 + "}</script></head><body onload='doTest()'>" 83 + "<p>hello world</p>" 84 + "<form name='form1'>" 85 + " <select name='select1'>" 86 + " <option name='option1'>One</option>" 87 + " <option name='option2' selected>Two</option>" 88 + " <option name='option3'>Three</option>" 89 + " </select>" 90 + "</form>" 91 + "</body></html>"; 92 93 final List collectedAlerts = new ArrayList(); 94 final HtmlPage page = loadPage(content, collectedAlerts); 95 assertEquals("foo", page.getTitleText()); 96 97 final List expectedAlerts = Arrays.asList( new String[]{ 98 "3", "1" 99 } ); 100 101 assertEquals( expectedAlerts, collectedAlerts ); 102 } 103 104 107 public void testSetSelectedIndex() throws Exception { 108 final String content 109 = "<html><head><title>foo</title><script>" 110 + "function doTest() {\n" 111 + " alert(document.form1.select1.length);\n" 112 + " alert(document.form1.select1.selectedIndex);\n" 113 + " document.form1.select1.selectedIndex = 2;\n" 114 + " alert(document.form1.select1.length);\n" 115 + " alert(document.form1.select1.selectedIndex);\n" 116 + " document.form1.select1.selectedIndex = -1;\n" 117 + "}</script></head><body onload='doTest()'>" 118 + "<p>hello world</p>" 119 + "<form name='form1' action='http://test' method='get'>" 120 + " <select name='select1'>" 121 + " <option value='option1' name='option1'>One</option>" 122 + " <option value='option2' name='option2' selected>Two</option>" 123 + " <option value='option3' name='option3'>Three</option>" 124 + " </select>" 125 + " <input type='submit' id='clickMe' name='submit' value='button'>" 126 + "</form>" 127 + "</body></html>"; 128 129 final List collectedAlerts = new ArrayList(); 130 final HtmlPage page = loadPage(content, collectedAlerts); 131 assertEquals("foo", page.getTitleText()); 132 133 final List expectedAlerts = Arrays.asList( new String[]{ 134 "3", "1", "3", "2" 135 } ); 136 137 assertEquals( expectedAlerts, collectedAlerts ); 138 139 final HtmlSubmitInput button = (HtmlSubmitInput) page.getHtmlElementById("clickMe"); 140 final HtmlPage newPage = (HtmlPage) button.click(); 141 142 143 final List expectedParameters = new ArrayList(); 144 expectedParameters.add( new KeyValuePair( "submit", "button" ) ); 145 146 final MockWebConnection webConnection = (MockWebConnection) newPage.getWebClient().getWebConnection(); 147 148 assertEquals("http://test", newPage.getWebResponse().getUrl().toExternalForm()); 149 assertEquals( "method", SubmitMethod.GET, webConnection.getLastMethod() ); 150 151 assertEquals( "parameters", expectedParameters, webConnection.getLastParameters() ); 152 } 153 154 157 public void testGetOptions() throws Exception { 158 final String content 159 = "<html><head><title>foo</title><script>" 160 + "function doTest() {\n" 161 + " var options = document.form1.select1.options;\n" 162 + " for( i=0; i<options.length; i++ ) {\n" 163 + " alert(options[i].value);\n" 164 + " alert(options[i].text);\n" 165 + " }\n" 166 + "}</script></head><body onload='doTest()'>" 167 + "<p>hello world</p>" 168 + "<form name='form1'>" 169 + " <select name='select1'>" 170 + " <option name='option1' value='value1'>One</option>" 171 + " <option name='option2' value='value2' selected>Two</option>" 172 + " <option name='option3' value='value3'>Three</option>" 173 + " </select>" 174 + "</form>" 175 + "</body></html>"; 176 177 final List collectedAlerts = new ArrayList(); 178 final HtmlPage page = loadPage(content, collectedAlerts); 179 assertEquals("foo", page.getTitleText()); 180 181 final List expectedAlerts = Arrays.asList( new String[]{ 182 "value1", "One", "value2", "Two", "value3", "Three" 183 } ); 184 185 assertEquals( expectedAlerts, collectedAlerts ); 186 } 187 188 189 192 public void testGetOptionLabel() throws Exception { 193 final String content 194 = "<html><head><title>foo</title><script>" 195 + "function doTest() {\n" 196 + " var options = document.form1.select1.options;\n" 197 + " for( i=0; i<options.length; i++ ) {\n" 198 + " alert(options[i].value);\n" 199 + " alert(options[i].text);\n" 200 + " }\n" 201 + "}</script></head><body onload='doTest()'>" 202 + "<p>hello world</p>" 203 + "<form name='form1'>" 204 + " <select name='select1'>" 205 + " <option name='option1' value='value1' label='OneLabel'>One</option>" 206 + " <option name='option2' value='value2' label='TwoLabel' selected>Two</option>" 207 + " <option name='option3' value='value3' label='ThreeLabel'>Three</option>" 208 + " </select>" 209 + "</form>" 210 + "</body></html>"; 211 212 final List collectedAlerts = new ArrayList(); 213 final HtmlPage page = loadPage(content, collectedAlerts); 214 assertEquals("foo", page.getTitleText()); 215 216 final List expectedAlerts = Arrays.asList( new String[]{ 217 "value1", "OneLabel", "value2", "TwoLabel", "value3", "ThreeLabel" 218 } ); 219 220 assertEquals( expectedAlerts, collectedAlerts ); 221 } 222 223 226 public void testGetOptionSelected() throws Exception { 227 final String content 228 = "<html><head><title>foo</title><script>" 229 + "function doTest() {\n" 230 + " var options = document.form1.select1.options;\n" 231 + " alert(options[0].selected);\n" 232 + " alert(options[1].selected);\n" 233 + " options[0].selected = true;\n" 234 + " alert(options[0].selected);\n" 235 + " alert(options[1].selected);\n" 236 + "}\n" 237 + "</script></head><body onload='doTest()'>" 238 + "<p>hello world</p>" 239 + "<form name='form1'>" 240 + " <select name='select1'>" 241 + " <option name='option1' value='value1'>One</option>" 242 + " <option name='option2' value='value2' selected>Two</option>" 243 + " <option name='option3' value='value3'>Three</option>" 244 + " </select>" 245 + "</form>" 246 + "</body></html>"; 247 248 final List collectedAlerts = new ArrayList(); 249 loadPage(content, collectedAlerts); 250 251 final List expectedAlerts = Arrays.asList( new String[]{ 252 "false", "true", "true", "false" 253 } ); 254 255 assertEquals( expectedAlerts, collectedAlerts ); 256 } 257 258 262 public void testGetOptionByIndex() throws Exception { 263 final String content 264 = "<html><head><title>first</title><script language='JavaScript'>" 265 + "function buggy(){\n" 267 + "var option1 = document.f1.elements['select'][0];\n" 268 + "alert(option1!=null);\n" 269 + "}\n" 270 + "</script></head><body onload='buggy();'>" 272 + "<form name='f1' action='xxx.html'><SELECT name='select'>" 273 + "<OPTION value='A'>111</OPTION>" 274 + "<OPTION value='B'>222</OPTION>" 275 + "</SELECT></form></body></html>"; 276 final List collectedAlerts = new ArrayList(); 277 final HtmlPage page = loadPage(content, collectedAlerts); 278 279 assertEquals("first", page.getTitleText()); 280 assertEquals( Collections.singletonList("true"), collectedAlerts ); 281 } 282 283 287 public void testGetOptionByOptionIndex() throws Exception { 288 final String content 289 = "<html><head><title>first</title><script language='JavaScript'>" 290 + "function buggy(){\n" 292 + "var option1 = document.form1.select1.options[0];\n" 293 + "alert(option1.text);\n" 294 + "}\n" 295 + "</script></head><body onload='buggy();'>" 297 + "<form name='form1'>" 298 + " <select name='select1'>" 299 + " <option name='option1' value='value1'>One</option>" 300 + " <option name='option2' value='value2' selected>Two</option>" 301 + " <option name='option3' value='value3'>Three</option>" 302 + " </select>" 303 + "</form>" 304 + "</form></body></html>"; 305 final List collectedAlerts = new ArrayList(); 306 final HtmlPage page = loadPage(content, collectedAlerts); 307 assertEquals("first", page.getTitleText()); 308 assertEquals( Collections.singletonList("One"), collectedAlerts ); 309 } 310 311 312 315 public void testAddOption() throws Exception { 316 final String content 317 = "<html><head><title>foo</title><script>" 318 + "function doTest() {\n" 319 + " var options = document.form1.select1.options;\n" 320 + " var index = options.length;\n" 321 + " options[index]=new Option('Four','value4');\n" 322 + " alert(options.length);\n" 323 + " alert(options[index].text);\n" 324 + " alert(options[index].value);\n" 325 + "}</script></head><body onload='doTest()'>" 326 + "<p>hello world</p>" 327 + "<form name='form1'>" 328 + " <select name='select1'>" 329 + " <option name='option1' value='value1'>One</option>" 330 + " <option name='option2' value='value2' selected>Two</option>" 331 + " <option name='option3' value='value3'>Three</option>" 332 + " </select>" 333 + "</form>" 334 + "</body></html>"; 335 336 final List collectedAlerts = new ArrayList(); 337 final HtmlPage page = loadPage(content, collectedAlerts); 338 assertEquals("foo", page.getTitleText()); 339 340 final List expectedAlerts = Arrays.asList( new String[]{ 341 "4", "Four", "value4" 342 } ); 343 344 assertEquals( expectedAlerts, collectedAlerts ); 345 } 346 347 350 public void testAddOptionWithAddMethod() throws Exception { 351 final String content 352 = "<html><head><title>foo</title><script>" 353 + "function doTest() {\n" 354 + " var options = document.form1.select1;\n" 355 + " options.add(new Option('Four','value4'), null);\n" 356 + " alert(options.length);\n" 357 + " var index = options.length - 1;\n" 358 + " alert(options[index].text);\n" 359 + " alert(options[index].value);\n" 360 + "}</script></head><body onload='doTest()'>" 361 + "<p>hello world</p>" 362 + "<form name='form1'>" 363 + " <select name='select1'>" 364 + " <option name='option1' value='value1'>One</option>" 365 + " <option name='option2' value='value2' selected>Two</option>" 366 + " <option name='option3' value='value3'>Three</option>" 367 + " </select>" 368 + "</form>" 369 + "</body></html>"; 370 371 final List collectedAlerts = new ArrayList(); 372 final HtmlPage page = loadPage(content, collectedAlerts); 373 assertEquals("foo", page.getTitleText()); 374 375 final List expectedAlerts = Arrays.asList( new String[]{ 376 "4", "Four", "value4" 377 } ); 378 379 assertEquals( expectedAlerts, collectedAlerts ); 380 } 381 382 385 public void testRemoveOption() throws Exception { 386 final String content 387 = "<html><head><title>foo</title><script>" 388 + "function doTest() {\n" 389 + " var options = document.form1.select1.options;\n" 390 + " options[1]=null;\n" 391 + " alert(options.length);\n" 392 + " alert(options[1].text);\n" 393 + " alert(options[1].value);\n" 394 + "}</script></head><body onload='doTest()'>" 395 + "<p>hello world</p>" 396 + "<form name='form1'>" 397 + " <select name='select1'>" 398 + " <option name='option1' value='value1'>One</option>" 399 + " <option name='option2' value='value2' selected>Two</option>" 400 + " <option name='option3' value='value3'>Three</option>" 401 + " </select>" 402 + "</form>" 403 + "</body></html>"; 404 405 final List collectedAlerts = new ArrayList(); 406 final HtmlPage page = loadPage(content, collectedAlerts); 407 assertEquals("foo", page.getTitleText()); 408 409 final List expectedAlerts = Arrays.asList( new String[]{ 410 "2", "Three", "value3" 411 } ); 412 413 assertEquals( expectedAlerts, collectedAlerts ); 414 } 415 416 419 public void testRemoveOptionWithRemoveMethod() throws Exception { 420 final String content 421 = "<html><head><title>foo</title><script>" 422 + "function doTest() {\n" 423 + " var options = document.form1.select1;\n" 424 + " options.remove(1);\n" 425 + " alert(options.length);\n" 426 + " alert(options[1].text);\n" 427 + " alert(options[1].value);\n" 428 + "}</script></head><body onload='doTest()'>" 429 + "<p>hello world</p>" 430 + "<form name='form1'>" 431 + " <select name='select1'>" 432 + " <option name='option1' value='value1'>One</option>" 433 + " <option name='option2' value='value2' selected>Two</option>" 434 + " <option name='option3' value='value3'>Three</option>" 435 + " </select>" 436 + "</form>" 437 + "</body></html>"; 438 439 final List collectedAlerts = new ArrayList(); 440 final HtmlPage page = loadPage(content, collectedAlerts); 441 assertEquals("foo", page.getTitleText()); 442 443 final List expectedAlerts = Arrays.asList( new String[]{ 444 "2", "Three", "value3" 445 } ); 446 447 assertEquals( expectedAlerts, collectedAlerts ); 448 } 449 450 453 public void testClearOptions() throws Exception { 454 final String content 455 = "<html><head><title>foo</title><script>" 456 + "function doTest() {\n" 457 + " var options = document.form1.select1.options;\n" 458 + " options.length=0;\n" 459 + " alert(options.length);\n" 460 + "}</script></head><body onload='doTest()'>" 461 + "<p>hello world</p>" 462 + "<form name='form1'>" 463 + " <select name='select1'>" 464 + " <option name='option1' value='value1'>One</option>" 465 + " <option name='option2' value='value2' selected>Two</option>" 466 + " <option name='option3' value='value3'>Three</option>" 467 + " </select>" 468 + "</form>" 469 + "</body></html>"; 470 471 final List collectedAlerts = new ArrayList(); 472 final HtmlPage page = loadPage(content, collectedAlerts); 473 assertEquals("foo", page.getTitleText()); 474 475 final List expectedAlerts = Arrays.asList( new String[]{ 476 "0" 477 } ); 478 479 assertEquals( expectedAlerts, collectedAlerts ); 480 } 481 482 485 public void testOptionArrayHasItemMethod() throws Exception { 486 final String content 487 = "<html><head><title>foo</title><script>" 488 + "function doTest() {\n" 489 + " var options = document.form1.select1.options;\n" 490 + " alert(options.item(0).text);\n" 491 + " alert(options.item(0).value);\n" 492 + "}</script></head><body onload='doTest()'>" 493 + "<p>hello world</p>" 494 + "<form name='form1'>" 495 + " <select name='select1'>" 496 + " <option name='option1' value='value1'>One</option>" 497 + " <option name='option2' value='value2' selected>Two</option>" 498 + " <option name='option3' value='value3'>Three</option>" 499 + " </select>" 500 + "</form>" 501 + "</body></html>"; 502 503 final List collectedAlerts = new ArrayList(); 504 final HtmlPage page = loadPage(content, collectedAlerts); 505 assertEquals("foo", page.getTitleText()); 506 507 final List expectedAlerts = Arrays.asList( new String[]{ 508 "One", 509 "value1" 510 } ); 511 512 assertEquals( expectedAlerts, collectedAlerts ); 513 } 514 515 518 public void testGetValue() throws Exception { 519 final String content 520 = "<html><head><title>foo</title><script>" 521 + "function doTest() {\n" 522 + " alert(document.form1.select1.value);\n" 523 + "}</script></head><body onload='doTest()'>" 524 + "<p>hello world</p>" 525 + "<form name='form1'>" 526 + " <select name='select1'>" 527 + " <option name='option1'>One</option>" 528 + " <option name='option2' selected is='test'>Two</option>" 529 + " <option name='option3'>Three</option>" 530 + " </select>" 531 + "</form>" 532 + "</body></html>"; 533 534 final List collectedAlerts = new ArrayList(); 535 final HtmlPage page = loadPage(content, collectedAlerts); 536 assertEquals("foo", page.getTitleText()); 537 538 final List expectedAlerts = Arrays.asList( new String[]{ 539 "Two" 540 } ); 541 542 assertEquals( expectedAlerts, collectedAlerts ); 543 } 544 545 549 public void testNoOnchangeFromJS() throws Exception { 550 final String content = "<html><head><title>Test infinite loop on js onchange</title></head>" 551 + "<body><form name='myForm'>" 552 + "<select name='a' onchange='this.form.b.selectedIndex=0'>" 553 + "<option value='1'>one</option>" 554 + "<option value='2'>two</option>" 555 + "</select>" 556 + "<select name='b' onchange='alert(\"b changed\")'>" 557 + "<option value='G'>green</option>" 558 + "<option value='R' selected>red</option>" 559 + "</select>" 560 + "</form>" 561 + "</body>" 562 + "</html>"; 563 final List collectedAlerts = new ArrayList(); 564 final HtmlPage page = loadPage(content, collectedAlerts); 565 final HtmlSelect selectA = page.getFormByName("myForm").getSelectByName("a"); 566 final HtmlOption optionA2 = selectA.getOption(1); 567 568 assertEquals("two", optionA2.asText()); 569 570 final HtmlSelect selectB = page.getFormByName("myForm").getSelectByName("b"); 571 assertEquals(1, selectB.getSelectedOptions().size()); 572 assertEquals("red", ((HtmlOption) selectB.getSelectedOptions().get(0)).asText()); 573 574 optionA2.setSelected(true); 576 assertTrue(optionA2.isSelected()); 577 assertEquals(1, selectB.getSelectedOptions().size()); 578 assertEquals("green", ((HtmlOption) selectB.getSelectedOptions().get(0)).asText()); 579 580 assertEquals(Collections.EMPTY_LIST, collectedAlerts); 581 } 582 583 584 587 public void testSetValue() throws Exception { 588 final String content 589 = "<html><head><title>foo</title><script>" 590 + "function doTest() {\n" 591 + " alert(document.form1.select1.selectedIndex);\n" 592 + " document.form1.select1.value = 'option2';\n" 593 + " alert(document.form1.select1.selectedIndex);\n" 594 + "}</script></head><body onload='doTest()'>" 595 + "<p>hello world</p>" 596 + "<form name='form1' action='http://test'>" 597 + " <select name='select1'>" 598 + " <option value='option1' name='option1'>One</option>" 599 + " <option value='option2' name='option2'>Two</option>" 600 + " </select>" 601 + "</form>" 602 + "</body></html>"; 603 604 final List expectedAlerts = Arrays.asList( new String[]{ 605 "0", "1" 606 } ); 607 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 608 609 final List collectedAlerts = new ArrayList(); 610 final HtmlPage page = loadPage(content, collectedAlerts); 611 assertEquals("foo", page.getTitleText()); 612 assertEquals( expectedAlerts, collectedAlerts ); 613 } 614 } 615 | Popular Tags |