From 98f712c2e328482312355b8a5204d50c2f73851c Mon Sep 17 00:00:00 2001 From: Brian Osborne Date: Thu, 1 Feb 2024 11:33:24 -0500 Subject: [PATCH] Add support for finding buttons that use aria-label for a value. --- src/Selector/ExactNamedSelector.php | 1 + src/Selector/NamedSelector.php | 8 ++++---- src/Selector/PartialNamedSelector.php | 1 + tests/Selector/NamedSelectorTest.php | 6 +++--- tests/Selector/fixtures/test.html | 19 +++++++++++++++++++ 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Selector/ExactNamedSelector.php b/src/Selector/ExactNamedSelector.php index 03315a9cf..aac319f78 100644 --- a/src/Selector/ExactNamedSelector.php +++ b/src/Selector/ExactNamedSelector.php @@ -23,6 +23,7 @@ public function __construct() $this->registerReplacement('%altMatch%', './@alt = %locator%'); $this->registerReplacement('%relMatch%', './@rel = %locator%'); $this->registerReplacement('%labelAttributeMatch%', './@label = %locator%'); + $this->registerReplacement('%ariaLabelAttributeMatch%', './@aria-label = %locator%'); parent::__construct(); } diff --git a/src/Selector/NamedSelector.php b/src/Selector/NamedSelector.php index b5d3e722e..5c4a19225 100644 --- a/src/Selector/NamedSelector.php +++ b/src/Selector/NamedSelector.php @@ -46,7 +46,7 @@ class NamedSelector implements SelectorInterface '%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%', + '%buttonMatch%' => '%idOrNameMatch% or %valueMatch% or %titleMatch% or %ariaLabelAttributeMatch%', '%linkMatch%' => '(%idMatch% or %tagTextMatch% or %titleMatch% or %relMatch%)', '%imgAltMatch%' => './/img[%altMatch%]', ); @@ -99,16 +99,16 @@ class NamedSelector implements SelectorInterface [./@href][(%linkMatch% or %imgAltMatch%)] | .//input -[%buttonTypeFilter%][(%idOrValueMatch% or %titleMatch%)] +[%buttonTypeFilter%][(%idOrValueMatch% or %titleMatch% or %ariaLabelAttributeMatch%)] | .//input [%lowercaseType% = 'image'][%altMatch%] | .//button -[(%idOrValueMatch% or %titleMatch% or %tagTextMatch%)] +[(%idOrValueMatch% or %titleMatch% or %tagTextMatch% or %ariaLabelAttributeMatch%)] | .//* -[(%lowercaseRole% = 'button' or %lowercaseRole% = 'link')][(%idOrValueMatch% or %titleMatch% or %tagTextMatch%)] +[(%lowercaseRole% = 'button' or %lowercaseRole% = 'link')][(%idOrValueMatch% or %titleMatch% or %tagTextMatch% or %ariaLabelAttributeMatch%)] XPATH ,'content' => <<registerReplacement('%altMatch%', 'contains(./@alt, %locator%)'); $this->registerReplacement('%relMatch%', 'contains(./@rel, %locator%)'); $this->registerReplacement('%labelAttributeMatch%', 'contains(./@label, %locator%)'); + $this->registerReplacement('%ariaLabelAttributeMatch%', 'contains(./@aria-label, %locator%)'); parent::__construct(); } diff --git a/tests/Selector/NamedSelectorTest.php b/tests/Selector/NamedSelectorTest.php index 2ef382978..0c9d09e07 100644 --- a/tests/Selector/NamedSelectorTest.php +++ b/tests/Selector/NamedSelectorTest.php @@ -130,7 +130,7 @@ public function getSelectorTests() 'link (without-href) ignored' => array('test.html', 'link', 'bad-link-text', 0), 'link* (role=link)' => array('test.html', 'link', 'link-role-text', 4, 7), - 'button (input, name/value/title)' => array('test.html', 'button', 'button-text', 25, 42), + 'button (input, name/value/title/aria-label)' => array('test.html', 'button', 'button-text', 33, 58), 'button (type=image, with-alt)' => array('test.html', 'button', 'button-alt-text', 1, 2), 'button (input type=submit, with-id)' => array('test.html', 'button', 'input-submit-button', 1), 'button (input type=image, with-id)' => array('test.html', 'button', 'input-image-button', 1), @@ -150,8 +150,8 @@ public function getSelectorTests() 'link_or_button (without-href) ignored' => array('test.html', 'link_or_button', 'bad-link-text', 0), 'link_or_button* (role=link)' => array('test.html', 'link_or_button', 'link-role-text', 4, 7), - // bug in selector: 17 instead of 25 and 34 instead of 42, because 8 buttons with `name` attribute were not matched - 'link_or_button (input, name/value/title)' => array('test.html', 'link_or_button', 'button-text', 17, 34), + // bug in selector: 25 instead of 33 and 50 instead of 58, because 8 buttons with `name` attribute were not matched + 'link_or_button (input, name/value/title/aria-label)' => array('test.html', 'link_or_button', 'button-text', 25, 50), 'link_or_button (type=image, with-alt)' => array('test.html', 'link_or_button', 'button-alt-text', 1, 2), 'link_or_button (input type=submit, with-id)' => array('test.html', 'link_or_button', 'input-submit-button', 1), 'link_or_button (input type=image, with-id)' => array('test.html', 'link_or_button', 'input-image-button', 1), diff --git a/tests/Selector/fixtures/test.html b/tests/Selector/fixtures/test.html index 12bcf8c9c..29f32319a 100644 --- a/tests/Selector/fixtures/test.html +++ b/tests/Selector/fixtures/test.html @@ -191,6 +191,25 @@ + + + + + + + + + + + + + + + + + + +