ChangeEventTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Behat\Mink\Tests\Driver\Js;
  3. use Behat\Mink\Tests\Driver\TestCase;
  4. /**
  5. * @group slow
  6. */
  7. class ChangeEventTest extends TestCase
  8. {
  9. /**
  10. * 'change' event should be fired after selecting an <option> in a <select>.
  11. *
  12. * TODO check whether this test is redundant with other change event tests.
  13. */
  14. public function testIssue255()
  15. {
  16. $session = $this->getSession();
  17. $session->visit($this->pathTo('/issue255.html'));
  18. $session->getPage()->selectFieldOption('foo_select', 'Option 3');
  19. $session->wait(2000, '$("#output_foo_select").text() != ""');
  20. $this->assertEquals('onChangeSelect', $this->getAssertSession()->elementExists('css', '#output_foo_select')->getText());
  21. }
  22. public function testIssue178()
  23. {
  24. $session = $this->getSession();
  25. $session->visit($this->pathTo('/issue178.html'));
  26. $this->findById('source')->setValue('foo');
  27. $this->assertEquals('foo', $this->findById('target')->getText());
  28. }
  29. /**
  30. * @dataProvider setValueChangeEventDataProvider
  31. * @group change-event-detector
  32. */
  33. public function testSetValueChangeEvent($elementId, $valueForEmpty, $valueForFilled = '')
  34. {
  35. $this->getSession()->visit($this->pathTo('/element_change_detector.html'));
  36. $page = $this->getSession()->getPage();
  37. $input = $this->findById($elementId);
  38. $this->assertNull($page->findById($elementId.'-result'));
  39. // Verify setting value, when control is initially empty.
  40. $input->setValue($valueForEmpty);
  41. $this->assertElementChangeCount($elementId, 'initial value setting triggers change event');
  42. if ($valueForFilled) {
  43. // Verify setting value, when control already has a value.
  44. $this->findById('results')->click();
  45. $input->setValue($valueForFilled);
  46. $this->assertElementChangeCount($elementId, 'value change triggers change event');
  47. }
  48. }
  49. public function setValueChangeEventDataProvider()
  50. {
  51. return array(
  52. 'input default' => array('the-input-default', 'from empty', 'from existing'),
  53. 'input text' => array('the-input-text', 'from empty', 'from existing'),
  54. 'input email' => array('the-email', 'from empty', 'from existing'),
  55. 'textarea' => array('the-textarea', 'from empty', 'from existing'),
  56. 'file' => array('the-file', 'from empty', 'from existing'),
  57. 'select' => array('the-select', '30'),
  58. 'radio' => array('the-radio-m', 'm'),
  59. );
  60. }
  61. /**
  62. * @dataProvider selectOptionChangeEventDataProvider
  63. * @group change-event-detector
  64. */
  65. public function testSelectOptionChangeEvent($elementId, $elementValue)
  66. {
  67. $this->getSession()->visit($this->pathTo('/element_change_detector.html'));
  68. $page = $this->getSession()->getPage();
  69. $input = $this->findById($elementId);
  70. $this->assertNull($page->findById($elementId.'-result'));
  71. $input->selectOption($elementValue);
  72. $this->assertElementChangeCount($elementId);
  73. }
  74. public function selectOptionChangeEventDataProvider()
  75. {
  76. return array(
  77. 'select' => array('the-select', '30'),
  78. 'radio' => array('the-radio-m', 'm'),
  79. );
  80. }
  81. /**
  82. * @dataProvider checkboxTestWayDataProvider
  83. * @group change-event-detector
  84. */
  85. public function testCheckChangeEvent($useSetValue)
  86. {
  87. $this->getSession()->visit($this->pathTo('/element_change_detector.html'));
  88. $page = $this->getSession()->getPage();
  89. $checkbox = $this->findById('the-unchecked-checkbox');
  90. $this->assertNull($page->findById('the-unchecked-checkbox-result'));
  91. if ($useSetValue) {
  92. $checkbox->setValue(true);
  93. } else {
  94. $checkbox->check();
  95. }
  96. $this->assertElementChangeCount('the-unchecked-checkbox');
  97. }
  98. /**
  99. * @dataProvider checkboxTestWayDataProvider
  100. * @group change-event-detector
  101. */
  102. public function testUncheckChangeEvent($useSetValue)
  103. {
  104. $this->getSession()->visit($this->pathTo('/element_change_detector.html'));
  105. $page = $this->getSession()->getPage();
  106. $checkbox = $this->findById('the-checked-checkbox');
  107. $this->assertNull($page->findById('the-checked-checkbox-result'));
  108. if ($useSetValue) {
  109. $checkbox->setValue(false);
  110. } else {
  111. $checkbox->uncheck();
  112. }
  113. $this->assertElementChangeCount('the-checked-checkbox');
  114. }
  115. public function checkboxTestWayDataProvider()
  116. {
  117. return array(
  118. array(true),
  119. array(false),
  120. );
  121. }
  122. private function assertElementChangeCount($elementId, $message = '')
  123. {
  124. $counterElement = $this->getSession()->getPage()->findById($elementId.'-result');
  125. $actualCount = null === $counterElement ? 0 : $counterElement->getText();
  126. $this->assertEquals('1', $actualCount, $message);
  127. }
  128. }