KeyboardApiTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * This file is part of Chrome PHP.
  4. *
  5. * (c) Soufiane Ghzal <sghzal@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace HeadlessChromium\Test;
  11. use HeadlessChromium\Browser;
  12. use HeadlessChromium\BrowserFactory;
  13. /**
  14. * @covers \HeadlessChromium\Input\Keyboard
  15. */
  16. class KeyboardApiTest extends BaseTestCase
  17. {
  18. /**
  19. * @var Browser\ProcessAwareBrowser
  20. */
  21. public static $browser;
  22. public static function setUpBeforeClass(): void
  23. {
  24. parent::setUpBeforeClass();
  25. $factory = new BrowserFactory();
  26. self::$browser = $factory->createBrowser();
  27. }
  28. public static function tearDownAfterClass(): void
  29. {
  30. parent::tearDownAfterClass();
  31. self::$browser->close();
  32. }
  33. private function openSitePage($file)
  34. {
  35. $page = self::$browser->createPage();
  36. $page->navigate(self::sitePath($file))->waitForNavigation();
  37. return $page;
  38. }
  39. /**
  40. * @throws \HeadlessChromium\Exception\CommunicationException
  41. * @throws \HeadlessChromium\Exception\NoResponseAvailable
  42. */
  43. public function testTypeText(): void
  44. {
  45. // initial navigation
  46. $page = $this->openSitePage('form.html');
  47. $page->keyboard()
  48. ->type('Tab')
  49. ->typeText('bar');
  50. $value = $page
  51. ->evaluate('document.querySelector("#myinput").value;')
  52. ->getReturnValue();
  53. // checks if the input contains the typed text
  54. $this->assertEquals('bar', $value);
  55. }
  56. /**
  57. * @throws \HeadlessChromium\Exception\CommunicationException
  58. * @throws \HeadlessChromium\Exception\NoResponseAvailable
  59. */
  60. public function testTypeRawKey(): void
  61. {
  62. // initial navigation
  63. $page = $this->openSitePage('form.html');
  64. // the initial focus should not be #myinput
  65. $value = $page
  66. ->evaluate('document.activeElement === document.querySelector("#myinput");')
  67. ->getReturnValue();
  68. $this->assertFalse($value);
  69. // press the Tab key
  70. $page->keyboard()->typeRawKey('Tab');
  71. // test the the focus switched to #myinput
  72. $value = $page
  73. ->evaluate('document.activeElement === document.querySelector("#myinput");')
  74. ->getReturnValue();
  75. $this->assertTrue($value);
  76. }
  77. /**
  78. * @throws \HeadlessChromium\Exception\CommunicationException
  79. * @throws \HeadlessChromium\Exception\NoResponseAvailable
  80. */
  81. public function testTypeKeyCombinations(): void
  82. {
  83. // initial navigation
  84. $page = $this->openSitePage('form.html');
  85. $text = 'bar';
  86. // select an input and type a random text
  87. $page->keyboard()
  88. ->typeRawKey('Tab')
  89. ->typeText($text);
  90. // select all the text using ctrl + a
  91. $page->keyboard()
  92. ->press(' control ') // key names should be case insensitive and trimmed
  93. ->type('a')
  94. ->release('Control');
  95. // type ctrl + c to copy the selected text and paste it twice with ctrl + v
  96. $page->keyboard()
  97. ->press('Ctrl') // aliases sould work
  98. ->type('c')
  99. ->type('V') // upper and lower case should behave the same way
  100. ->type('v')
  101. ->release();
  102. $value = $page
  103. ->evaluate('document.querySelector("#myinput").value;')
  104. ->getReturnValue();
  105. // check if the input contains the typed text twice
  106. $this->assertEquals($text.$text, $value);
  107. }
  108. /**
  109. * @throws \HeadlessChromium\Exception\CommunicationException
  110. * @throws \HeadlessChromium\Exception\NoResponseAvailable
  111. */
  112. public function testReleaseAll(): void
  113. {
  114. // initial navigation
  115. $page = $this->openSitePage('form.html');
  116. $page->keyboard()
  117. ->press('a')
  118. ->press('b')
  119. ->release();
  120. $this->assertEquals(0, \count($page->keyboard()->getPressedKeys()));
  121. }
  122. /**
  123. * @throws \HeadlessChromium\Exception\CommunicationException
  124. * @throws \HeadlessChromium\Exception\NoResponseAvailable
  125. */
  126. public function testKeyInterval(): void
  127. {
  128. // initial navigation
  129. $page = $this->openSitePage('form.html');
  130. $start = \round(\microtime(true) * 1000);
  131. $page->keyboard()
  132. ->setKeyInterval(100)
  133. ->typeRawKey('Tab')
  134. ->typeText('bar');
  135. $millisecondsElapsed = \round(\microtime(true) * 1000) - $start;
  136. // if this test takes less than 300ms to run (3 keys x 100ms), setKeyInterval is not working
  137. $this->assertGreaterThan(300, $millisecondsElapsed);
  138. }
  139. }