weekly 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 67 'list_item' => 'list_item',
f05c1013
RW
68 'message_area_region' => 'message_area_region',
69 'message_area_region_content' => 'message_area_region_content',
520071f0
AN
70 'question' => 'question',
71 'region' => 'region',
72 'section' => 'section',
73 'table' => 'table',
74 'table_row' => 'table_row',
75 'xpath_element' => 'xpath_element',
76 'form_row' => 'form_row',
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',
8857c715 93 'icon' => 'icon',
520071f0
AN
94 'link' => 'link',
95 'link_or_button' => 'link_or_button',
96 'list_item' => 'list_item',
f05c1013
RW
97 'message_area_action' => 'message_area_action',
98 'message_area_region' => 'message_area_region',
99 'message_area_region_content' => 'message_area_region_content',
520071f0
AN
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 ]
8857c715
DW
154XPATH
155 , 'icon' => <<<XPATH
156.//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
520071f0
AN
157XPATH
158 , 'list_item' => <<<XPATH
159.//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
160XPATH
161 , 'question' => <<<XPATH
162.//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
163 [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
164XPATH
165 , 'region' => <<<XPATH
166.//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
167XPATH
168 , 'section' => <<<XPATH
169.//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
170 [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
171 contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
172.//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
173 [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
174XPATH
175 , 'table' => <<<XPATH
176.//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
177XPATH
178 , 'table_row' => <<<XPATH
179.//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
180XPATH
181 , 'text' => <<<XPATH
182.//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
183XPATH
184 , 'form_row' => <<<XPATH
185.//*[self::label or self::div[contains(concat(' ', @class, ' '), ' fstaticlabel ')]][contains(., %locator%)]/ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
f05c1013
RW
186XPATH
187 , 'message_area_region' => <<<XPATH
f074d6f0 188.//div[@data-region='message-drawer']/descendant::*[@data-region = %locator%]
f05c1013
RW
189XPATH
190 , 'message_area_region_content' => <<<XPATH
f074d6f0 191.//div[@data-region='message-drawer']/descendant::*[@data-region-content = %locator%]
f05c1013
RW
192XPATH
193 , 'message_area_action' => <<<XPATH
f074d6f0 194.//div[@data-region='message-drawer']/descendant::*[@data-action = %locator%]
ab2095fb
AG
195XPATH
196 , 'autocomplete_selection' => <<<XPATH
197.//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-selection', ' '))]/span[@role='listitem'][contains(normalize-space(.), %locator%)]
1b63ff10
SR
198XPATH
199 , 'autocomplete_suggestions' => <<<XPATH
200.//ul[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-suggestions', ' '))]/li[@role='option'][contains(normalize-space(.), %locator%)]
520071f0
AN
201XPATH
202 );
203
204 protected static $customselectors = [
205 'field' => [
206 'upstream' => <<<XPATH
207.//*
208[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
209|
210.//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
211|
212.//*
213[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
214|
215.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
216XPATH
217 ,
218 'filemanager' => <<<XPATH
6ee51d01 219.//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
d9821755 220 /descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
ca25005c
AN
221XPATH
222 ,
223 'passwordunmask' => <<<XPATH
224.//*[@data-passwordunmask='wrapper']
737b54fe 225 /descendant::input[@id = %locator% or @id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
520071f0
AN
226XPATH
227 ],
228 ];
229
1e815600
AN
230 /**
231 * Mink comes with a number of named replacements.
232 * Sometimes we want to add our own.
233 *
234 * @var array XPaths for moodle elements.
235 */
236 protected static $customreplacements = [
237 '%buttonMatch%' => [
238 'upstream' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
239 'aria' => '%ariaLabelMatch%',
240 ],
241 '%ariaLabelMatch%' => [
242 'moodle' => 'contains(./@aria-label, %locator%)',
243 ],
244 ];
245
520071f0
AN
246 /**
247 * Allowed selectors getter.
248 *
249 * @return array
250 */
251 public static function get_allowed_selectors() {
252 return static::$allowedselectors;
253 }
254
255 /**
256 * Allowed text selectors getter.
257 *
258 * @return array
259 */
260 public static function get_allowed_text_selectors() {
261 return static::$allowedtextselectors;
262 }
263}