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 * Moodle-specific selectors.
22 * @copyright 2013 David Monllaó
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * Moodle selectors manager.
31 * @copyright 2013 David Monllaó
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class behat_partial_named_selector extends \Behat\Mink\Selector\PartialNamedSelector {
36 // Use the named selector trait.
37 use behat_named_selector;
40 * Creates selector instance.
42 public function __construct() {
43 foreach (self::$customselectors as $alias => $selectors) {
44 $this->registerNamedXpath($alias, implode(' | ', $selectors));
47 foreach (static::$moodleselectors as $name => $xpath) {
48 $this->registerNamedXpath($name, $xpath);
51 foreach (self::$customreplacements as $from => $tos) {
52 $this->registerReplacement($from, implode(' or ', $tos));
55 $this->registerReplacement('%iconMatch%', "(contains(concat(' ', @class, ' '), ' icon ') or name() = 'img')");
56 $this->registerReplacement('%imgAltMatch%', './/*[%iconMatch% and (%altMatch% or %titleMatch%)]');
57 parent::__construct();
61 * @var array Allowed types when using text selectors arguments.
63 protected static $allowedtextselectors = array(
64 'activity' => 'activity',
66 'css_element' => 'css_element',
67 'dialogue' => 'dialogue',
68 'fieldset' => 'fieldset',
70 'list_item' => 'list_item',
71 'question' => 'question',
73 'section' => 'section',
75 'table_row' => 'table_row',
76 'xpath_element' => 'xpath_element',
77 'form_row' => 'form_row',
78 'group_message_header' => 'group_message_header',
79 'group_message' => 'group_message',
80 'autocomplete' => 'autocomplete',
85 * @var array Allowed types when using selector arguments.
87 protected static $allowedselectors = array(
88 'activity' => 'activity',
91 'checkbox' => 'checkbox',
92 'css_element' => 'css_element',
93 'dialogue' => 'dialogue',
95 'fieldset' => 'fieldset',
97 'filemanager' => 'filemanager',
98 'group_message' => 'group_message',
99 'group_message_conversation' => 'group_message_conversation',
100 'group_message_header' => 'group_message_header',
101 'group_message_member' => 'group_message_member',
102 'group_message_tab' => 'group_message_tab',
103 'group_message_list_area' => 'group_message_list_area',
104 'group_message_message_content' => 'group_message_message_content',
105 'icon_container' => 'icon_container',
108 'link_or_button' => 'link_or_button',
109 'list_item' => 'list_item',
110 'optgroup' => 'optgroup',
111 'option' => 'option',
112 'question' => 'question',
114 'region' => 'region',
115 'section' => 'section',
116 'select' => 'select',
118 'table_row' => 'table_row',
120 'xpath_element' => 'xpath_element',
121 'form_row' => 'form_row',
122 'autocomplete_selection' => 'autocomplete_selection',
123 'autocomplete_suggestions' => 'autocomplete_suggestions',
124 'autocomplete' => 'autocomplete',
125 'iframe' => 'iframe',
129 * Behat by default comes with XPath, CSS and named selectors,
130 * named selectors are a mapping between names (like button) and
131 * xpaths that represents that names and includes a placeholder that
132 * will be replaced by the locator. These are Moodle's own xpaths.
134 * @var array XPaths for moodle elements.
136 protected static $moodleselectors = array(
137 'activity' => <<<XPATH
138 .//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][normalize-space(.) = %locator% ]
140 , 'block' => <<<XPATH
141 .//*[@data-block][contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
142 descendant::*[self::h2|self::h3|self::h4|self::h5][normalize-space(.) = %locator%] or
143 @aria-label = %locator%]
145 , 'dialogue' => <<<XPATH
146 .//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
147 normalize-space(descendant::div[
148 contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
150 .//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
151 normalize-space(descendant::div[@class='hd']) = %locator%]
153 .//div[@data-region='modal' and descendant::*[@data-region='title'] = %locator%]
156 contains(concat(' ', normalize-space(@class), ' '), ' modal-content ')
158 normalize-space(descendant::*[self::h4 or self::h5][contains(concat(' ', normalize-space(@class), ' '), ' modal-title ')]) = %locator%
162 contains(concat(' ', normalize-space(@class), ' '), ' modal ')
164 normalize-space(descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' modal-header ')]) = %locator%
167 , 'group_message' => <<<XPATH
168 .//*[@data-conversation-id]//img[contains(@alt, %locator%)]/..
170 , 'group_message_conversation' => <<<XPATH
171 .//*[@data-region='message-drawer' and contains(., %locator%)]//div[@data-region='content-message-container']
173 , 'group_message_header' => <<<XPATH
174 .//*[@data-region='message-drawer']//div[@data-region='header-content' and contains(., %locator%)]
176 , 'group_message_member' => <<<XPATH
177 .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
178 //div[@class='list-group' and not(contains(@class, 'hidden'))]//*[text()[contains(., %locator%)]] |
179 .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
180 //div[@data-region='empty-message-container' and not(contains(@class, 'hidden')) and contains(., %locator%)]
182 , 'group_message_tab' => <<<XPATH
183 .//*[@data-region='message-drawer']//button[@data-toggle='collapse' and contains(string(), %locator%)]
185 , 'group_message_list_area' => <<<XPATH
186 .//*[@data-region='message-drawer']//*[contains(@data-region, concat('view-overview-', %locator%))]
188 , 'group_message_message_content' => <<<XPATH
189 .//*[@data-region='message-drawer']//*[@data-region='message' and @data-message-id and contains(., %locator%)]
191 , 'icon_container' => <<<XPATH
192 .//span[contains(@data-region, concat(%locator%,'-icon-container'))]
195 .//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
197 , 'list_item' => <<<XPATH
198 .//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
200 , 'question' => <<<XPATH
201 .//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
202 [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
204 , 'region' => <<<XPATH
205 .//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
207 , 'section' => <<<XPATH
208 .//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
209 [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
210 contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
211 .//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
212 [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
214 , 'table' => <<<XPATH
215 .//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
217 , 'table_row' => <<<XPATH
218 .//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
221 .//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
223 , 'form_row' => <<<XPATH
224 .//*[contains(concat(' ', @class, ' '), ' col-form-label ')]
225 [normalize-space(.)= %locator%]
226 /ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
228 , 'autocomplete_selection' => <<<XPATH
229 .//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-selection', ' '))]/span[@role='listitem'][contains(normalize-space(.), %locator%)]
231 , 'autocomplete_suggestions' => <<<XPATH
232 .//ul[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-suggestions', ' '))]/li[@role='option'][contains(normalize-space(.), %locator%)]
234 , 'autocomplete' => <<<XPATH
235 .//descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]/ancestor::*[@data-fieldtype = 'autocomplete']
237 , 'iframe' => <<<XPATH
238 .//iframe[contains(concat(' ', normalize-space(@class), ' '), %locator% )]
242 protected static $customselectors = [
244 'upstream' => <<<XPATH
246 [%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
248 .//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
251 [%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
253 .//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
256 'filemanager' => <<<XPATH
257 .//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
258 /descendant::input[@id = substring-before(//p[contains(normalize-space(string(.)), %locator%)]/@id, '_label')]
261 'passwordunmask' => <<<XPATH
262 .//*[@data-passwordunmask='wrapper']
263 /descendant::input[@id = %locator% or @id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
269 * Mink comes with a number of named replacements.
270 * Sometimes we want to add our own.
272 * @var array XPaths for moodle elements.
274 protected static $customreplacements = [
276 'upstream' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
277 'aria' => '%ariaLabelMatch%',
279 '%ariaLabelMatch%' => [
280 'moodle' => 'contains(./@aria-label, %locator%)',
284 /** @var List of deprecated selectors */
285 protected static $deprecatedselectors = [
286 'group_message' => 'core_message > Message',
287 'group_message_member' => 'core_message > Message member',
288 'group_message_tab' => 'core_message > Message tab',
289 'group_message_list_area' => 'core_message > Message list area',
290 'group_message_message_content' => 'core_message > Message content',
294 * Allowed selectors getter.
298 public static function get_allowed_selectors() {
299 return static::$allowedselectors;
303 * Allowed text selectors getter.
307 public static function get_allowed_text_selectors() {
308 return static::$allowedtextselectors;