2017-04-04 17:18:05 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An actor in a test scenario.
|
|
|
|
*
|
|
|
|
* Every Actor object is intended to be used only in a single test scenario.
|
|
|
|
* An Actor can control its web browser thanks to the Mink Session received when
|
|
|
|
* it was created, so in each scenario each Actor must have its own Mink
|
|
|
|
* Session; the same Mink Session can be used by different Actors in different
|
|
|
|
* scenarios, but never by different Actors in the same scenario.
|
|
|
|
*
|
|
|
|
* The test servers used in an scenario can change between different test runs,
|
|
|
|
* so an Actor stores the base URL for the current test server being used; in
|
|
|
|
* most cases the tests are specified using relative paths that can be converted
|
|
|
|
* to the appropriate absolute URL using locatePath() in the step
|
|
|
|
* implementation.
|
2017-04-04 17:25:22 +03:00
|
|
|
*
|
|
|
|
* An Actor can find elements in its Mink Session using its find() method; it is
|
|
|
|
* a wrapper over the find() method provided by Mink that extends it with
|
|
|
|
* several features: the element can be looked for based on a Locator object, an
|
|
|
|
* exception is thrown if the element is not found, and, optionally, it is
|
|
|
|
* possible to try again to find the element several times before giving up.
|
2017-04-15 19:56:24 +03:00
|
|
|
*
|
|
|
|
* The amount of time to wait before giving up is specified in each call to
|
|
|
|
* find(). However, a general multiplier to be applied to every timeout can be
|
|
|
|
* set using setFindTimeoutMultiplier(); this makes possible to retry longer
|
|
|
|
* before giving up without modifying the tests themselves. Note that the
|
|
|
|
* multiplier affects the timeout, but not the timeout step; the rate at which
|
|
|
|
* find() will try again to find the element does not change.
|
2017-04-23 19:40:58 +03:00
|
|
|
*
|
|
|
|
* All actors share a notebook in which data can be annotated. This makes
|
|
|
|
* possible to share data between different test steps, no matter which Actor
|
|
|
|
* performs them.
|
2017-04-04 17:18:05 +03:00
|
|
|
*/
|
|
|
|
class Actor {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Behat\Mink\Session
|
|
|
|
*/
|
|
|
|
private $session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $baseUrl;
|
|
|
|
|
2017-04-15 19:56:24 +03:00
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $findTimeoutMultiplier;
|
|
|
|
|
2017-04-23 19:40:58 +03:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $sharedNotebook;
|
|
|
|
|
2017-04-04 17:18:05 +03:00
|
|
|
/**
|
|
|
|
* Creates a new Actor.
|
|
|
|
*
|
|
|
|
* @param \Behat\Mink\Session $session the Mink Session used to control its
|
|
|
|
* web browser.
|
|
|
|
* @param string $baseUrl the base URL used when solving relative URLs.
|
2017-04-23 19:40:58 +03:00
|
|
|
* @param array $sharedNotebook the notebook shared between all actors.
|
2017-04-04 17:18:05 +03:00
|
|
|
*/
|
2017-04-23 19:40:58 +03:00
|
|
|
public function __construct(\Behat\Mink\Session $session, $baseUrl, &$sharedNotebook) {
|
2017-04-04 17:18:05 +03:00
|
|
|
$this->session = $session;
|
|
|
|
$this->baseUrl = $baseUrl;
|
2017-04-23 19:40:58 +03:00
|
|
|
$this->sharedNotebook = &$sharedNotebook;
|
2017-04-15 19:56:24 +03:00
|
|
|
$this->findTimeoutMultiplier = 1;
|
2017-04-04 17:18:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the base URL.
|
|
|
|
*
|
|
|
|
* @param string $baseUrl the base URL used when solving relative URLs.
|
|
|
|
*/
|
|
|
|
public function setBaseUrl($baseUrl) {
|
|
|
|
$this->baseUrl = $baseUrl;
|
|
|
|
}
|
|
|
|
|
2017-04-15 19:56:24 +03:00
|
|
|
/**
|
|
|
|
* Sets the multiplier for find timeouts.
|
|
|
|
*
|
|
|
|
* @param float $findTimeoutMultiplier the multiplier to apply to find
|
|
|
|
* timeouts.
|
|
|
|
*/
|
|
|
|
public function setFindTimeoutMultiplier($findTimeoutMultiplier) {
|
|
|
|
$this->findTimeoutMultiplier = $findTimeoutMultiplier;
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:18:05 +03:00
|
|
|
/**
|
|
|
|
* Returns the Mink Session used to control its web browser.
|
|
|
|
*
|
|
|
|
* @return \Behat\Mink\Session the Mink Session used to control its web
|
|
|
|
* browser.
|
|
|
|
*/
|
|
|
|
public function getSession() {
|
|
|
|
return $this->session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the full path for the given relative path based on the base URL.
|
|
|
|
*
|
|
|
|
* @param string relativePath the relative path.
|
|
|
|
* @return string the full path.
|
|
|
|
*/
|
|
|
|
public function locatePath($relativePath) {
|
|
|
|
return $this->baseUrl . $relativePath;
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:25:22 +03:00
|
|
|
/**
|
|
|
|
* Finds an element in the Mink Session of this Actor.
|
|
|
|
*
|
|
|
|
* The given element locator is relative to its ancestor (either another
|
|
|
|
* locator or an actual element); if it has no ancestor then the base
|
|
|
|
* document element is used.
|
|
|
|
*
|
|
|
|
* Sometimes an element may not be found simply because it has not appeared
|
|
|
|
* yet; for those cases this method supports trying again to find the
|
|
|
|
* element several times before giving up. The timeout parameter controls
|
|
|
|
* how much time to wait, at most, to find the element; the timeoutStep
|
|
|
|
* parameter controls how much time to wait before trying again to find the
|
|
|
|
* element. If ancestor locators need to be found the timeout is applied
|
|
|
|
* individually to each one, that is, if the timeout is 10 seconds the
|
|
|
|
* method will wait up to 10 seconds to find the ancestor of the ancestor
|
|
|
|
* and, then, up to 10 seconds to find the ancestor and, then, up to 10
|
|
|
|
* seconds to find the element. By default the timeout is 0, so the element
|
|
|
|
* and its ancestor will be looked for just once; the default time to wait
|
2017-04-15 19:56:24 +03:00
|
|
|
* before retrying is half a second. If the timeout is not 0 it will be
|
|
|
|
* affected by the multiplier set using setFindTimeoutMultiplier(), if any.
|
2017-04-04 17:25:22 +03:00
|
|
|
*
|
|
|
|
* In any case, if the element, or its ancestors, can not be found a
|
|
|
|
* NoSuchElementException is thrown.
|
|
|
|
*
|
|
|
|
* @param Locator $elementLocator the locator for the element.
|
|
|
|
* @param float $timeout the number of seconds (decimals allowed) to wait at
|
|
|
|
* most for the element to appear.
|
|
|
|
* @param float $timeoutStep the number of seconds (decimals allowed) to
|
|
|
|
* wait before trying to find the element again.
|
|
|
|
* @return \Behat\Mink\Element\Element the element found.
|
|
|
|
* @throws NoSuchElementException if the element, or its ancestor, can not
|
|
|
|
* be found.
|
|
|
|
*/
|
2017-05-04 13:10:02 +03:00
|
|
|
public function find(Locator $elementLocator, $timeout = 0, $timeoutStep = 0.5) {
|
2017-04-15 19:56:24 +03:00
|
|
|
$timeout = $timeout * $this->findTimeoutMultiplier;
|
|
|
|
|
2017-05-04 13:35:01 +03:00
|
|
|
return self::findInternal($this->session, $elementLocator, $timeout, $timeoutStep);
|
2017-05-03 13:34:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-04 13:35:01 +03:00
|
|
|
* Finds an element in the given Mink Session.
|
2017-05-03 13:34:04 +03:00
|
|
|
*
|
|
|
|
* The timeout is not affected by the multiplier set using
|
|
|
|
* setFindTimeoutMultiplier().
|
|
|
|
*
|
2017-05-04 13:35:01 +03:00
|
|
|
* @see find($session, $elementLocator, $timeout, $timeoutStep)
|
2017-05-03 13:34:04 +03:00
|
|
|
*/
|
2017-05-04 13:35:01 +03:00
|
|
|
private static function findInternal(\Behat\Mink\Session $session, Locator $elementLocator, $timeout, $timeoutStep) {
|
2017-04-04 17:25:22 +03:00
|
|
|
$element = null;
|
|
|
|
$selector = $elementLocator->getSelector();
|
|
|
|
$locator = $elementLocator->getLocator();
|
2017-05-04 13:35:01 +03:00
|
|
|
$ancestorElement = self::findAncestorElement($session, $elementLocator, $timeout, $timeoutStep);
|
2017-04-04 17:25:22 +03:00
|
|
|
|
|
|
|
$findCallback = function() use (&$element, $selector, $locator, $ancestorElement) {
|
|
|
|
$element = $ancestorElement->find($selector, $locator);
|
|
|
|
|
|
|
|
return $element !== null;
|
|
|
|
};
|
|
|
|
if (!Utils::waitFor($findCallback, $timeout, $timeoutStep)) {
|
2017-04-15 15:53:28 +03:00
|
|
|
$message = $elementLocator->getDescription() . " could not be found";
|
|
|
|
if ($timeout > 0) {
|
|
|
|
$message = $message . " after $timeout seconds";
|
|
|
|
}
|
|
|
|
throw new NoSuchElementException($message);
|
2017-04-04 17:25:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ancestor element from which the given locator will be looked
|
|
|
|
* for.
|
|
|
|
*
|
|
|
|
* If the ancestor of the given locator is another locator the element for
|
|
|
|
* the ancestor locator is found and returned. If the ancestor of the given
|
|
|
|
* locator is already an element that element is the one returned. If the
|
|
|
|
* given locator has no ancestor then the base document element is returned.
|
|
|
|
*
|
|
|
|
* The timeout is used only when finding the element for the ancestor
|
|
|
|
* locator; if the timeout expires a NoSuchElementException is thrown.
|
|
|
|
*
|
2017-05-04 13:35:01 +03:00
|
|
|
* @param \Behat\Mink\Session $session the Mink Session to get the ancestor
|
|
|
|
* element from.
|
2017-04-04 17:25:22 +03:00
|
|
|
* @param Locator $elementLocator the locator for the element to get its
|
|
|
|
* ancestor.
|
|
|
|
* @param float $timeout the number of seconds (decimals allowed) to wait at
|
|
|
|
* most for the ancestor element to appear.
|
|
|
|
* @param float $timeoutStep the number of seconds (decimals allowed) to
|
|
|
|
* wait before trying to find the ancestor element again.
|
|
|
|
* @return \Behat\Mink\Element\Element the ancestor element found.
|
|
|
|
* @throws NoSuchElementException if the ancestor element can not be found.
|
|
|
|
*/
|
2017-05-04 13:35:01 +03:00
|
|
|
private static function findAncestorElement(\Behat\Mink\Session $session, Locator $elementLocator, $timeout, $timeoutStep) {
|
2017-04-04 17:25:22 +03:00
|
|
|
$ancestorElement = $elementLocator->getAncestor();
|
|
|
|
if ($ancestorElement instanceof Locator) {
|
|
|
|
try {
|
2017-05-04 13:35:01 +03:00
|
|
|
$ancestorElement = self::findInternal($session, $ancestorElement, $timeout, $timeoutStep);
|
2017-04-04 17:25:22 +03:00
|
|
|
} catch (NoSuchElementException $exception) {
|
|
|
|
// Little hack to show the stack of ancestor elements that could
|
|
|
|
// not be found, as Behat only shows the message of the last
|
|
|
|
// exception in the chain.
|
|
|
|
$message = $exception->getMessage() . "\n" .
|
|
|
|
$elementLocator->getDescription() . " could not be found";
|
2017-04-15 15:53:28 +03:00
|
|
|
if ($timeout > 0) {
|
|
|
|
$message = $message . " after $timeout seconds";
|
|
|
|
}
|
2017-04-04 17:25:22 +03:00
|
|
|
throw new NoSuchElementException($message, $exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ancestorElement === null) {
|
2017-05-04 13:35:01 +03:00
|
|
|
$ancestorElement = $session->getPage();
|
2017-04-04 17:25:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $ancestorElement;
|
|
|
|
}
|
|
|
|
|
2017-04-23 19:40:58 +03:00
|
|
|
/**
|
|
|
|
* Returns the shared notebook of the Actors.
|
|
|
|
*
|
|
|
|
* @return array the shared notebook of the Actors.
|
|
|
|
*/
|
|
|
|
public function &getSharedNotebook() {
|
|
|
|
return $this->sharedNotebook;
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:18:05 +03:00
|
|
|
}
|