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