ElementWrapper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Wrapper to automatically handle failed commands on Mink elements.
  24. *
  25. * Commands executed on Mink elements may fail for several reasons. The
  26. * ElementWrapper frees the caller of the commands from handling the most common
  27. * reasons of failure.
  28. *
  29. * StaleElementReference exceptions are thrown when the command is executed on
  30. * an element that is no longer attached to the DOM. This can happen even in
  31. * a chained call like "$actor->find($locator)->click()"; in the milliseconds
  32. * between finding the element and clicking it the element could have been
  33. * removed from the page (for example, if a previous interaction with the page
  34. * started an asynchronous update of the DOM). Every command executed through
  35. * the ElementWrapper is guarded against StaleElementReference exceptions; if
  36. * the element is stale it is found again using the same parameters to find it
  37. * in the first place.
  38. *
  39. * NoSuchElement exceptions are sometimes thrown instead of
  40. * StaleElementReference exceptions. This can happen when the Selenium2 driver
  41. * for Mink performs an action on an element through the WebDriver session
  42. * instead of directly through the WebDriver element. In that case, if the
  43. * element with the given ID does not exist, a NoSuchElement exception would be
  44. * thrown instead of a StaleElementReference exception, so those cases are
  45. * handled like StaleElementReference exceptions.
  46. *
  47. * ElementNotVisible exceptions are thrown when the command requires the element
  48. * to be visible but the element is not. Finding an element only guarantees that
  49. * (at that time) the element is attached to the DOM, but it does not provide
  50. * any guarantee regarding its visibility. Due to that, a call like
  51. * "$actor->find($locator)->click()" can fail if the element was hidden and
  52. * meant to be made visible by a previous interaction with the page, but that
  53. * interaction triggered an asynchronous update that was not finished when the
  54. * click command is executed. All commands executed through the ElementWrapper
  55. * that require the element to be visible are guarded against ElementNotVisible
  56. * exceptions; if the element is not visible it is waited for it to be visible
  57. * up to the timeout set to find it.
  58. *
  59. * MoveTargetOutOfBounds exceptions are sometimes thrown instead of
  60. * ElementNotVisible exceptions. This can happen when the Selenium2 driver for
  61. * Mink moves the cursor on an element using the "moveto" method of the
  62. * WebDriver session, for example, before clicking on an element. In that case,
  63. * if the element is not visible, "moveto" would throw a MoveTargetOutOfBounds
  64. * exception instead of an ElementNotVisible exception, so those cases are
  65. * handled like ElementNotVisible exceptions.
  66. *
  67. * Despite the automatic handling it is possible for the commands to throw those
  68. * exceptions when they are executed again; this class does not handle cases
  69. * like an element becoming stale several times in a row (uncommon) or an
  70. * element not becoming visible before the timeout expires (which would mean
  71. * that the timeout is too short or that the test has to, indeed, fail). In a
  72. * similar way, MoveTargetOutOfBounds exceptions would be thrown again if
  73. * originally they were thrown because the element was visible but "out of
  74. * reach".
  75. *
  76. * If needed, automatically handling failed commands can be disabled calling
  77. * "doNotHandleFailedCommands()"; as it returns the ElementWrapper it can be
  78. * chained with the command to execute (but note that automatically handling
  79. * failed commands will still be disabled if further commands are executed on
  80. * the ElementWrapper).
  81. */
  82. class ElementWrapper {
  83. /**
  84. * @var ElementFinder
  85. */
  86. private $elementFinder;
  87. /**
  88. * @var \Behat\Mink\Element\Element
  89. */
  90. private $element;
  91. /**
  92. * @param boolean
  93. */
  94. private $handleFailedCommands;
  95. /**
  96. * Creates a new ElementWrapper.
  97. *
  98. * The wrapped element is found in the constructor itself using the
  99. * ElementFinder.
  100. *
  101. * @param ElementFinder $elementFinder the command object to find the
  102. * wrapped element.
  103. * @throws NoSuchElementException if the element, or its ancestor, can not
  104. * be found.
  105. */
  106. public function __construct(ElementFinder $elementFinder) {
  107. $this->elementFinder = $elementFinder;
  108. $this->element = $elementFinder->find();
  109. $this->handleFailedCommands = true;
  110. }
  111. /**
  112. * Returns the raw Mink element.
  113. *
  114. * @return \Behat\Mink\Element\Element the wrapped element.
  115. */
  116. public function getWrappedElement() {
  117. return $this->element;
  118. }
  119. /**
  120. * Prevents the automatic handling of failed commands.
  121. *
  122. * @return ElementWrapper this ElementWrapper.
  123. */
  124. public function doNotHandleFailedCommands() {
  125. $this->handleFailedCommands = false;
  126. return $this;
  127. }
  128. /**
  129. * Returns whether the wrapped element is visible or not.
  130. *
  131. * @return bool true if the wrapped element is visible, false otherwise.
  132. */
  133. public function isVisible() {
  134. $commandCallback = function() {
  135. return $this->element->isVisible();
  136. };
  137. return $this->executeCommand($commandCallback, "visibility could not be got");
  138. }
  139. /**
  140. * Returns whether the wrapped element is checked or not.
  141. *
  142. * @return bool true if the wrapped element is checked, false otherwise.
  143. */
  144. public function isChecked() {
  145. $commandCallback = function() {
  146. return $this->element->isChecked();
  147. };
  148. return $this->executeCommand($commandCallback, "check state could not be got");
  149. }
  150. /**
  151. * Returns the text of the wrapped element.
  152. *
  153. * If the wrapped element is not visible the returned text is an empty
  154. * string.
  155. *
  156. * @return string the text of the wrapped element, or an empty string if it
  157. * is not visible.
  158. */
  159. public function getText() {
  160. $commandCallback = function() {
  161. return $this->element->getText();
  162. };
  163. return $this->executeCommand($commandCallback, "text could not be got");
  164. }
  165. /**
  166. * Returns the value of the wrapped element.
  167. *
  168. * The value can be got even if the wrapped element is not visible.
  169. *
  170. * @return string the value of the wrapped element.
  171. */
  172. public function getValue() {
  173. $commandCallback = function() {
  174. return $this->element->getValue();
  175. };
  176. return $this->executeCommand($commandCallback, "value could not be got");
  177. }
  178. /**
  179. * Sets the given value on the wrapped element.
  180. *
  181. * If automatically waits for the wrapped element to be visible (up to the
  182. * timeout set when finding it).
  183. *
  184. * @param string $value the value to set.
  185. */
  186. public function setValue($value) {
  187. $commandCallback = function() use ($value) {
  188. $this->element->setValue($value);
  189. };
  190. $this->executeCommandOnVisibleElement($commandCallback, "value could not be set");
  191. }
  192. /**
  193. * Clicks on the wrapped element.
  194. *
  195. * If automatically waits for the wrapped element to be visible (up to the
  196. * timeout set when finding it).
  197. */
  198. public function click() {
  199. $commandCallback = function() {
  200. $this->element->click();
  201. };
  202. $this->executeCommandOnVisibleElement($commandCallback, "could not be clicked");
  203. }
  204. /**
  205. * Check the wrapped element.
  206. *
  207. * If automatically waits for the wrapped element to be visible (up to the
  208. * timeout set when finding it).
  209. */
  210. public function check() {
  211. $commandCallback = function() {
  212. $this->element->check();
  213. };
  214. $this->executeCommand($commandCallback, "could not be checked");
  215. }
  216. /**
  217. * uncheck the wrapped element.
  218. *
  219. * If automatically waits for the wrapped element to be visible (up to the
  220. * timeout set when finding it).
  221. */
  222. public function uncheck() {
  223. $commandCallback = function() {
  224. $this->element->uncheck();
  225. };
  226. $this->executeCommand($commandCallback, "could not be unchecked");
  227. }
  228. /**
  229. * Executes the given command.
  230. *
  231. * If a StaleElementReference or a NoSuchElement exception is thrown the
  232. * wrapped element is found again and, then, the command is executed again.
  233. *
  234. * @param \Closure $commandCallback the command to execute.
  235. * @param string $errorMessage an error message that describes the failed
  236. * command (appended to the description of the element).
  237. */
  238. private function executeCommand(\Closure $commandCallback, $errorMessage) {
  239. if (!$this->handleFailedCommands) {
  240. return $commandCallback();
  241. }
  242. try {
  243. return $commandCallback();
  244. } catch (\WebDriver\Exception\StaleElementReference $exception) {
  245. $this->printFailedCommandMessage($exception, $errorMessage);
  246. } catch (\WebDriver\Exception\NoSuchElement $exception) {
  247. $this->printFailedCommandMessage($exception, $errorMessage);
  248. }
  249. $this->element = $this->elementFinder->find();
  250. return $commandCallback();
  251. }
  252. /**
  253. * Executes the given command on a visible element.
  254. *
  255. * If a StaleElementReference or a NoSuchElement exception is thrown the
  256. * wrapped element is found again and, then, the command is executed again.
  257. * If an ElementNotVisible or a MoveTargetOutOfBounds exception is thrown it
  258. * is waited for the wrapped element to be visible and, then, the command is
  259. * executed again.
  260. *
  261. * @param \Closure $commandCallback the command to execute.
  262. * @param string $errorMessage an error message that describes the failed
  263. * command (appended to the description of the element).
  264. */
  265. private function executeCommandOnVisibleElement(\Closure $commandCallback, $errorMessage) {
  266. if (!$this->handleFailedCommands) {
  267. return $commandCallback();
  268. }
  269. try {
  270. return $this->executeCommand($commandCallback, $errorMessage);
  271. } catch (\WebDriver\Exception\ElementNotVisible $exception) {
  272. $this->printFailedCommandMessage($exception, $errorMessage);
  273. } catch (\WebDriver\Exception\MoveTargetOutOfBounds $exception) {
  274. $this->printFailedCommandMessage($exception, $errorMessage);
  275. }
  276. $this->waitForElementToBeVisible();
  277. return $commandCallback();
  278. }
  279. /**
  280. * Prints information about the failed command.
  281. *
  282. * @param \Exception exception the exception thrown by the command.
  283. * @param string $errorMessage an error message that describes the failed
  284. * command (appended to the description of the locator of the element).
  285. */
  286. private function printFailedCommandMessage(\Exception $exception, $errorMessage) {
  287. echo $this->elementFinder->getDescription() . " " . $errorMessage . "\n";
  288. echo "Exception message: " . $exception->getMessage() . "\n";
  289. echo "Trying again\n";
  290. }
  291. /**
  292. * Waits for the wrapped element to be visible.
  293. *
  294. * This method waits up to the timeout used when finding the wrapped
  295. * element; therefore, it may return when the element is still not visible.
  296. *
  297. * @return boolean true if the element is visible after the wait, false
  298. * otherwise.
  299. */
  300. private function waitForElementToBeVisible() {
  301. $isVisibleCallback = function() {
  302. return $this->isVisible();
  303. };
  304. $timeout = $this->elementFinder->getTimeout();
  305. $timeoutStep = $this->elementFinder->getTimeoutStep();
  306. return Utils::waitFor($isVisibleCallback, $timeout, $timeoutStep);
  307. }
  308. }