Add general multiplier for find timeouts

Although the timeouts specified in the acceptance tests are enough in
most cases they may not be when running them in a slow system or
environment. For those situations a general multiplier for find
timeouts is added. It can be set in the "behat.yml" configuration file
to increase the timeout used in every find call (except those that used
a timeout of 0, as in those cases the element had to be already present
when finding it and whether the system is slow or not does not change
that).

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
This commit is contained in:
Daniel Calviño Sánchez 2017-04-15 18:56:24 +02:00
parent a7e1833cf3
commit be96be09b5
2 changed files with 48 additions and 1 deletions

View File

@ -41,6 +41,13 @@
* 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.
*
* 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.
*/
class Actor {
@ -54,6 +61,11 @@ class Actor {
*/
private $baseUrl;
/**
* @var float
*/
private $findTimeoutMultiplier;
/**
* Creates a new Actor.
*
@ -64,6 +76,7 @@ class Actor {
public function __construct(\Behat\Mink\Session $session, $baseUrl) {
$this->session = $session;
$this->baseUrl = $baseUrl;
$this->findTimeoutMultiplier = 1;
}
/**
@ -75,6 +88,16 @@ class Actor {
$this->baseUrl = $baseUrl;
}
/**
* Sets the multiplier for find timeouts.
*
* @param float $findTimeoutMultiplier the multiplier to apply to find
* timeouts.
*/
public function setFindTimeoutMultiplier($findTimeoutMultiplier) {
$this->findTimeoutMultiplier = $findTimeoutMultiplier;
}
/**
* Returns the Mink Session used to control its web browser.
*
@ -113,7 +136,8 @@ class Actor {
* 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
* before retrying is half a second.
* before retrying is half a second. If the timeout is not 0 it will be
* affected by the multiplier set using setFindTimeoutMultiplier(), if any.
*
* In any case, if the element, or its ancestors, can not be found a
* NoSuchElementException is thrown.
@ -128,6 +152,8 @@ class Actor {
* be found.
*/
public function find($elementLocator, $timeout = 0, $timeoutStep = 0.5) {
$timeout = $timeout * $this->findTimeoutMultiplier;
$element = null;
$selector = $elementLocator->getSelector();
$locator = $elementLocator->getLocator();

View File

@ -38,6 +38,10 @@ use Behat\MinkExtension\Context\RawMinkContext;
* Besides updating the current actor in sibling contexts the ActorContext also
* propagates its inherited "base_url" Mink parameter to the Actors as needed.
*
* By default no multiplier for the find timeout is set in the Actors. However,
* it can be customized using the "actorFindTimeoutMultiplier" parameter of the
* ActorContext in "behat.yml".
*
* Every actor used in the scenarios must have a corresponding Mink session
* declared in "behat.yml" with the same name as the actor. All used sessions
* are stopped after each scenario is run.
@ -54,6 +58,21 @@ class ActorContext extends RawMinkContext {
*/
private $currentActor;
/**
* @var float
*/
private $actorFindTimeoutMultiplier;
/**
* Creates a new ActorContext.
*
* @param float $actorFindTimeoutMultiplier the find timeout multiplier to
* set in the Actors.
*/
public function __construct($actorFindTimeoutMultiplier = 1) {
$this->actorFindTimeoutMultiplier = $actorFindTimeoutMultiplier;
}
/**
* Sets a Mink parameter.
*
@ -85,6 +104,7 @@ class ActorContext extends RawMinkContext {
$this->actors = array();
$this->actors["default"] = new Actor($this->getSession(), $this->getMinkParameter("base_url"));
$this->actors["default"]->setFindTimeoutMultiplier($this->actorFindTimeoutMultiplier);
$this->currentActor = $this->actors["default"];
}
@ -108,6 +128,7 @@ class ActorContext extends RawMinkContext {
public function iActAs($actorName) {
if (!array_key_exists($actorName, $this->actors)) {
$this->actors[$actorName] = new Actor($this->getSession($actorName), $this->getMinkParameter("base_url"));
$this->actors[$actorName]->setFindTimeoutMultiplier($this->actorFindTimeoutMultiplier);
}
$this->currentActor = $this->actors[$actorName];