SelectTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Behat\Mink\Tests\Driver\Form;
  3. use Behat\Mink\Tests\Driver\TestCase;
  4. class SelectTest extends TestCase
  5. {
  6. public function testMultiselect()
  7. {
  8. $this->getSession()->visit($this->pathTo('/multiselect_form.html'));
  9. $webAssert = $this->getAssertSession();
  10. $page = $this->getSession()->getPage();
  11. $this->assertEquals('Multiselect Test', $webAssert->elementExists('css', 'h1')->getText());
  12. $select = $webAssert->fieldExists('select_number');
  13. $multiSelect = $webAssert->fieldExists('select_multiple_numbers[]');
  14. $secondMultiSelect = $webAssert->fieldExists('select_multiple_values[]');
  15. $this->assertEquals('20', $select->getValue());
  16. $this->assertSame(array(), $multiSelect->getValue());
  17. $this->assertSame(array('2', '3'), $secondMultiSelect->getValue());
  18. $select->selectOption('thirty');
  19. $this->assertEquals('30', $select->getValue());
  20. $multiSelect->selectOption('one', true);
  21. $this->assertSame(array('1'), $multiSelect->getValue());
  22. $multiSelect->selectOption('three', true);
  23. $this->assertEquals(array('1', '3'), $multiSelect->getValue());
  24. $secondMultiSelect->selectOption('two');
  25. $this->assertSame(array('2'), $secondMultiSelect->getValue());
  26. $button = $page->findButton('Register');
  27. $this->assertNotNull($button);
  28. $button->press();
  29. $space = ' ';
  30. $out = <<<OUT
  31. 'agreement' = 'off',
  32. 'select_multiple_numbers' =$space
  33. array (
  34. 0 = '1',
  35. 1 = '3',
  36. ),
  37. 'select_multiple_values' =$space
  38. array (
  39. 0 = '2',
  40. ),
  41. 'select_number' = '30',
  42. OUT;
  43. $this->assertContains($out, $page->getContent());
  44. }
  45. /**
  46. * @dataProvider testElementSelectedStateCheckDataProvider
  47. */
  48. public function testElementSelectedStateCheck($selectName, $optionValue, $optionText)
  49. {
  50. $session = $this->getSession();
  51. $webAssert = $this->getAssertSession();
  52. $session->visit($this->pathTo('/multiselect_form.html'));
  53. $select = $webAssert->fieldExists($selectName);
  54. $option = $webAssert->elementExists('named', array('option', $optionValue));
  55. $this->assertFalse($option->isSelected());
  56. $select->selectOption($optionText);
  57. $this->assertTrue($option->isSelected());
  58. }
  59. public function testElementSelectedStateCheckDataProvider()
  60. {
  61. return array(
  62. array('select_number', '30', 'thirty'),
  63. array('select_multiple_numbers[]', '2', 'two'),
  64. );
  65. }
  66. public function testSetValueSingleSelect()
  67. {
  68. $session = $this->getSession();
  69. $session->visit($this->pathTo('/multiselect_form.html'));
  70. $select = $this->getAssertSession()->fieldExists('select_number');
  71. $select->setValue('10');
  72. $this->assertEquals('10', $select->getValue());
  73. }
  74. public function testSetValueMultiSelect()
  75. {
  76. $session = $this->getSession();
  77. $session->visit($this->pathTo('/multiselect_form.html'));
  78. $select = $this->getAssertSession()->fieldExists('select_multiple_values[]');
  79. $select->setValue(array('1', '2'));
  80. $this->assertEquals(array('1', '2'), $select->getValue());
  81. }
  82. /**
  83. * @see https://github.com/Behat/Mink/issues/193
  84. */
  85. public function testOptionWithoutValue()
  86. {
  87. $session = $this->getSession();
  88. $session->visit($this->pathTo('/issue193.html'));
  89. $session->getPage()->selectFieldOption('options-without-values', 'Two');
  90. $this->assertEquals('Two', $this->findById('options-without-values')->getValue());
  91. $this->assertTrue($this->findById('two')->isSelected());
  92. $this->assertFalse($this->findById('one')->isSelected());
  93. $session->getPage()->selectFieldOption('options-with-values', 'two');
  94. $this->assertEquals('two', $this->findById('options-with-values')->getValue());
  95. }
  96. /**
  97. * @see https://github.com/Behat/Mink/issues/131
  98. */
  99. public function testAccentuatedOption()
  100. {
  101. $this->getSession()->visit($this->pathTo('/issue131.html'));
  102. $page = $this->getSession()->getPage();
  103. $page->selectFieldOption('foobar', 'Gimme some accentués characters');
  104. $this->assertEquals('1', $page->findField('foobar')->getValue());
  105. }
  106. }