Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move declaration of replacements and selectors to new function #815

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
[Unreleased]
==================

* Move selector and replacement declaration to dedicated functions.
Note: If you were previously extending the NamedSelector class and defining overrides to existing values, these will
need to be moved to the relevant `getRawSelectors()` or `getRawReplacements()` function as appropriate.
See #815 for further information on this change.

1.9.0 / 2021-10-11
==================

Expand Down
18 changes: 9 additions & 9 deletions src/Selector/ExactNamedSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
class ExactNamedSelector extends NamedSelector
{
public function __construct()
protected function getRawReplacements()
{
$this->registerReplacement('%tagTextMatch%', 'normalize-space(string(.)) = %locator%');
$this->registerReplacement('%valueMatch%', './@value = %locator%');
$this->registerReplacement('%titleMatch%', './@title = %locator%');
$this->registerReplacement('%altMatch%', './@alt = %locator%');
$this->registerReplacement('%relMatch%', './@rel = %locator%');
$this->registerReplacement('%labelAttributeMatch%', './@label = %locator%');

parent::__construct();
return array_merge(parent::getRawReplacements(), [
'%tagTextMatch%' => 'normalize-space(string(.)) = %locator%',
'%valueMatch%' => './@value = %locator%',
'%titleMatch%' => './@title = %locator%',
'%altMatch%' => './@alt = %locator%',
'%relMatch%' => './@rel = %locator%',
'%labelAttributeMatch%' => './@label = %locator%',
]);
}
}
146 changes: 84 additions & 62 deletions src/Selector/NamedSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,80 @@
*/
class NamedSelector implements SelectorInterface
{
private $replacements = array(
// simple replacements
'%lowercaseType%' => "translate(./@type, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')",
'%lowercaseRole%' => "translate(./@role, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')",
'%tagTextMatch%' => 'contains(normalize-space(string(.)), %locator%)',
'%labelTextMatch%' => './@id = //label[%tagTextMatch%]/@for',
'%idMatch%' => './@id = %locator%',
'%valueMatch%' => 'contains(./@value, %locator%)',
'%idOrValueMatch%' => '(%idMatch% or %valueMatch%)',
'%idOrNameMatch%' => '(%idMatch% or ./@name = %locator%)',
'%placeholderMatch%' => './@placeholder = %locator%',
'%titleMatch%' => 'contains(./@title, %locator%)',
'%altMatch%' => 'contains(./@alt, %locator%)',
'%relMatch%' => 'contains(./@rel, %locator%)',
'%labelAttributeMatch%' => 'contains(./@label, %locator%)',

// complex replacements
'%inputTypeWithoutPlaceholderFilter%' => "%lowercaseType% = 'radio' or %lowercaseType% = 'checkbox' or %lowercaseType% = 'file'",
'%fieldFilterWithPlaceholder%' => 'self::input[not(%inputTypeWithoutPlaceholderFilter%)] | self::textarea',
'%fieldMatchWithPlaceholder%' => '(%idOrNameMatch% or %labelTextMatch% or %placeholderMatch%)',
'%fieldMatchWithoutPlaceholder%' => '(%idOrNameMatch% or %labelTextMatch%)',
'%fieldFilterWithoutPlaceholder%' => 'self::input[%inputTypeWithoutPlaceholderFilter%] | self::select',
'%buttonTypeFilter%' => "%lowercaseType% = 'submit' or %lowercaseType% = 'image' or %lowercaseType% = 'button' or %lowercaseType% = 'reset'",
'%notFieldTypeFilter%' => "not(%buttonTypeFilter% or %lowercaseType% = 'hidden')",
'%buttonMatch%' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
'%linkMatch%' => '(%idMatch% or %tagTextMatch% or %titleMatch% or %relMatch%)',
'%imgAltMatch%' => './/img[%altMatch%]',
);

private $selectors = array(
'fieldset' => <<<XPATH
private $replacements = [];

private $selectors = [];

private $xpathEscaper;

/**
* Creates selector instance.
*/
public function __construct()
{
$this->xpathEscaper = new Escaper();

foreach ($this->getRawReplacements() as $from => $to) {
$this->registerReplacement($from, $to);
}

foreach ($this->getRawSelectors() as $alias => $selector) {
$this->registerNamedXpath($alias, $selector);
}
}

/**
* Get the list of replacements in their raw state with replacements not applied.
*
* @return array
*/
protected function getRawReplacements()
{
return array(
// simple replacements
'%lowercaseType%' => "translate(./@type, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')",
'%lowercaseRole%' => "translate(./@role, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')",
'%tagTextMatch%' => 'contains(normalize-space(string(.)), %locator%)',
'%labelTextMatch%' => './@id = //label[%tagTextMatch%]/@for',
'%idMatch%' => './@id = %locator%',
'%valueMatch%' => 'contains(./@value, %locator%)',
'%idOrValueMatch%' => '(%idMatch% or %valueMatch%)',
'%idOrNameMatch%' => '(%idMatch% or ./@name = %locator%)',
'%placeholderMatch%' => './@placeholder = %locator%',
'%titleMatch%' => 'contains(./@title, %locator%)',
'%altMatch%' => 'contains(./@alt, %locator%)',
'%relMatch%' => 'contains(./@rel, %locator%)',
'%labelAttributeMatch%' => 'contains(./@label, %locator%)',

// complex replacements
'%inputTypeWithoutPlaceholderFilter%' => "%lowercaseType% = 'radio' or %lowercaseType% = 'checkbox' or %lowercaseType% = 'file'",
'%fieldFilterWithPlaceholder%' => 'self::input[not(%inputTypeWithoutPlaceholderFilter%)] | self::textarea',
'%fieldMatchWithPlaceholder%' => '(%idOrNameMatch% or %labelTextMatch% or %placeholderMatch%)',
'%fieldMatchWithoutPlaceholder%' => '(%idOrNameMatch% or %labelTextMatch%)',
'%fieldFilterWithoutPlaceholder%' => 'self::input[%inputTypeWithoutPlaceholderFilter%] | self::select',
'%buttonTypeFilter%' => "%lowercaseType% = 'submit' or %lowercaseType% = 'image' or %lowercaseType% = 'button' or %lowercaseType% = 'reset'",
'%notFieldTypeFilter%' => "not(%buttonTypeFilter% or %lowercaseType% = 'hidden')",
'%buttonMatch%' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
'%linkMatch%' => '(%idMatch% or %tagTextMatch% or %titleMatch% or %relMatch%)',
'%imgAltMatch%' => './/img[%altMatch%]',
);
}

/**
* Get the list of selectors in their raw state with replacements not applied.
*
* @return array
*/
protected function getRawSelectors()
{

return array(
'fieldset' => <<<XPATH
.//fieldset
[(%idMatch% or .//legend[%tagTextMatch%])]
XPATH

,'field' => <<<XPATH
,'field' => <<<XPATH
.//*
[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
|
Expand All @@ -66,15 +104,15 @@ class NamedSelector implements SelectorInterface
.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
XPATH

,'link' => <<<XPATH
,'link' => <<<XPATH
.//a
[./@href][(%linkMatch% or %imgAltMatch%)]
|
.//*
[%lowercaseRole% = 'link'][(%idOrValueMatch% or %titleMatch% or %tagTextMatch%)]
XPATH

,'button' => <<<XPATH
,'button' => <<<XPATH
.//input
[%buttonTypeFilter%][(%buttonMatch%)]
|
Expand All @@ -88,7 +126,7 @@ class NamedSelector implements SelectorInterface
[%lowercaseRole% = 'button'][(%buttonMatch% or %tagTextMatch%)]
XPATH

,'link_or_button' => <<<XPATH
,'link_or_button' => <<<XPATH
.//a
[./@href][(%linkMatch% or %imgAltMatch%)]
|
Expand All @@ -105,76 +143,60 @@ class NamedSelector implements SelectorInterface
[(%lowercaseRole% = 'button' or %lowercaseRole% = 'link')][(%idOrValueMatch% or %titleMatch% or %tagTextMatch%)]
XPATH

,'content' => <<<XPATH
,'content' => <<<XPATH
./descendant-or-self::*
[%tagTextMatch%]
XPATH

,'select' => <<<XPATH
,'select' => <<<XPATH
.//select
[%fieldMatchWithoutPlaceholder%]
|
.//label[%tagTextMatch%]//.//select
XPATH

,'checkbox' => <<<XPATH
,'checkbox' => <<<XPATH
.//input
[%lowercaseType% = 'checkbox'][%fieldMatchWithoutPlaceholder%]
|
.//label[%tagTextMatch%]//.//input[%lowercaseType% = 'checkbox']
XPATH

,'radio' => <<<XPATH
,'radio' => <<<XPATH
.//input
[%lowercaseType% = 'radio'][%fieldMatchWithoutPlaceholder%]
|
.//label[%tagTextMatch%]//.//input[%lowercaseType% = 'radio']
XPATH

,'file' => <<<XPATH
,'file' => <<<XPATH
.//input
[%lowercaseType% = 'file'][%fieldMatchWithoutPlaceholder%]
|
.//label[%tagTextMatch%]//.//input[%lowercaseType% = 'file']
XPATH

,'optgroup' => <<<XPATH
,'optgroup' => <<<XPATH
.//optgroup
[%labelAttributeMatch%]
XPATH

,'option' => <<<XPATH
,'option' => <<<XPATH
.//option
[(./@value = %locator% or %tagTextMatch%)]
XPATH

,'table' => <<<XPATH
,'table' => <<<XPATH
.//table
[(%idMatch% or .//caption[%tagTextMatch%])]
XPATH
,'id' => <<<XPATH
,'id' => <<<XPATH
.//*[%idMatch%]
XPATH
,'id_or_name' => <<<XPATH
,'id_or_name' => <<<XPATH
.//*[%idOrNameMatch%]
XPATH
);
private $xpathEscaper;

/**
* Creates selector instance.
*/
public function __construct()
{
$this->xpathEscaper = new Escaper();

foreach ($this->replacements as $from => $to) {
$this->registerReplacement($from, $to);
}

foreach ($this->selectors as $alias => $selector) {
$this->registerNamedXpath($alias, $selector);
}
);
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/Selector/PartialNamedSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
*/
class PartialNamedSelector extends NamedSelector
{
public function __construct()
protected function getRawReplacements()
{
$this->registerReplacement('%tagTextMatch%', 'contains(normalize-space(string(.)), %locator%)');
$this->registerReplacement('%valueMatch%', 'contains(./@value, %locator%)');
$this->registerReplacement('%titleMatch%', 'contains(./@title, %locator%)');
$this->registerReplacement('%altMatch%', 'contains(./@alt, %locator%)');
$this->registerReplacement('%relMatch%', 'contains(./@rel, %locator%)');
$this->registerReplacement('%labelAttributeMatch%', 'contains(./@label, %locator%)');

parent::__construct();
return array_merge(parent::getRawReplacements(), [
'%tagTextMatch%' => 'contains(normalize-space(string(.)), %locator%)',
'%valueMatch%' => 'contains(./@value, %locator%)',
'%titleMatch%' => 'contains(./@title, %locator%)',
'%altMatch%' => 'contains(./@alt, %locator%)',
'%relMatch%' => 'contains(./@rel, %locator%)',
'%labelAttributeMatch%' => 'contains(./@label, %locator%)',
]);
}
}