weekly release 3.8dev+
[moodle.git] / lib / behat / classes / partial_named_selector.php
CommitLineData
520071f0
AN
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Moodle-specific selectors.
19 *
20 * @package core
21 * @category test
22 * @copyright 2013 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26/**
27 * Moodle selectors manager.
28 *
29 * @package core
30 * @category test
31 * @copyright 2013 David MonllaĆ³
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class behat_partial_named_selector extends \Behat\Mink\Selector\PartialNamedSelector {
35
61832fae
AN
36 // Use the named selector trait.
37 use behat_named_selector;
38
520071f0
AN
39 /**
40 * Creates selector instance.
41 */
42 public function __construct() {
43 foreach (self::$customselectors as $alias => $selectors) {
44 $this->registerNamedXpath($alias, implode(' | ', $selectors));
45 }
46
47 foreach (static::$moodleselectors as $name => $xpath) {
48 $this->registerNamedXpath($name, $xpath);
49 }
50
1e815600
AN
51 foreach (self::$customreplacements as $from => $tos) {
52 $this->registerReplacement($from, implode(' or ', $tos));
53 }
54
8857c715
DW
55 $this->registerReplacement('%iconMatch%', "(contains(concat(' ', @class, ' '), ' icon ') or name() = 'img')");
56 $this->registerReplacement('%imgAltMatch%', './/*[%iconMatch% and (%altMatch% or %titleMatch%)]');
520071f0
AN
57 parent::__construct();
58 }
59
60 /**
1e815600 61 * @var array Allowed types when using text selectors arguments.
520071f0
AN
62 */
63 protected static $allowedtextselectors = array(
64 'activity' => 'activity',
65 'block' => 'block',
66 'css_element' => 'css_element',
67 'dialogue' => 'dialogue',
68 'fieldset' => 'fieldset',
8857c715 69 'icon' => 'icon',
520071f0
AN
70 'list_item' => 'list_item',
71 'question' => 'question',
72 'region' => 'region',
73 'section' => 'section',
74 'table' => 'table',
75 'table_row' => 'table_row',
76 'xpath_element' => 'xpath_element',
77 'form_row' => 'form_row',
e688eab8 78 'group_message_header' => 'group_message_header',
f0e8f0a5 79 'group_message' => 'group_message',
989f3800 80 'autocomplete' => 'autocomplete',
520071f0
AN
81 );
82
83 /**
1e815600 84 * @var array Allowed types when using selector arguments.
520071f0
AN
85 */
86 protected static $allowedselectors = array(
87 'activity' => 'activity',
88 'block' => 'block',
89 'button' => 'button',
90 'checkbox' => 'checkbox',
91 'css_element' => 'css_element',
92 'dialogue' => 'dialogue',
93 'field' => 'field',
94 'fieldset' => 'fieldset',
95 'file' => 'file',
96 'filemanager' => 'filemanager',
65f631c5 97 'group_message' => 'group_message',
d624c135 98 'group_message_conversation' => 'group_message_conversation',
65f631c5
AA
99 'group_message_header' => 'group_message_header',
100 'group_message_member' => 'group_message_member',
101 'group_message_tab' => 'group_message_tab',
e688eab8 102 'group_message_list_area' => 'group_message_list_area',
c961389b 103 'group_message_message_content' => 'group_message_message_content',
f0e8f0a5 104 'icon_container' => 'icon_container',
8857c715 105 'icon' => 'icon',
520071f0
AN
106 'link' => 'link',
107 'link_or_button' => 'link_or_button',
108 'list_item' => 'list_item',
109 'optgroup' => 'optgroup',
110 'option' => 'option',
111 'question' => 'question',
112 'radio' => 'radio',
113 'region' => 'region',
114 'section' => 'section',
115 'select' => 'select',
116 'table' => 'table',
117 'table_row' => 'table_row',
118 'text' => 'text',
119 'xpath_element' => 'xpath_element',
120 'form_row' => 'form_row',
ab2095fb 121 'autocomplete_selection' => 'autocomplete_selection',
1b63ff10 122 'autocomplete_suggestions' => 'autocomplete_suggestions',
989f3800 123 'autocomplete' => 'autocomplete',
520071f0
AN
124 );
125
126 /**
127 * Behat by default comes with XPath, CSS and named selectors,
128 * named selectors are a mapping between names (like button) and
129 * xpaths that represents that names and includes a placeholder that
130 * will be replaced by the locator. These are Moodle's own xpaths.
131 *
1e815600 132 * @var array XPaths for moodle elements.
520071f0
AN
133 */
134 protected static $moodleselectors = array(
135 'activity' => <<<XPATH
136.//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][normalize-space(.) = %locator% ]
137XPATH
138 , 'block' => <<<XPATH
139.//*[@data-block][contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
3ec69c2e 140 descendant::*[self::h2|self::h3|self::h4|self::h5][normalize-space(.) = %locator%] or
520071f0
AN
141 @aria-label = %locator%]
142XPATH
143 , 'dialogue' => <<<XPATH
144.//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
145 normalize-space(descendant::div[
146 contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
147 ]) = %locator%] |
148.//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
149 normalize-space(descendant::div[@class='hd']) = %locator%]
434d7869 150 |
dd4ac3d2
AN
151.//div[@data-region='modal' and descendant::*[@data-region='title'] = %locator%]
152 |
153.//div[
154 contains(concat(' ', normalize-space(@class), ' '), ' modal-content ')
155 and
3ec69c2e 156 normalize-space(descendant::*[self::h4 or self::h5][contains(concat(' ', normalize-space(@class), ' '), ' modal-title ')]) = %locator%
dd4ac3d2
AN
157 ]
158 |
159.//div[
160 contains(concat(' ', normalize-space(@class), ' '), ' modal ')
161 and
162 normalize-space(descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' modal-header ')] = %locator%)
163 ]
65f631c5
AA
164XPATH
165 , 'group_message' => <<<XPATH
166 .//*[@data-conversation-id]//img[contains(@alt, %locator%)]/..
167XPATH
d624c135
AA
168 , 'group_message_conversation' => <<<XPATH
169 .//*[@data-region='message-drawer' and contains(., %locator%)]//div[@data-region='content-message-container']
170XPATH
171 , 'group_message_header' => <<<XPATH
f0e8f0a5 172 .//*[@data-region='message-drawer']//div[@data-region='header-content' and contains(., %locator%)]
65f631c5
AA
173XPATH
174 , 'group_message_member' => <<<XPATH
175 .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
176 //div[@class='list-group' and not(contains(@class, 'hidden'))]//*[text()[contains(., %locator%)]] |
177 .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
9171b588 178 //div[@data-region='empty-message-container' and not(contains(@class, 'hidden')) and contains(., %locator%)]
65f631c5
AA
179XPATH
180 , 'group_message_tab' => <<<XPATH
a58fbd48 181 .//*[@data-region='message-drawer']//button[@data-toggle='collapse' and contains(string(), %locator%)]
e688eab8
AA
182XPATH
183 , 'group_message_list_area' => <<<XPATH
184 .//*[@data-region='message-drawer']//*[contains(@data-region, concat('view-overview-', %locator%))]
c961389b
AA
185XPATH
186 , 'group_message_message_content' => <<<XPATH
187 .//*[@data-region='message-drawer']//*[@data-region='message' and @data-message-id and contains(., %locator%)]
f0e8f0a5
AA
188XPATH
189 , 'icon_container' => <<<XPATH
190 .//span[contains(@data-region, concat(%locator%,'-icon-container'))]
8857c715
DW
191XPATH
192 , 'icon' => <<<XPATH
193.//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
520071f0
AN
194XPATH
195 , 'list_item' => <<<XPATH
196.//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
197XPATH
198 , 'question' => <<<XPATH
199.//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
200 [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
201XPATH
202 , 'region' => <<<XPATH
203.//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
204XPATH
205 , 'section' => <<<XPATH
206.//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
207 [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
208 contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
209.//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
210 [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
211XPATH
212 , 'table' => <<<XPATH
213.//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
214XPATH
215 , 'table_row' => <<<XPATH
216.//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
217XPATH
218 , 'text' => <<<XPATH
219.//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
220XPATH
221 , 'form_row' => <<<XPATH
222.//*[self::label or self::div[contains(concat(' ', @class, ' '), ' fstaticlabel ')]][contains(., %locator%)]/ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
ab2095fb
AG
223XPATH
224 , 'autocomplete_selection' => <<<XPATH
225.//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-selection', ' '))]/span[@role='listitem'][contains(normalize-space(.), %locator%)]
1b63ff10
SR
226XPATH
227 , 'autocomplete_suggestions' => <<<XPATH
228.//ul[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-suggestions', ' '))]/li[@role='option'][contains(normalize-space(.), %locator%)]
989f3800
AN
229XPATH
230 , 'autocomplete' => <<<XPATH
231.//descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]/ancestor::*[@data-fieldtype = 'autocomplete']
520071f0
AN
232XPATH
233 );
234
235 protected static $customselectors = [
236 'field' => [
237 'upstream' => <<<XPATH
238.//*
239[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
240|
241.//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
242|
243.//*
244[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
245|
246.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
247XPATH
248 ,
249 'filemanager' => <<<XPATH
6ee51d01 250.//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
d9821755 251 /descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
ca25005c
AN
252XPATH
253 ,
254 'passwordunmask' => <<<XPATH
255.//*[@data-passwordunmask='wrapper']
737b54fe 256 /descendant::input[@id = %locator% or @id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
520071f0
AN
257XPATH
258 ],
259 ];
260
1e815600
AN
261 /**
262 * Mink comes with a number of named replacements.
263 * Sometimes we want to add our own.
264 *
265 * @var array XPaths for moodle elements.
266 */
267 protected static $customreplacements = [
268 '%buttonMatch%' => [
269 'upstream' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
270 'aria' => '%ariaLabelMatch%',
271 ],
272 '%ariaLabelMatch%' => [
273 'moodle' => 'contains(./@aria-label, %locator%)',
274 ],
275 ];
276
61832fae
AN
277 /** @var List of deprecated selectors */
278 protected static $deprecatedselectors = [
5c783f14
AN
279 'group_message' => 'core_message > Message',
280 'group_message_member' => 'core_message > Message member',
281 'group_message_tab' => 'core_message > Message tab',
282 'group_message_list_area' => 'core_message > Message list area',
283 'group_message_message_content' => 'core_message > Message content',
61832fae
AN
284 ];
285
520071f0
AN
286 /**
287 * Allowed selectors getter.
288 *
289 * @return array
290 */
291 public static function get_allowed_selectors() {
292 return static::$allowedselectors;
293 }
294
295 /**
296 * Allowed text selectors getter.
297 *
298 * @return array
299 */
300 public static function get_allowed_text_selectors() {
301 return static::$allowedtextselectors;
302 }
303}