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