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