EventsTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Behat\Mink\Tests\Driver\Js;
  3. use Behat\Mink\Tests\Driver\TestCase;
  4. class EventsTest extends TestCase
  5. {
  6. /**
  7. * @group mouse-events
  8. */
  9. public function testClick()
  10. {
  11. $this->getSession()->visit($this->pathTo('/js_test.html'));
  12. $clicker = $this->getAssertSession()->elementExists('css', '.elements div#clicker');
  13. $this->assertEquals('not clicked', $clicker->getText());
  14. $clicker->click();
  15. $this->assertEquals('single clicked', $clicker->getText());
  16. }
  17. /**
  18. * @group mouse-events
  19. */
  20. public function testDoubleClick()
  21. {
  22. $this->getSession()->visit($this->pathTo('/js_test.html'));
  23. $clicker = $this->getAssertSession()->elementExists('css', '.elements div#clicker');
  24. $this->assertEquals('not clicked', $clicker->getText());
  25. $clicker->doubleClick();
  26. $this->assertEquals('double clicked', $clicker->getText());
  27. }
  28. /**
  29. * @group mouse-events
  30. */
  31. public function testRightClick()
  32. {
  33. $this->getSession()->visit($this->pathTo('/js_test.html'));
  34. $clicker = $this->getAssertSession()->elementExists('css', '.elements div#clicker');
  35. $this->assertEquals('not clicked', $clicker->getText());
  36. $clicker->rightClick();
  37. $this->assertEquals('right clicked', $clicker->getText());
  38. }
  39. /**
  40. * @group mouse-events
  41. */
  42. public function testFocus()
  43. {
  44. $this->getSession()->visit($this->pathTo('/js_test.html'));
  45. $focusBlurDetector = $this->getAssertSession()->elementExists('css', '.elements input#focus-blur-detector');
  46. $this->assertEquals('no action detected', $focusBlurDetector->getValue());
  47. $focusBlurDetector->focus();
  48. $this->assertEquals('focused', $focusBlurDetector->getValue());
  49. }
  50. /**
  51. * @group mouse-events
  52. * @depends testFocus
  53. */
  54. public function testBlur()
  55. {
  56. $this->getSession()->visit($this->pathTo('/js_test.html'));
  57. $focusBlurDetector = $this->getAssertSession()->elementExists('css', '.elements input#focus-blur-detector');
  58. $this->assertEquals('no action detected', $focusBlurDetector->getValue());
  59. $focusBlurDetector->blur();
  60. $this->assertEquals('blured', $focusBlurDetector->getValue());
  61. }
  62. /**
  63. * @group mouse-events
  64. */
  65. public function testMouseOver()
  66. {
  67. $this->getSession()->visit($this->pathTo('/js_test.html'));
  68. $mouseOverDetector = $this->getAssertSession()->elementExists('css', '.elements div#mouseover-detector');
  69. $this->assertEquals('no mouse action detected', $mouseOverDetector->getText());
  70. $mouseOverDetector->mouseOver();
  71. $this->assertEquals('mouse overed', $mouseOverDetector->getText());
  72. }
  73. /**
  74. * @dataProvider provideKeyboardEventsModifiers
  75. */
  76. public function testKeyboardEvents($modifier, $eventProperties)
  77. {
  78. $this->getSession()->visit($this->pathTo('/js_test.html'));
  79. $webAssert = $this->getAssertSession();
  80. $input1 = $webAssert->elementExists('css', '.elements input.input.first');
  81. $input2 = $webAssert->elementExists('css', '.elements input.input.second');
  82. $input3 = $webAssert->elementExists('css', '.elements input.input.third');
  83. $event = $webAssert->elementExists('css', '.elements .text-event');
  84. $input1->keyDown('u', $modifier);
  85. $this->assertEquals('key downed:'.$eventProperties, $event->getText());
  86. $input2->keyPress('r', $modifier);
  87. $this->assertEquals('key pressed:114 / '.$eventProperties, $event->getText());
  88. $input3->keyUp(78, $modifier);
  89. $this->assertEquals('key upped:78 / '.$eventProperties, $event->getText());
  90. }
  91. public function provideKeyboardEventsModifiers()
  92. {
  93. return array(
  94. 'none' => array(null, '0 / 0 / 0 / 0'),
  95. 'alt' => array('alt', '1 / 0 / 0 / 0'),
  96. // jQuery considers ctrl as being a metaKey in the normalized event
  97. 'ctrl' => array('ctrl', '0 / 1 / 0 / 1'),
  98. 'shift' => array('shift', '0 / 0 / 1 / 0'),
  99. 'meta' => array('meta', '0 / 0 / 0 / 1'),
  100. );
  101. }
  102. }