weekly release 3.4dev
[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
8857c715
DW
48 $this->registerReplacement('%iconMatch%', "(contains(concat(' ', @class, ' '), ' icon ') or name() = 'img')");
49 $this->registerReplacement('%imgAltMatch%', './/*[%iconMatch% and (%altMatch% or %titleMatch%)]');
520071f0
AN
50 parent::__construct();
51 }
52
53 /**
54 * @var Allowed types when using text selectors arguments.
55 */
56 protected static $allowedtextselectors = array(
57 'activity' => 'activity',
58 'block' => 'block',
59 'css_element' => 'css_element',
60 'dialogue' => 'dialogue',
61 'fieldset' => 'fieldset',
8857c715 62 'icon' => 'icon',
520071f0 63 'list_item' => 'list_item',
f05c1013
RW
64 'message_area_region' => 'message_area_region',
65 'message_area_region_content' => 'message_area_region_content',
520071f0
AN
66 'question' => 'question',
67 'region' => 'region',
68 'section' => 'section',
69 'table' => 'table',
70 'table_row' => 'table_row',
71 'xpath_element' => 'xpath_element',
72 'form_row' => 'form_row',
73 );
74
75 /**
76 * @var Allowed types when using selector arguments.
77 */
78 protected static $allowedselectors = array(
79 'activity' => 'activity',
80 'block' => 'block',
81 'button' => 'button',
82 'checkbox' => 'checkbox',
83 'css_element' => 'css_element',
84 'dialogue' => 'dialogue',
85 'field' => 'field',
86 'fieldset' => 'fieldset',
87 'file' => 'file',
88 'filemanager' => 'filemanager',
8857c715 89 'icon' => 'icon',
520071f0
AN
90 'link' => 'link',
91 'link_or_button' => 'link_or_button',
92 'list_item' => 'list_item',
f05c1013
RW
93 'message_area_action' => 'message_area_action',
94 'message_area_region' => 'message_area_region',
95 'message_area_region_content' => 'message_area_region_content',
520071f0
AN
96 'optgroup' => 'optgroup',
97 'option' => 'option',
98 'question' => 'question',
99 'radio' => 'radio',
100 'region' => 'region',
101 'section' => 'section',
102 'select' => 'select',
103 'table' => 'table',
104 'table_row' => 'table_row',
105 'text' => 'text',
106 'xpath_element' => 'xpath_element',
107 'form_row' => 'form_row',
108 );
109
110 /**
111 * Behat by default comes with XPath, CSS and named selectors,
112 * named selectors are a mapping between names (like button) and
113 * xpaths that represents that names and includes a placeholder that
114 * will be replaced by the locator. These are Moodle's own xpaths.
115 *
116 * @var XPaths for moodle elements.
117 */
118 protected static $moodleselectors = array(
119 'activity' => <<<XPATH
120.//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][normalize-space(.) = %locator% ]
121XPATH
122 , 'block' => <<<XPATH
123.//*[@data-block][contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
124 descendant::*[self::h2|self::h3][normalize-space(.) = %locator%] or
125 @aria-label = %locator%]
126XPATH
127 , 'dialogue' => <<<XPATH
128.//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
129 normalize-space(descendant::div[
130 contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
131 ]) = %locator%] |
132.//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
133 normalize-space(descendant::div[@class='hd']) = %locator%]
8857c715
DW
134XPATH
135 , 'icon' => <<<XPATH
136.//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
520071f0
AN
137XPATH
138 , 'list_item' => <<<XPATH
139.//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
140XPATH
141 , 'question' => <<<XPATH
142.//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
143 [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
144XPATH
145 , 'region' => <<<XPATH
146.//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
147XPATH
148 , 'section' => <<<XPATH
149.//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
150 [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
151 contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
152.//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
153 [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
154XPATH
155 , 'table' => <<<XPATH
156.//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
157XPATH
158 , 'table_row' => <<<XPATH
159.//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
160XPATH
161 , 'text' => <<<XPATH
162.//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
163XPATH
164 , 'form_row' => <<<XPATH
165.//*[self::label or self::div[contains(concat(' ', @class, ' '), ' fstaticlabel ')]][contains(., %locator%)]/ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
f05c1013
RW
166XPATH
167 , 'message_area_region' => <<<XPATH
168.//div[@data-region='messaging-area']/descendant::*[@data-region = %locator%]
169XPATH
170 , 'message_area_region_content' => <<<XPATH
171.//div[@data-region='messaging-area']/descendant::*[@data-region-content = %locator%]
172XPATH
173 , 'message_area_action' => <<<XPATH
174.//div[@data-region='messaging-area']/descendant::*[@data-action = %locator%]
520071f0
AN
175XPATH
176 );
177
178 protected static $customselectors = [
179 'field' => [
180 'upstream' => <<<XPATH
181.//*
182[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
183|
184.//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
185|
186.//*
187[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
188|
189.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
190XPATH
191 ,
192 'filemanager' => <<<XPATH
6ee51d01 193.//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
d9821755 194 /descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
ca25005c
AN
195XPATH
196 ,
197 'passwordunmask' => <<<XPATH
198.//*[@data-passwordunmask='wrapper']
737b54fe 199 /descendant::input[@id = %locator% or @id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
520071f0
AN
200XPATH
201 ],
202 ];
203
204 /**
205 * Allowed selectors getter.
206 *
207 * @return array
208 */
209 public static function get_allowed_selectors() {
210 return static::$allowedselectors;
211 }
212
213 /**
214 * Allowed text selectors getter.
215 *
216 * @return array
217 */
218 public static function get_allowed_text_selectors() {
219 return static::$allowedtextselectors;
220 }
221}