RemoteMouse.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // Copyright 2004-present Facebook. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. namespace Facebook\WebDriver\Remote;
  16. use Facebook\WebDriver\Interactions\Internal\WebDriverCoordinates;
  17. use Facebook\WebDriver\WebDriverMouse;
  18. /**
  19. * Execute mouse commands for RemoteWebDriver.
  20. */
  21. class RemoteMouse implements WebDriverMouse
  22. {
  23. /**
  24. * @var RemoteExecuteMethod
  25. */
  26. private $executor;
  27. /**
  28. * @param RemoteExecuteMethod $executor
  29. */
  30. public function __construct(RemoteExecuteMethod $executor)
  31. {
  32. $this->executor = $executor;
  33. }
  34. /**
  35. * @param null|WebDriverCoordinates $where
  36. *
  37. * @return RemoteMouse
  38. */
  39. public function click(WebDriverCoordinates $where = null)
  40. {
  41. $this->moveIfNeeded($where);
  42. $this->executor->execute(DriverCommand::CLICK, [
  43. 'button' => 0,
  44. ]);
  45. return $this;
  46. }
  47. /**
  48. * @param WebDriverCoordinates $where
  49. *
  50. * @return RemoteMouse
  51. */
  52. public function contextClick(WebDriverCoordinates $where = null)
  53. {
  54. $this->moveIfNeeded($where);
  55. $this->executor->execute(DriverCommand::CLICK, [
  56. 'button' => 2,
  57. ]);
  58. return $this;
  59. }
  60. /**
  61. * @param WebDriverCoordinates $where
  62. *
  63. * @return RemoteMouse
  64. */
  65. public function doubleClick(WebDriverCoordinates $where = null)
  66. {
  67. $this->moveIfNeeded($where);
  68. $this->executor->execute(DriverCommand::DOUBLE_CLICK);
  69. return $this;
  70. }
  71. /**
  72. * @param WebDriverCoordinates $where
  73. *
  74. * @return RemoteMouse
  75. */
  76. public function mouseDown(WebDriverCoordinates $where = null)
  77. {
  78. $this->moveIfNeeded($where);
  79. $this->executor->execute(DriverCommand::MOUSE_DOWN);
  80. return $this;
  81. }
  82. /**
  83. * @param WebDriverCoordinates $where
  84. * @param int|null $x_offset
  85. * @param int|null $y_offset
  86. *
  87. * @return RemoteMouse
  88. */
  89. public function mouseMove(
  90. WebDriverCoordinates $where = null,
  91. $x_offset = null,
  92. $y_offset = null
  93. ) {
  94. $params = [];
  95. if ($where !== null) {
  96. $params['element'] = $where->getAuxiliary();
  97. }
  98. if ($x_offset !== null) {
  99. $params['xoffset'] = $x_offset;
  100. }
  101. if ($y_offset !== null) {
  102. $params['yoffset'] = $y_offset;
  103. }
  104. $this->executor->execute(DriverCommand::MOVE_TO, $params);
  105. return $this;
  106. }
  107. /**
  108. * @param WebDriverCoordinates $where
  109. *
  110. * @return RemoteMouse
  111. */
  112. public function mouseUp(WebDriverCoordinates $where = null)
  113. {
  114. $this->moveIfNeeded($where);
  115. $this->executor->execute(DriverCommand::MOUSE_UP);
  116. return $this;
  117. }
  118. /**
  119. * @param WebDriverCoordinates $where
  120. */
  121. protected function moveIfNeeded(WebDriverCoordinates $where = null)
  122. {
  123. if ($where) {
  124. $this->mouseMove($where);
  125. }
  126. }
  127. }