Add wrappers to adapt the element finding system of Mink

Mink elements (including the document element) provide a
"find(selector, locator)" method to look for child elements in their web
browser session. The Locator class is added to be able to store the
selector and locator in a single object; it also provides a fluent API
to ease the definition of Mink locators, specially those using the
"named" selector.

The method "find(locator, timeout, timeoutStep)" is added to Actor
objects; it is simply a wrapper over Mink's "find(selector, locator)"
method, although it throws an exception if the element can not be found
instead of returning null, and it also makes possible to automatically
retry to find the element for certain amount of time.

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
This commit is contained in:
Daniel Calviño Sánchez 2017-04-04 16:25:22 +02:00
parent 7c07f01d59
commit b22997796b
3 changed files with 465 additions and 0 deletions

View File

@ -35,6 +35,12 @@
* most cases the tests are specified using relative paths that can be converted
* to the appropriate absolute URL using locatePath() in the step
* implementation.
*
* 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.
*/
class Actor {
@ -89,4 +95,97 @@ class Actor {
return $this->baseUrl . $relativePath;
}
/**
* 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
* before retrying is half a second.
*
* 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.
*/
public function find($elementLocator, $timeout = 0, $timeoutStep = 0.5) {
$element = null;
$selector = $elementLocator->getSelector();
$locator = $elementLocator->getLocator();
$ancestorElement = $this->findAncestorElement($elementLocator, $timeout, $timeoutStep);
$findCallback = function() use (&$element, $selector, $locator, $ancestorElement) {
$element = $ancestorElement->find($selector, $locator);
return $element !== null;
};
if (!Utils::waitFor($findCallback, $timeout, $timeoutStep)) {
throw new NoSuchElementException($elementLocator->getDescription() . " could not be found");
}
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.
*
* @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.
*/
private function findAncestorElement($elementLocator, $timeout, $timeoutStep) {
$ancestorElement = $elementLocator->getAncestor();
if ($ancestorElement instanceof Locator) {
try {
$ancestorElement = $this->find($ancestorElement, $timeout, $timeoutStep);
} 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";
throw new NoSuchElementException($message, $exception);
}
}
if ($ancestorElement === null) {
$ancestorElement = $this->getSession()->getPage();
}
return $ancestorElement;
}
}

View File

@ -0,0 +1,329 @@
<?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/>.
*
*/
/**
* Data object for the information needed to locate an element in a web page
* using Mink.
*
* Locators can be created directly using the constructor, or through a more
* fluent interface with Locator::forThe().
*/
class Locator {
/**
* @var string
*/
private $description;
/**
* @var string
*/
private $selector;
/**
* @var string|array
*/
private $locator;
/**
* @var null|Locator|\Behat\Mink\Element\ElementInterface
*/
private $ancestor;
/**
* Starting point for the fluent interface to create Locators.
*
* @return LocatorBuilder
*/
public static function forThe() {
return new LocatorBuilder();
}
/**
* @param string $description
* @param string $selector
* @param string|array $locator
* @param null|Locator|\Behat\Mink\Element\ElementInterface $ancestor
*/
public function __construct($description, $selector, $locator, $ancestor = null) {
$this->description = $description;
$this->selector = $selector;
$this->locator = $locator;
$this->ancestor = $ancestor;
}
/**
* @return string
*/
public function getDescription() {
return $this->description;
}
/**
* @return string
*/
public function getSelector() {
return $this->selector;
}
/**
* @return string|array
*/
public function getLocator() {
return $this->locator;
}
/**
* @return null|Locator|\Behat\Mink\Element\ElementInterface
*/
public function getAncestor() {
return $this->ancestor;
}
}
class LocatorBuilder {
/**
* @param string $selector
* @param string|array $locator
* @return LocatorBuilderSecondStep
*/
public function customSelector($selector, $locator) {
return new LocatorBuilderSecondStep($selector, $locator);
}
/**
* @param string $cssExpression
* @return LocatorBuilderSecondStep
*/
public function css($cssExpression) {
return $this->customSelector("css", $cssExpression);
}
/**
* @param string $xpathExpression
* @return LocatorBuilderSecondStep
*/
public function xpath($xpathExpression) {
return $this->customSelector("xpath", $xpathExpression);
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function id($value) {
return $this->customSelector("named", array("id", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function idOrName($value) {
return $this->customSelector("named", array("id_or_name", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function link($value) {
return $this->customSelector("named", array("link", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function button($value) {
return $this->customSelector("named", array("button", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function linkOrButton($value) {
return $this->customSelector("named", array("link_or_button", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function content($value) {
return $this->customSelector("named", array("content", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function field($value) {
return $this->customSelector("named", array("field", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function selectField($value) {
return $this->customSelector("named", array("select", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function checkbox($value) {
return $this->customSelector("named", array("checkbox", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function radioButton($value) {
return $this->customSelector("named", array("radio", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function fileInput($value) {
return $this->customSelector("named", array("file", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function optionGroup($value) {
return $this->customSelector("named", array("optgroup", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function option($value) {
return $this->customSelector("named", array("option", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function fieldSet($value) {
return $this->customSelector("named", array("fieldset", $value));
}
/**
* @param string $value
* @return LocatorBuilderSecondStep
*/
public function table($value) {
return $this->customSelector("named", array("table", $value));
}
}
class LocatorBuilderSecondStep {
/**
* @var string
*/
private $selector;
/**
* @var string|array
*/
private $locator;
/**
* @param string $selector
* @param string|array $locator
*/
public function __construct($selector, $locator) {
$this->selector = $selector;
$this->locator = $locator;
}
/**
* @param Locator|\Behat\Mink\Element\ElementInterface $ancestor
* @return LocatorBuilderThirdStep
*/
public function descendantOf($ancestor) {
return new LocatorBuilderThirdStep($this->selector, $this->locator, $ancestor);
}
/**
* @param string $description
* @return Locator
*/
public function describedAs($description) {
return new Locator($description, $this->selector, $this->locator);
}
}
class LocatorBuilderThirdStep {
/**
* @var string
*/
private $selector;
/**
* @var string|array
*/
private $locator;
/**
* @var Locator|\Behat\Mink\Element\ElementInterface
*/
private $ancestor;
/**
* @param string $selector
* @param string|array $locator
* @param Locator|\Behat\Mink\Element\ElementInterface $ancestor
*/
public function __construct($selector, $locator, $ancestor) {
$this->selector = $selector;
$this->locator = $locator;
$this->ancestor = $ancestor;
}
/**
* @param string $description
* @return Locator
*/
public function describedAs($description) {
return new Locator($description, $this->selector, $this->locator, $this->ancestor);
}
}

View File

@ -0,0 +1,37 @@
<?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/>.
*
*/
/**
* Exception to signal that the element looked for could not be found.
*/
class NoSuchElementException extends \Exception {
/**
* @param string $message
* @param null|\Exception $previous
*/
public function __construct($message, \Exception $previous = null) {
parent::__construct($message, 0, $previous);
}
}