mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 12:32:08 +02:00
MDL-75153 form: Behat support for group structures in select menu field
Adds behat support for group structures in the select menu field. When looking for a particular option you can further specify the group under which this option should be located by using the '>' delimiter (e.g. "Group name > Option name"). In addition to that, this commit intorduces new step definition which can be used to check whether the select menu field contains a particular option.
This commit is contained in:
parent
3aa5e2688a
commit
460744a6a6
@ -18,6 +18,8 @@ declare(strict_types=1);
|
||||
|
||||
require_once(__DIR__ . '/behat_form_field.php');
|
||||
|
||||
use \Behat\Mink\Element\NodeElement;
|
||||
|
||||
/**
|
||||
* Custom interaction with select_menu elements
|
||||
*
|
||||
@ -26,18 +28,23 @@ require_once(__DIR__ . '/behat_form_field.php');
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class behat_form_select_menu extends behat_form_field {
|
||||
|
||||
/**
|
||||
* Sets the value of the select menu field.
|
||||
*
|
||||
* @param string $value The string that is used to identify an option within the select menu. If the string
|
||||
* has two items separated by '>' (ex. "Group > Option"), the first item ("Group") will be
|
||||
* used to identify a particular group within the select menu, while the second ("Option")
|
||||
* will be used to identify an option within that group. Otherwise, a string with a single
|
||||
* item (ex. "Option") will be used to identify an option within the select menu regardless
|
||||
* of any existing groups.
|
||||
*/
|
||||
public function set_value($value) {
|
||||
self::require_javascript();
|
||||
|
||||
$rootnode = $this->field->getParent();
|
||||
$options = $rootnode->findAll('css', '[role=option]');
|
||||
$this->field->click();
|
||||
foreach ($options as $option) {
|
||||
if (trim($option->getHtml()) == $value) {
|
||||
$option->click();
|
||||
break;
|
||||
}
|
||||
}
|
||||
$option = $this->find_option($value);
|
||||
$option->click();
|
||||
}
|
||||
|
||||
public function get_value() {
|
||||
@ -45,4 +52,50 @@ class behat_form_select_menu extends behat_form_field {
|
||||
$input = $rootnode->find('css', 'input');
|
||||
return $input->getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a given option exists in the select menu field.
|
||||
*
|
||||
* @param string $option The string that is used to identify an option within the select menu. If the string
|
||||
* has two items separated by '>' (ex. "Group > Option"), the first item ("Group") will be
|
||||
* used to identify a particular group within the select menu, while the second ("Option")
|
||||
* will be used to identify an option within that group. Otherwise, a string with a single
|
||||
* item (ex. "Option") will be used to identify an option within the select menu regardless
|
||||
* of any existing groups.
|
||||
* @return bool Whether the option exists in the select menu field or not.
|
||||
*/
|
||||
public function has_option(string $option): bool {
|
||||
if ($this->find_option($option)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and returns a given option from the select menu field.
|
||||
*
|
||||
* @param string $option The string that is used to identify an option within the select menu. If the string
|
||||
* has two items separated by '>' (ex. "Group > Option"), the first item ("Group") will be
|
||||
* used to identify a particular group within the select menu, while the second ("Option")
|
||||
* will be used to identify an option within that group. Otherwise, a string with a single
|
||||
* item (ex. "Option") will be used to identify an option within the select menu regardless
|
||||
* of any existing groups.
|
||||
* @return NodeElement|null The option element or null if it cannot be found.
|
||||
*/
|
||||
private function find_option(string $option): ?NodeElement {
|
||||
// Split the value string by ">" to determine whether a group has been specified.
|
||||
$path = preg_split('/\s*>\s*/', trim($option));
|
||||
|
||||
if (count($path) > 1) { // Group has been specified.
|
||||
$optionxpath = '//li[contains(@role, "presentation") and normalize-space(text()) = "' .
|
||||
$this->escape($path[0]) . '"]' .
|
||||
'/following-sibling::li[contains(@role, "option") and normalize-space(text()) = "' .
|
||||
$this->escape($path[1]) . '"]';
|
||||
} else { // Group has not been specified.
|
||||
$optionxpath = '//li[contains(@role, "option") and normalize-space(text()) = "' .
|
||||
$this->escape($path[0]) . '"]';
|
||||
}
|
||||
|
||||
return $this->field->getParent()->find('xpath', $optionxpath);
|
||||
}
|
||||
}
|
||||
|
@ -755,4 +755,45 @@ class behat_forms extends behat_base {
|
||||
$node = $this->get_node_in_container('xpath_element', $xpathtarget, 'form_row', $field);
|
||||
$this->ensure_node_is_visible($node);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the select menu contains an option with specified text or not.
|
||||
*
|
||||
* @Then the :name select menu should contain :option
|
||||
* @Then the :name select menu should :not contain :option
|
||||
*
|
||||
* @throws ExpectationException When the expectation is not satisfied
|
||||
* @param string $label The label of the select menu element
|
||||
* @param string $option The string that is used to identify an option within the select menu. If the string
|
||||
* has two items separated by '>' (ex. "Group > Option"), the first item ("Group") will be
|
||||
* used to identify a particular group within the select menu, while the second ("Option")
|
||||
* will be used to identify an option within that group. Otherwise, a string with a single
|
||||
* item (ex. "Option") will be used to identify an option within the select menu regardless
|
||||
* of any existing groups.
|
||||
* @param string|null $not If set, the select menu should not contain the specified option. If null, the option
|
||||
* should be present.
|
||||
*/
|
||||
public function the_select_menu_should_contain(string $label, string $option, ?string $not = null) {
|
||||
|
||||
$field = behat_field_manager::get_form_field_from_label($label, $this);
|
||||
|
||||
if (!method_exists($field, 'has_option')) {
|
||||
throw new coding_exception('Field does not support the has_option function.');
|
||||
}
|
||||
|
||||
// If the select menu contains the specified option but it should not.
|
||||
if ($field->has_option($option) && $not) {
|
||||
throw new ExpectationException(
|
||||
"The select menu should not contain \"{$option}\" but it does.",
|
||||
$this->getSession()
|
||||
);
|
||||
}
|
||||
// If the select menu does not contain the specified option but it should.
|
||||
if (!$field->has_option($option) && !$not) {
|
||||
throw new ExpectationException(
|
||||
"The select menu should contain \"{$option}\" but it does not.",
|
||||
$this->getSession()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user