EventFiringWebElement.php 10.0 KB

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