MDL-55713 behat: Adjust how NamedPartials are loaded
[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
48 // Call the constructor after adding any new selector or replacement values.
49 parent::__construct();
50 }
51
52 /**
53 * @var Allowed types when using text selectors arguments.
54 */
55 protected static $allowedtextselectors = array(
56 'activity' => 'activity',
57 'block' => 'block',
58 'css_element' => 'css_element',
59 'dialogue' => 'dialogue',
60 'fieldset' => 'fieldset',
61 'list_item' => 'list_item',
62 'question' => 'question',
63 'region' => 'region',
64 'section' => 'section',
65 'table' => 'table',
66 'table_row' => 'table_row',
67 'xpath_element' => 'xpath_element',
68 'form_row' => 'form_row',
69 );
70
71 /**
72 * @var Allowed types when using selector arguments.
73 */
74 protected static $allowedselectors = array(
75 'activity' => 'activity',
76 'block' => 'block',
77 'button' => 'button',
78 'checkbox' => 'checkbox',
79 'css_element' => 'css_element',
80 'dialogue' => 'dialogue',
81 'field' => 'field',
82 'fieldset' => 'fieldset',
83 'file' => 'file',
84 'filemanager' => 'filemanager',
85 'link' => 'link',
86 'link_or_button' => 'link_or_button',
87 'list_item' => 'list_item',
88 'optgroup' => 'optgroup',
89 'option' => 'option',
90 'question' => 'question',
91 'radio' => 'radio',
92 'region' => 'region',
93 'section' => 'section',
94 'select' => 'select',
95 'table' => 'table',
96 'table_row' => 'table_row',
97 'text' => 'text',
98 'xpath_element' => 'xpath_element',
99 'form_row' => 'form_row',
100 );
101
102 /**
103 * Behat by default comes with XPath, CSS and named selectors,
104 * named selectors are a mapping between names (like button) and
105 * xpaths that represents that names and includes a placeholder that
106 * will be replaced by the locator. These are Moodle's own xpaths.
107 *
108 * @var XPaths for moodle elements.
109 */
110 protected static $moodleselectors = array(
111 'activity' => <<<XPATH
112.//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][normalize-space(.) = %locator% ]
113XPATH
114 , 'block' => <<<XPATH
115.//*[@data-block][contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
116 descendant::*[self::h2|self::h3][normalize-space(.) = %locator%] or
117 @aria-label = %locator%]
118XPATH
119 , 'dialogue' => <<<XPATH
120.//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
121 normalize-space(descendant::div[
122 contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
123 ]) = %locator%] |
124.//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
125 normalize-space(descendant::div[@class='hd']) = %locator%]
126XPATH
127 , 'list_item' => <<<XPATH
128.//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
129XPATH
130 , 'question' => <<<XPATH
131.//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
132 [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
133XPATH
134 , 'region' => <<<XPATH
135.//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
136XPATH
137 , 'section' => <<<XPATH
138.//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
139 [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
140 contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
141.//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
142 [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
143XPATH
144 , 'table' => <<<XPATH
145.//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
146XPATH
147 , 'table_row' => <<<XPATH
148.//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
149XPATH
150 , 'text' => <<<XPATH
151.//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
152XPATH
153 , 'form_row' => <<<XPATH
154.//*[self::label or self::div[contains(concat(' ', @class, ' '), ' fstaticlabel ')]][contains(., %locator%)]/ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
155XPATH
156 );
157
158 protected static $customselectors = [
159 'field' => [
160 'upstream' => <<<XPATH
161.//*
162[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
163|
164.//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
165|
166.//*
167[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
168|
169.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
170XPATH
171 ,
172 'filemanager' => <<<XPATH
173+.//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
174XPATH
175 ],
176 ];
177
178 /**
179 * Allowed selectors getter.
180 *
181 * @return array
182 */
183 public static function get_allowed_selectors() {
184 return static::$allowedselectors;
185 }
186
187 /**
188 * Allowed text selectors getter.
189 *
190 * @return array
191 */
192 public static function get_allowed_text_selectors() {
193 return static::$allowedtextselectors;
194 }
195}