Actor.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * An actor in a test scenario.
  24. *
  25. * Every Actor object is intended to be used only in a single test scenario.
  26. * An Actor can control its web browser thanks to the Mink Session received when
  27. * it was created, so in each scenario each Actor must have its own Mink
  28. * Session; the same Mink Session can be used by different Actors in different
  29. * scenarios, but never by different Actors in the same scenario.
  30. *
  31. * The test servers used in an scenario can change between different test runs,
  32. * so an Actor stores the base URL for the current test server being used; in
  33. * most cases the tests are specified using relative paths that can be converted
  34. * to the appropriate absolute URL using locatePath() in the step
  35. * implementation.
  36. *
  37. * An Actor can find elements in its Mink Session using its find() method; it is
  38. * a wrapper over the find() method provided by Mink that extends it with
  39. * several features: the element can be looked for based on a Locator object, an
  40. * exception is thrown if the element is not found, and, optionally, it is
  41. * possible to try again to find the element several times before giving up.
  42. *
  43. * The returned object is also a wrapper over the element itself that
  44. * automatically handles common causes of failed commands, like clicking on a
  45. * hidden element; in this case, the wrapper would wait for the element to be
  46. * visible up to the timeout set to find the element.
  47. *
  48. * The amount of time to wait before giving up is specified in each call to
  49. * find(). However, a general multiplier to be applied to every timeout can be
  50. * set using setFindTimeoutMultiplier(); this makes possible to retry longer
  51. * before giving up without modifying the tests themselves. Note that the
  52. * multiplier affects the timeout, but not the timeout step; the rate at which
  53. * find() will try again to find the element does not change.
  54. *
  55. * All actors share a notebook in which data can be annotated. This makes
  56. * possible to share data between different test steps, no matter which Actor
  57. * performs them.
  58. */
  59. class Actor {
  60. /**
  61. * @var string
  62. */
  63. private $name;
  64. /**
  65. * @var \Behat\Mink\Session
  66. */
  67. private $session;
  68. /**
  69. * @var string
  70. */
  71. private $baseUrl;
  72. /**
  73. * @var float
  74. */
  75. private $findTimeoutMultiplier;
  76. /**
  77. * @var array
  78. */
  79. private $sharedNotebook;
  80. /**
  81. * Creates a new Actor.
  82. *
  83. * @param string $name the name of the actor.
  84. * @param \Behat\Mink\Session $session the Mink Session used to control its
  85. * web browser.
  86. * @param string $baseUrl the base URL used when solving relative URLs.
  87. * @param array $sharedNotebook the notebook shared between all actors.
  88. */
  89. public function __construct($name, \Behat\Mink\Session $session, $baseUrl, &$sharedNotebook) {
  90. $this->name = $name;
  91. $this->session = $session;
  92. $this->baseUrl = $baseUrl;
  93. $this->sharedNotebook = &$sharedNotebook;
  94. $this->findTimeoutMultiplier = 1;
  95. }
  96. /**
  97. * Returns the name of this Actor.
  98. *
  99. * @return string the name of this Actor.
  100. */
  101. public function getName() {
  102. return $this->name;
  103. }
  104. /**
  105. * Sets the base URL.
  106. *
  107. * @param string $baseUrl the base URL used when solving relative URLs.
  108. */
  109. public function setBaseUrl($baseUrl) {
  110. $this->baseUrl = $baseUrl;
  111. }
  112. /**
  113. * Returns the multiplier for find timeouts.
  114. *
  115. * @return float the multiplier to apply to find timeouts.
  116. */
  117. public function getFindTimeoutMultiplier() {
  118. return $this->findTimeoutMultiplier;
  119. }
  120. /**
  121. * Sets the multiplier for find timeouts.
  122. *
  123. * @param float $findTimeoutMultiplier the multiplier to apply to find
  124. * timeouts.
  125. */
  126. public function setFindTimeoutMultiplier($findTimeoutMultiplier) {
  127. $this->findTimeoutMultiplier = $findTimeoutMultiplier;
  128. }
  129. /**
  130. * Returns the Mink Session used to control its web browser.
  131. *
  132. * @return \Behat\Mink\Session the Mink Session used to control its web
  133. * browser.
  134. */
  135. public function getSession() {
  136. return $this->session;
  137. }
  138. /**
  139. * Returns the full path for the given relative path based on the base URL.
  140. *
  141. * @param string relativePath the relative path.
  142. * @return string the full path.
  143. */
  144. public function locatePath($relativePath) {
  145. return $this->baseUrl . $relativePath;
  146. }
  147. /**
  148. * Finds an element in the Mink Session of this Actor.
  149. *
  150. * The given element locator is relative to its ancestor (either another
  151. * locator or an actual element); if it has no ancestor then the base
  152. * document element is used.
  153. *
  154. * Sometimes an element may not be found simply because it has not appeared
  155. * yet; for those cases this method supports trying again to find the
  156. * element several times before giving up. The timeout parameter controls
  157. * how much time to wait, at most, to find the element; the timeoutStep
  158. * parameter controls how much time to wait before trying again to find the
  159. * element. If ancestor locators need to be found the timeout is applied
  160. * individually to each one, that is, if the timeout is 10 seconds the
  161. * method will wait up to 10 seconds to find the ancestor of the ancestor
  162. * and, then, up to 10 seconds to find the ancestor and, then, up to 10
  163. * seconds to find the element. By default the timeout is 0, so the element
  164. * and its ancestor will be looked for just once; the default time to wait
  165. * before retrying is half a second. If the timeout is not 0 it will be
  166. * affected by the multiplier set using setFindTimeoutMultiplier(), if any.
  167. *
  168. * When found, the element is returned wrapped in an ElementWrapper; the
  169. * ElementWrapper handles common causes of failures when executing commands
  170. * in an element, like clicking on a hidden element.
  171. *
  172. * In any case, if the element, or its ancestors, can not be found a
  173. * NoSuchElementException is thrown.
  174. *
  175. * @param Locator $elementLocator the locator for the element.
  176. * @param float $timeout the number of seconds (decimals allowed) to wait at
  177. * most for the element to appear.
  178. * @param float $timeoutStep the number of seconds (decimals allowed) to
  179. * wait before trying to find the element again.
  180. * @return ElementWrapper an ElementWrapper object for the element.
  181. * @throws NoSuchElementException if the element, or its ancestor, can not
  182. * be found.
  183. */
  184. public function find(Locator $elementLocator, $timeout = 0, $timeoutStep = 0.5) {
  185. $timeout = $timeout * $this->findTimeoutMultiplier;
  186. $elementFinder = new ElementFinder($this->session, $elementLocator, $timeout, $timeoutStep);
  187. return new ElementWrapper($elementFinder);
  188. }
  189. /**
  190. * Returns the shared notebook of the Actors.
  191. *
  192. * @return array the shared notebook of the Actors.
  193. */
  194. public function &getSharedNotebook() {
  195. return $this->sharedNotebook;
  196. }
  197. }