ElementWrapper.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 $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 the text of the wrapped element.
  141. *
  142. * If the wrapped element is not visible the returned text is an empty
  143. * string.
  144. *
  145. * @return string the text of the wrapped element, or an empty string if it
  146. * is not visible.
  147. */
  148. public function getText() {
  149. $commandCallback = function() {
  150. return $this->element->getText();
  151. };
  152. return $this->executeCommand($commandCallback, "text could not be got");
  153. }
  154. /**
  155. * Returns the value of the wrapped element.
  156. *
  157. * The value can be got even if the wrapped element is not visible.
  158. *
  159. * @return string the value of the wrapped element.
  160. */
  161. public function getValue() {
  162. $commandCallback = function() {
  163. return $this->element->getValue();
  164. };
  165. return $this->executeCommand($commandCallback, "value could not be got");
  166. }
  167. /**
  168. * Sets the given value on the wrapped element.
  169. *
  170. * If automatically waits for the wrapped element to be visible (up to the
  171. * timeout set when finding it).
  172. *
  173. * @param string $value the value to set.
  174. */
  175. public function setValue($value) {
  176. $commandCallback = function() use ($value) {
  177. $this->element->setValue($value);
  178. };
  179. $this->executeCommandOnVisibleElement($commandCallback, "value could not be set");
  180. }
  181. /**
  182. * Clicks on the wrapped element.
  183. *
  184. * If automatically waits for the wrapped element to be visible (up to the
  185. * timeout set when finding it).
  186. */
  187. public function click() {
  188. $commandCallback = function() {
  189. $this->element->click();
  190. };
  191. $this->executeCommandOnVisibleElement($commandCallback, "could not be clicked");
  192. }
  193. /**
  194. * Executes the given command.
  195. *
  196. * If a StaleElementReference or a NoSuchElement exception is thrown the
  197. * wrapped element is found again and, then, the command is executed again.
  198. *
  199. * @param \Closure $commandCallback the command to execute.
  200. * @param string $errorMessage an error message that describes the failed
  201. * command (appended to the description of the element).
  202. */
  203. private function executeCommand(\Closure $commandCallback, $errorMessage) {
  204. if (!$this->handleFailedCommands) {
  205. return $commandCallback();
  206. }
  207. try {
  208. return $commandCallback();
  209. } catch (\WebDriver\Exception\StaleElementReference $exception) {
  210. $this->printFailedCommandMessage($exception, $errorMessage);
  211. } catch (\WebDriver\Exception\NoSuchElement $exception) {
  212. $this->printFailedCommandMessage($exception, $errorMessage);
  213. }
  214. $this->element = $this->elementFinder->find();
  215. return $commandCallback();
  216. }
  217. /**
  218. * Executes the given command on a visible element.
  219. *
  220. * If a StaleElementReference or a NoSuchElement exception is thrown the
  221. * wrapped element is found again and, then, the command is executed again.
  222. * If an ElementNotVisible or a MoveTargetOutOfBounds exception is thrown it
  223. * is waited for the wrapped element to be visible and, then, the command is
  224. * executed again.
  225. *
  226. * @param \Closure $commandCallback the command to execute.
  227. * @param string $errorMessage an error message that describes the failed
  228. * command (appended to the description of the element).
  229. */
  230. private function executeCommandOnVisibleElement(\Closure $commandCallback, $errorMessage) {
  231. if (!$this->handleFailedCommands) {
  232. return $commandCallback();
  233. }
  234. try {
  235. return $this->executeCommand($commandCallback, $errorMessage);
  236. } catch (\WebDriver\Exception\ElementNotVisible $exception) {
  237. $this->printFailedCommandMessage($exception, $errorMessage);
  238. } catch (\WebDriver\Exception\MoveTargetOutOfBounds $exception) {
  239. $this->printFailedCommandMessage($exception, $errorMessage);
  240. }
  241. $this->waitForElementToBeVisible();
  242. return $commandCallback();
  243. }
  244. /**
  245. * Prints information about the failed command.
  246. *
  247. * @param \Exception exception the exception thrown by the command.
  248. * @param string $errorMessage an error message that describes the failed
  249. * command (appended to the description of the locator of the element).
  250. */
  251. private function printFailedCommandMessage(\Exception $exception, $errorMessage) {
  252. echo $this->elementFinder->getDescription() . " " . $errorMessage . "\n";
  253. echo "Exception message: " . $exception->getMessage() . "\n";
  254. echo "Trying again\n";
  255. }
  256. /**
  257. * Waits for the wrapped element to be visible.
  258. *
  259. * This method waits up to the timeout used when finding the wrapped
  260. * element; therefore, it may return when the element is still not visible.
  261. *
  262. * @return boolean true if the element is visible after the wait, false
  263. * otherwise.
  264. */
  265. private function waitForElementToBeVisible() {
  266. $isVisibleCallback = function() {
  267. return $this->isVisible();
  268. };
  269. $timeout = $this->elementFinder->getTimeout();
  270. $timeoutStep = $this->elementFinder->getTimeoutStep();
  271. return Utils::waitFor($isVisibleCallback, $timeout, $timeoutStep);
  272. }
  273. }