2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Auto complete form field class.
22 * @copyright 2015 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/behat_form_text.php');
31 * Auto complete form field.
35 * @copyright 2015 Damyon Wiese
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class behat_form_autocomplete extends behat_form_text {
41 * Sets the value to a field.
43 * @param string $value
46 public function set_value($value) {
47 if (!$this->running_javascript()) {
48 throw new coding_exception('Setting the value of an autocomplete field requires javascript.');
51 // Clear all current selections.
52 $rootnode = $this->field->getParent()->getParent();
53 $selections = $rootnode->findAll('css', '.form-autocomplete-selection [role=option]');
54 foreach ($selections as $selection) {
56 $this->wait_for_pending_js();
59 $allowscreation = $this->field->hasAttribute('data-tags') && !empty($this->field->getAttribute('data-tags'));
60 $hasmultiple = $this->field->hasAttribute('data-multiple') && !empty($this->field->getAttribute('data-multiple'));
62 if ($hasmultiple && false !== strpos($value, ',')) {
63 // Commas have a special meaning as a value separator in 'multiple' autocomplete elements.
64 // To handle this we break the value up by comma, and enter it in chunks.
65 $values = explode(',', $value);
67 while ($value = array_shift($values)) {
68 $this->add_value(trim($value), $allowscreation);
71 $this->add_value(trim($value), $allowscreation);
76 * Add a value to the autocomplete.
78 * @param string $value
79 * @param bool $allowscreation
81 protected function add_value(string $value, bool $allowscreation): void {
82 $value = trim($value);
84 // Click into the field.
85 $this->field->click();
87 // Remove any existing text.
89 behat_base::type_keys($this->session, [behat_keys::BACKSPACE, behat_keys::DELETE]);
90 } while (strlen($this->field->getValue()) > 0);
91 $this->wait_for_pending_js();
93 // Type in the new value.
94 behat_base::type_keys($this->session, str_split($value));
95 $this->wait_for_pending_js();
97 // If the autocomplete found suggestions, then it will have:
98 // 1) marked itself as expanded; and
99 // 2) have an aria-selected suggestion in the list.
100 $expanded = $this->field->getAttribute('aria-expanded');
101 $suggestion = $this->field->getParent()->getParent()->find('css', '.form-autocomplete-suggestions > [aria-selected="true"]');
103 if ($expanded && null !== $suggestion) {
104 // A suggestion was found.
105 // Click on the first item in the list.
106 $suggestion->click();
107 } else if ($allowscreation) {
108 // Press the return key to create a new entry.
109 behat_base::type_keys($this->session, [behat_keys::ENTER]);
111 throw new \InvalidArgumentException(
112 "Unable to find '{$value}' in the list of options, and unable to create a new option"
116 $this->wait_for_pending_js();
118 // Press the escape to close the autocomplete suggestions list.
119 behat_base::type_keys($this->session, [behat_keys::ESCAPE]);
120 $this->wait_for_pending_js();