class SelectorsHandler
Selectors handler.
@author Konstantin Kudryashov <ever.zet@gmail.com>
Hierarchy
- class \Behat\Mink\Selector\SelectorsHandler
Expanded class hierarchy of SelectorsHandler
3 files declare their use of SelectorsHandler
- Element.php in vendor/
behat/ mink/ src/ Element/ Element.php - ElementFinder.php in vendor/
behat/ mink/ src/ Element/ ElementFinder.php - Session.php in vendor/
behat/ mink/ src/ Session.php
File
-
vendor/
behat/ mink/ src/ Selector/ SelectorsHandler.php, line 20
Namespace
Behat\Mink\SelectorView source
class SelectorsHandler {
/**
* @var array<string, SelectorInterface>
*/
private $selectors = [];
/**
* @var Escaper
*/
private $escaper;
/**
* Initializes selectors handler.
*
* @param array<string, SelectorInterface> $selectors default selectors to register
*/
public function __construct(array $selectors = array()) {
$this->escaper = new Escaper();
$this->registerSelector('named_partial', new PartialNamedSelector());
$this->registerSelector('named_exact', new ExactNamedSelector());
$this->registerSelector('css', new CssSelector());
foreach ($selectors as $name => $selector) {
$this->registerSelector($name, $selector);
}
}
/**
* Registers new selector engine with specified name.
*
* @param string $name selector engine name
* @param SelectorInterface $selector selector engine instance
*
* @return void
*/
public function registerSelector(string $name, SelectorInterface $selector) {
$this->selectors[$name] = $selector;
}
/**
* Checks whether selector with specified name is registered on handler.
*
* @param string $name selector engine name
*
* @return bool
*/
public function isSelectorRegistered(string $name) {
return isset($this->selectors[$name]);
}
/**
* Returns selector engine with specified name.
*
* @param string $name selector engine name
*
* @return SelectorInterface
*
* @throws \InvalidArgumentException
*/
public function getSelector(string $name) {
if ('named' === $name) {
@trigger_error('Using the "named" selector directly from the handler is deprecated as of 1.6 and will be removed in 2.0.' . ' Use the "named_partial" or use the "named" selector through the Element API instead.', E_USER_DEPRECATED);
$name = 'named_partial';
}
if (!$this->isSelectorRegistered($name)) {
throw new \InvalidArgumentException("Selector \"{$name}\" is not registered.");
}
return $this->selectors[$name];
}
/**
* Translates selector with specified name to XPath.
*
* @param string $selector selector engine name (registered)
* @param string|array $locator selector locator (an array or a string depending on the selector being used)
*
* @return string
*/
public function selectorToXpath(string $selector, $locator) {
if ('xpath' === $selector) {
if (!is_string($locator)) {
throw new \InvalidArgumentException('The xpath selector expects to get a string as locator');
}
return $locator;
}
return $this->getSelector($selector)
->translateToXPath($locator);
}
/**
* Translates string to XPath literal.
*
* @deprecated since Mink 1.7. Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral when building Xpath
* or pass the unescaped value when using the named selector.
*
* @param string $s
*
* @return string
*/
public function xpathLiteral(string $s) {
@trigger_error('The ' . __METHOD__ . ' method is deprecated as of 1.7 and will be removed in 2.0.' . ' Use \\Behat\\Mink\\Selector\\Xpath\\Escaper::escapeLiteral instead when building Xpath' . ' or pass the unescaped value when using the named selector.', E_USER_DEPRECATED);
return $this->escaper
->escapeLiteral($s);
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary |
---|---|---|---|---|
SelectorsHandler::$escaper | private | property | ||
SelectorsHandler::$selectors | private | property | ||
SelectorsHandler::getSelector | public | function | Returns selector engine with specified name. | |
SelectorsHandler::isSelectorRegistered | public | function | Checks whether selector with specified name is registered on handler. | |
SelectorsHandler::registerSelector | public | function | Registers new selector engine with specified name. | |
SelectorsHandler::selectorToXpath | public | function | Translates selector with specified name to XPath. | |
SelectorsHandler::xpathLiteral | Deprecated | public | function | Translates string to XPath literal. |
SelectorsHandler::__construct | public | function | Initializes selectors handler. |