WebDriverActions.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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\Interactions;
  16. use Facebook\WebDriver\Interactions\Internal\WebDriverButtonReleaseAction;
  17. use Facebook\WebDriver\Interactions\Internal\WebDriverClickAction;
  18. use Facebook\WebDriver\Interactions\Internal\WebDriverClickAndHoldAction;
  19. use Facebook\WebDriver\Interactions\Internal\WebDriverContextClickAction;
  20. use Facebook\WebDriver\Interactions\Internal\WebDriverDoubleClickAction;
  21. use Facebook\WebDriver\Interactions\Internal\WebDriverKeyDownAction;
  22. use Facebook\WebDriver\Interactions\Internal\WebDriverKeyUpAction;
  23. use Facebook\WebDriver\Interactions\Internal\WebDriverMouseMoveAction;
  24. use Facebook\WebDriver\Interactions\Internal\WebDriverMoveToOffsetAction;
  25. use Facebook\WebDriver\Interactions\Internal\WebDriverSendKeysAction;
  26. use Facebook\WebDriver\WebDriver;
  27. use Facebook\WebDriver\WebDriverElement;
  28. use Facebook\WebDriver\WebDriverHasInputDevices;
  29. /**
  30. * WebDriver action builder. It implements the builder pattern.
  31. */
  32. class WebDriverActions
  33. {
  34. protected $driver;
  35. protected $keyboard;
  36. protected $mouse;
  37. protected $action;
  38. /**
  39. * @param WebDriverHasInputDevices $driver
  40. */
  41. public function __construct(WebDriverHasInputDevices $driver)
  42. {
  43. $this->driver = $driver;
  44. $this->keyboard = $driver->getKeyboard();
  45. $this->mouse = $driver->getMouse();
  46. $this->action = new WebDriverCompositeAction();
  47. }
  48. /**
  49. * A convenience method for performing the actions without calling build().
  50. */
  51. public function perform()
  52. {
  53. $this->action->perform();
  54. }
  55. /**
  56. * Mouse click.
  57. * If $element is provided, move to the middle of the element first.
  58. *
  59. * @param WebDriverElement $element
  60. * @return WebDriverActions
  61. */
  62. public function click(WebDriverElement $element = null)
  63. {
  64. $this->action->addAction(
  65. new WebDriverClickAction($this->mouse, $element)
  66. );
  67. return $this;
  68. }
  69. /**
  70. * Mouse click and hold.
  71. * If $element is provided, move to the middle of the element first.
  72. *
  73. * @param WebDriverElement $element
  74. * @return WebDriverActions
  75. */
  76. public function clickAndHold(WebDriverElement $element = null)
  77. {
  78. $this->action->addAction(
  79. new WebDriverClickAndHoldAction($this->mouse, $element)
  80. );
  81. return $this;
  82. }
  83. /**
  84. * Context-click (right click).
  85. * If $element is provided, move to the middle of the element first.
  86. *
  87. * @param WebDriverElement $element
  88. * @return WebDriverActions
  89. */
  90. public function contextClick(WebDriverElement $element = null)
  91. {
  92. $this->action->addAction(
  93. new WebDriverContextClickAction($this->mouse, $element)
  94. );
  95. return $this;
  96. }
  97. /**
  98. * Double click.
  99. * If $element is provided, move to the middle of the element first.
  100. *
  101. * @param WebDriverElement $element
  102. * @return WebDriverActions
  103. */
  104. public function doubleClick(WebDriverElement $element = null)
  105. {
  106. $this->action->addAction(
  107. new WebDriverDoubleClickAction($this->mouse, $element)
  108. );
  109. return $this;
  110. }
  111. /**
  112. * Drag and drop from $source to $target.
  113. *
  114. * @param WebDriverElement $source
  115. * @param WebDriverElement $target
  116. * @return WebDriverActions
  117. */
  118. public function dragAndDrop(WebDriverElement $source, WebDriverElement $target)
  119. {
  120. $this->action->addAction(
  121. new WebDriverClickAndHoldAction($this->mouse, $source)
  122. );
  123. $this->action->addAction(
  124. new WebDriverMouseMoveAction($this->mouse, $target)
  125. );
  126. $this->action->addAction(
  127. new WebDriverButtonReleaseAction($this->mouse, $target)
  128. );
  129. return $this;
  130. }
  131. /**
  132. * Drag $source and drop by offset ($x_offset, $y_offset).
  133. *
  134. * @param WebDriverElement $source
  135. * @param int $x_offset
  136. * @param int $y_offset
  137. * @return WebDriverActions
  138. */
  139. public function dragAndDropBy(WebDriverElement $source, $x_offset, $y_offset)
  140. {
  141. $this->action->addAction(
  142. new WebDriverClickAndHoldAction($this->mouse, $source)
  143. );
  144. $this->action->addAction(
  145. new WebDriverMoveToOffsetAction($this->mouse, null, $x_offset, $y_offset)
  146. );
  147. $this->action->addAction(
  148. new WebDriverButtonReleaseAction($this->mouse, null)
  149. );
  150. return $this;
  151. }
  152. /**
  153. * Mouse move by offset.
  154. *
  155. * @param int $x_offset
  156. * @param int $y_offset
  157. * @return WebDriverActions
  158. */
  159. public function moveByOffset($x_offset, $y_offset)
  160. {
  161. $this->action->addAction(
  162. new WebDriverMoveToOffsetAction($this->mouse, null, $x_offset, $y_offset)
  163. );
  164. return $this;
  165. }
  166. /**
  167. * Move to the middle of the given WebDriverElement.
  168. * Extra shift, calculated from the top-left corner of the element, can be set by passing $x_offset and $y_offset
  169. * parameters.
  170. *
  171. * @param WebDriverElement $element
  172. * @param int $x_offset
  173. * @param int $y_offset
  174. * @return WebDriverActions
  175. */
  176. public function moveToElement(WebDriverElement $element, $x_offset = null, $y_offset = null)
  177. {
  178. $this->action->addAction(new WebDriverMoveToOffsetAction(
  179. $this->mouse,
  180. $element,
  181. $x_offset,
  182. $y_offset
  183. ));
  184. return $this;
  185. }
  186. /**
  187. * Release the mouse button.
  188. * If $element is provided, move to the middle of the element first.
  189. *
  190. * @param WebDriverElement $element
  191. * @return WebDriverActions
  192. */
  193. public function release(WebDriverElement $element = null)
  194. {
  195. $this->action->addAction(
  196. new WebDriverButtonReleaseAction($this->mouse, $element)
  197. );
  198. return $this;
  199. }
  200. /**
  201. * Press a key on keyboard.
  202. * If $element is provided, focus on that element first.
  203. *
  204. * @see WebDriverKeys for special keys like CONTROL, ALT, etc.
  205. * @param WebDriverElement $element
  206. * @param string $key
  207. * @return WebDriverActions
  208. */
  209. public function keyDown(WebDriverElement $element = null, $key = null)
  210. {
  211. $this->action->addAction(
  212. new WebDriverKeyDownAction($this->keyboard, $this->mouse, $element, $key)
  213. );
  214. return $this;
  215. }
  216. /**
  217. * Release a key on keyboard.
  218. * If $element is provided, focus on that element first.
  219. *
  220. * @see WebDriverKeys for special keys like CONTROL, ALT, etc.
  221. * @param WebDriverElement $element
  222. * @param string $key
  223. * @return WebDriverActions
  224. */
  225. public function keyUp(WebDriverElement $element = null, $key = null)
  226. {
  227. $this->action->addAction(
  228. new WebDriverKeyUpAction($this->keyboard, $this->mouse, $element, $key)
  229. );
  230. return $this;
  231. }
  232. /**
  233. * Send keys by keyboard.
  234. * If $element is provided, focus on that element first.
  235. *
  236. * @see WebDriverKeys for special keys like CONTROL, ALT, etc.
  237. * @param WebDriverElement $element
  238. * @param string $keys
  239. * @return WebDriverActions
  240. */
  241. public function sendKeys(WebDriverElement $element = null, $keys = null)
  242. {
  243. $this->action->addAction(
  244. new WebDriverSendKeysAction(
  245. $this->keyboard,
  246. $this->mouse,
  247. $element,
  248. $keys
  249. )
  250. );
  251. return $this;
  252. }
  253. }