From 1dc320f140ff90a03f4fce1cade8944e4e70e43a Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Mon, 30 Jan 2017 11:37:34 +0800 Subject: [PATCH] MDL-57765 behat: Allow exact selectors to be used In Blocks with boost theme selector to find block with name is partial match it might fail in some cases. It would be nice to use exact match to avoid unexpected failures --- lib/behat/classes/behat_selectors.php | 20 +++++++++++++++++-- lib/behat/classes/exact_named_selector.php | 14 ++++++++++++- .../behat/behat_theme_boost_behat_blocks.php | 20 +++++++++---------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/behat/classes/behat_selectors.php b/lib/behat/classes/behat_selectors.php index d682a06ec5e..04e255ac5fc 100644 --- a/lib/behat/classes/behat_selectors.php +++ b/lib/behat/classes/behat_selectors.php @@ -26,6 +26,8 @@ require_once(__DIR__ . '/exact_named_selector.php'); require_once(__DIR__ . '/partial_named_selector.php'); +use Behat\Mink\Exception\ExpectationException as ExpectationException; + /** * Moodle selectors manager. * @@ -52,8 +54,22 @@ class behat_selectors { $locator = $element; } else { // Named selectors uses arrays as locators including the type of named selector. - $locator = array($selectortype, behat_context_helper::escape($element)); - $selector = 'named_partial'; + $allowedselectors = self::get_allowed_selectors(); + if (!isset($allowedselectors[$selectortype])) { + throw new ExpectationException('The "' . $selectortype . '" selector not registered.', $session); + } + $locator = array($allowedselectors[$selectortype], behat_context_helper::escape($element)); + + // Get the selector which should be used. + $allowedpartialselectors = behat_partial_named_selector::get_allowed_selectors(); + $allowedexactselectors = behat_exact_named_selector::get_allowed_selectors(); + if (isset($allowedpartialselectors[$selectortype])) { + $selector = 'named_partial'; + } else if (isset($allowedexactselectors[$selectortype])) { + $selector = 'named_exact'; + } else { + throw new ExpectationException('The "' . $selectortype . '" selector not registered.', $session); + } } return array($selector, $locator); diff --git a/lib/behat/classes/exact_named_selector.php b/lib/behat/classes/exact_named_selector.php index 61f3b5b739b..9868de5f037 100644 --- a/lib/behat/classes/exact_named_selector.php +++ b/lib/behat/classes/exact_named_selector.php @@ -39,7 +39,19 @@ class behat_exact_named_selector extends \Behat\Mink\Selector\ExactNamedSelector /** * @var Allowed types when using selector arguments. */ - protected static $allowedselectors = []; + protected static $allowedselectors = array( + 'button_exact' => 'button', + 'checkbox_exact' => 'checkbox', + 'field_exact' => 'field', + 'fieldset_exact' => 'fieldset', + 'link_exact' => 'link', + 'link_or_button_exact' => 'link_or_button', + 'option_exact' => 'option', + 'radio_exact' => 'radio', + 'select_exact' => 'select', + 'table_exact' => 'table', + 'text_exact' => 'text', + ); /** * Allowed selectors getter. diff --git a/theme/boost/tests/behat/behat_theme_boost_behat_blocks.php b/theme/boost/tests/behat/behat_theme_boost_behat_blocks.php index 4cd419e1889..8761aa30e0e 100644 --- a/theme/boost/tests/behat/behat_theme_boost_behat_blocks.php +++ b/theme/boost/tests/behat/behat_theme_boost_behat_blocks.php @@ -41,9 +41,9 @@ class behat_theme_boost_behat_blocks extends behat_blocks { $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', get_string('addblock')); if (!$this->running_javascript()) { - $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link', '#region-main', 'css_element']); + $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']); } else { - $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link', '.modal-body', 'css_element']); + $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', '.modal-body', 'css_element']); } } @@ -70,11 +70,11 @@ class behat_theme_boost_behat_blocks extends behat_blocks { $cancelstr = get_string('cancel'); if (!$this->running_javascript()) { - $this->execute('behat_general::should_exist_in_the', [$blockname, 'link', '#region-main', 'css_element']); - $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link', '#region-main', 'css_element']); + $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']); + $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '#region-main', 'css_element']); } else { - $this->execute('behat_general::should_exist_in_the', [$blockname, 'link', '.modal-body', 'css_element']); - $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link', '.modal-body', 'css_element']); + $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', '.modal-body', 'css_element']); + $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '.modal-body', 'css_element']); } } @@ -83,11 +83,11 @@ class behat_theme_boost_behat_blocks extends behat_blocks { $cancelstr = get_string('cancel'); if (!$this->running_javascript()) { - $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link', '#region-main', 'css_element']); - $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link', '#region-main', 'css_element']); + $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']); + $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '#region-main', 'css_element']); } else { - $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link', '.modal-body', 'css_element']); - $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link', '.modal-body', 'css_element']); + $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', '.modal-body', 'css_element']); + $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '.modal-body', 'css_element']); } }