Skip to content

Commit

Permalink
MDL-57765 behat: Allow exact selectors to be used
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Rajesh Taneja committed Jan 30, 2017
1 parent 5130953 commit 1dc320f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
20 changes: 18 additions & 2 deletions lib/behat/classes/behat_selectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -52,8 +54,22 @@ public static function get_behat_selector($selectortype, $element, Behat\Mink\Se
$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);
Expand Down
14 changes: 13 additions & 1 deletion lib/behat/classes/exact_named_selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions theme/boost/tests/behat/behat_theme_boost_behat_blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public function i_add_the_block($blockname) {
$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']);
}
}

Expand All @@ -70,11 +70,11 @@ public function the_add_block_selector_should_contain_block($blockname) {

$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']);
}
}

Expand All @@ -83,11 +83,11 @@ public function the_add_block_selector_should_not_contain_block($blockname) {

$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']);
}
}

Expand Down

0 comments on commit 1dc320f

Please sign in to comment.