EventFiringWebDriver.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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\Support\Events;
  16. use Facebook\WebDriver\Exception\UnsupportedOperationException;
  17. use Facebook\WebDriver\Exception\WebDriverException;
  18. use Facebook\WebDriver\Interactions\Touch\WebDriverTouchScreen;
  19. use Facebook\WebDriver\JavaScriptExecutor;
  20. use Facebook\WebDriver\WebDriver;
  21. use Facebook\WebDriver\WebDriverBy;
  22. use Facebook\WebDriver\WebDriverDispatcher;
  23. use Facebook\WebDriver\WebDriverElement;
  24. use Facebook\WebDriver\WebDriverOptions;
  25. use Facebook\WebDriver\WebDriverTargetLocator;
  26. use Facebook\WebDriver\WebDriverWait;
  27. class EventFiringWebDriver implements WebDriver, JavaScriptExecutor
  28. {
  29. /**
  30. * @var WebDriver
  31. */
  32. protected $driver;
  33. /**
  34. * @var WebDriverDispatcher
  35. */
  36. protected $dispatcher;
  37. /**
  38. * @param WebDriver $driver
  39. * @param WebDriverDispatcher $dispatcher
  40. */
  41. public function __construct(WebDriver $driver, WebDriverDispatcher $dispatcher = null)
  42. {
  43. $this->dispatcher = $dispatcher ?: new WebDriverDispatcher();
  44. if (!$this->dispatcher->getDefaultDriver()) {
  45. $this->dispatcher->setDefaultDriver($this);
  46. }
  47. $this->driver = $driver;
  48. }
  49. /**
  50. * @return WebDriverDispatcher
  51. */
  52. public function getDispatcher()
  53. {
  54. return $this->dispatcher;
  55. }
  56. /**
  57. * @return WebDriver
  58. */
  59. public function getWebDriver()
  60. {
  61. return $this->driver;
  62. }
  63. /**
  64. * @param mixed $url
  65. * @throws WebDriverException
  66. * @return $this
  67. */
  68. public function get($url)
  69. {
  70. $this->dispatch('beforeNavigateTo', $url, $this);
  71. try {
  72. $this->driver->get($url);
  73. } catch (WebDriverException $exception) {
  74. $this->dispatchOnException($exception);
  75. throw $exception;
  76. }
  77. $this->dispatch('afterNavigateTo', $url, $this);
  78. return $this;
  79. }
  80. /**
  81. * @param WebDriverBy $by
  82. * @throws WebDriverException
  83. * @return array
  84. */
  85. public function findElements(WebDriverBy $by)
  86. {
  87. $this->dispatch('beforeFindBy', $by, null, $this);
  88. $elements = [];
  89. try {
  90. foreach ($this->driver->findElements($by) as $element) {
  91. $elements[] = $this->newElement($element);
  92. }
  93. } catch (WebDriverException $exception) {
  94. $this->dispatchOnException($exception);
  95. throw $exception;
  96. }
  97. $this->dispatch('afterFindBy', $by, null, $this);
  98. return $elements;
  99. }
  100. /**
  101. * @param WebDriverBy $by
  102. * @throws WebDriverException
  103. * @return EventFiringWebElement
  104. */
  105. public function findElement(WebDriverBy $by)
  106. {
  107. $this->dispatch('beforeFindBy', $by, null, $this);
  108. try {
  109. $element = $this->newElement($this->driver->findElement($by));
  110. } catch (WebDriverException $exception) {
  111. $this->dispatchOnException($exception);
  112. throw $exception;
  113. }
  114. $this->dispatch('afterFindBy', $by, null, $this);
  115. return $element;
  116. }
  117. /**
  118. * @param string $script
  119. * @param array $arguments
  120. * @throws WebDriverException
  121. * @return mixed
  122. */
  123. public function executeScript($script, array $arguments = [])
  124. {
  125. if (!$this->driver instanceof JavaScriptExecutor) {
  126. throw new UnsupportedOperationException(
  127. 'driver does not implement JavaScriptExecutor'
  128. );
  129. }
  130. $this->dispatch('beforeScript', $script, $this);
  131. try {
  132. $result = $this->driver->executeScript($script, $arguments);
  133. } catch (WebDriverException $exception) {
  134. $this->dispatchOnException($exception);
  135. throw $exception;
  136. }
  137. $this->dispatch('afterScript', $script, $this);
  138. return $result;
  139. }
  140. /**
  141. * @param string $script
  142. * @param array $arguments
  143. * @throws WebDriverException
  144. * @return mixed
  145. */
  146. public function executeAsyncScript($script, array $arguments = [])
  147. {
  148. if (!$this->driver instanceof JavaScriptExecutor) {
  149. throw new UnsupportedOperationException(
  150. 'driver does not implement JavaScriptExecutor'
  151. );
  152. }
  153. $this->dispatch('beforeScript', $script, $this);
  154. try {
  155. $result = $this->driver->executeAsyncScript($script, $arguments);
  156. } catch (WebDriverException $exception) {
  157. $this->dispatchOnException($exception);
  158. throw $exception;
  159. }
  160. $this->dispatch('afterScript', $script, $this);
  161. return $result;
  162. }
  163. /**
  164. * @throws WebDriverException
  165. * @return $this
  166. */
  167. public function close()
  168. {
  169. try {
  170. $this->driver->close();
  171. return $this;
  172. } catch (WebDriverException $exception) {
  173. $this->dispatchOnException($exception);
  174. throw $exception;
  175. }
  176. }
  177. /**
  178. * @throws WebDriverException
  179. * @return string
  180. */
  181. public function getCurrentURL()
  182. {
  183. try {
  184. return $this->driver->getCurrentURL();
  185. } catch (WebDriverException $exception) {
  186. $this->dispatchOnException($exception);
  187. throw $exception;
  188. }
  189. }
  190. /**
  191. * @throws WebDriverException
  192. * @return string
  193. */
  194. public function getPageSource()
  195. {
  196. try {
  197. return $this->driver->getPageSource();
  198. } catch (WebDriverException $exception) {
  199. $this->dispatchOnException($exception);
  200. throw $exception;
  201. }
  202. }
  203. /**
  204. * @throws WebDriverException
  205. * @return string
  206. */
  207. public function getTitle()
  208. {
  209. try {
  210. return $this->driver->getTitle();
  211. } catch (WebDriverException $exception) {
  212. $this->dispatchOnException($exception);
  213. throw $exception;
  214. }
  215. }
  216. /**
  217. * @throws WebDriverException
  218. * @return string
  219. */
  220. public function getWindowHandle()
  221. {
  222. try {
  223. return $this->driver->getWindowHandle();
  224. } catch (WebDriverException $exception) {
  225. $this->dispatchOnException($exception);
  226. throw $exception;
  227. }
  228. }
  229. /**
  230. * @throws WebDriverException
  231. * @return array
  232. */
  233. public function getWindowHandles()
  234. {
  235. try {
  236. return $this->driver->getWindowHandles();
  237. } catch (WebDriverException $exception) {
  238. $this->dispatchOnException($exception);
  239. throw $exception;
  240. }
  241. }
  242. /**
  243. * @throws WebDriverException
  244. */
  245. public function quit()
  246. {
  247. try {
  248. $this->driver->quit();
  249. } catch (WebDriverException $exception) {
  250. $this->dispatchOnException($exception);
  251. throw $exception;
  252. }
  253. }
  254. /**
  255. * @param null|string $save_as
  256. * @throws WebDriverException
  257. * @return string
  258. */
  259. public function takeScreenshot($save_as = null)
  260. {
  261. try {
  262. return $this->driver->takeScreenshot($save_as);
  263. } catch (WebDriverException $exception) {
  264. $this->dispatchOnException($exception);
  265. throw $exception;
  266. }
  267. }
  268. /**
  269. * @param int $timeout_in_second
  270. * @param int $interval_in_millisecond
  271. * @throws WebDriverException
  272. * @return WebDriverWait
  273. */
  274. public function wait($timeout_in_second = 30, $interval_in_millisecond = 250)
  275. {
  276. try {
  277. return $this->driver->wait($timeout_in_second, $interval_in_millisecond);
  278. } catch (WebDriverException $exception) {
  279. $this->dispatchOnException($exception);
  280. throw $exception;
  281. }
  282. }
  283. /**
  284. * @throws WebDriverException
  285. * @return WebDriverOptions
  286. */
  287. public function manage()
  288. {
  289. try {
  290. return $this->driver->manage();
  291. } catch (WebDriverException $exception) {
  292. $this->dispatchOnException($exception);
  293. throw $exception;
  294. }
  295. }
  296. /**
  297. * @throws WebDriverException
  298. * @return EventFiringWebDriverNavigation
  299. */
  300. public function navigate()
  301. {
  302. try {
  303. return new EventFiringWebDriverNavigation(
  304. $this->driver->navigate(),
  305. $this->getDispatcher()
  306. );
  307. } catch (WebDriverException $exception) {
  308. $this->dispatchOnException($exception);
  309. throw $exception;
  310. }
  311. }
  312. /**
  313. * @throws WebDriverException
  314. * @return WebDriverTargetLocator
  315. */
  316. public function switchTo()
  317. {
  318. try {
  319. return $this->driver->switchTo();
  320. } catch (WebDriverException $exception) {
  321. $this->dispatchOnException($exception);
  322. throw $exception;
  323. }
  324. }
  325. /**
  326. * @throws WebDriverException
  327. * @return WebDriverTouchScreen
  328. */
  329. public function getTouch()
  330. {
  331. try {
  332. return $this->driver->getTouch();
  333. } catch (WebDriverException $exception) {
  334. $this->dispatchOnException($exception);
  335. throw $exception;
  336. }
  337. }
  338. public function execute($name, $params)
  339. {
  340. try {
  341. return $this->driver->execute($name, $params);
  342. } catch (WebDriverException $exception) {
  343. $this->dispatchOnException($exception);
  344. throw $exception;
  345. }
  346. }
  347. /**
  348. * @param WebDriverElement $element
  349. * @return EventFiringWebElement
  350. */
  351. protected function newElement(WebDriverElement $element)
  352. {
  353. return new EventFiringWebElement($element, $this->getDispatcher());
  354. }
  355. /**
  356. * @param mixed $method
  357. * @param mixed ...$arguments
  358. */
  359. protected function dispatch($method, ...$arguments)
  360. {
  361. if (!$this->dispatcher) {
  362. return;
  363. }
  364. $this->dispatcher->dispatch($method, $arguments);
  365. }
  366. /**
  367. * @param WebDriverException $exception
  368. */
  369. protected function dispatchOnException(WebDriverException $exception)
  370. {
  371. $this->dispatch('onException', $exception, $this);
  372. }
  373. }