Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 */
34
class behat_partial_named_selector extends \Behat\Mink\Selector\PartialNamedSelector {
35
 
36
    // Use the named selector trait.
37
    use behat_named_selector;
38
 
39
    /**
40
     * Creates selector instance.
41
     */
42
    public function __construct() {
43
        foreach (self::$customselectors as $alias => $selectors) {
44
            $this->registerNamedXpath($alias, implode(' | ', $selectors));
45
        }
46
 
47
        foreach (static::$moodleselectors as $name => $xpath) {
48
            $this->registerNamedXpath($name, $xpath);
49
        }
50
 
51
        foreach (self::$customreplacements as $from => $tos) {
52
            $this->registerReplacement($from, implode(' or ', $tos));
53
        }
54
 
55
        $this->registerReplacement('%iconMatch%', "(contains(concat(' ', @class, ' '), ' icon ') or self::img)");
56
        $this->registerReplacement('%imgAltMatch%', './/*[%iconMatch% and (%altMatch% or %titleMatch%)]');
57
        parent::__construct();
58
    }
59
 
60
    /**
61
     * @var array Allowed types when using text selectors arguments.
62
     */
63
    protected static $allowedtextselectors = array(
64
        'activity' => 'activity',
65
        'block' => 'block',
66
        'css_element' => 'css_element',
67
        'dialogue' => 'dialogue',
68
        'dropdown_item' => 'dropdown_item',
69
        'fieldset' => 'fieldset',
70
        'icon' => 'icon',
71
        'list_item' => 'list_item',
72
        'question' => 'question',
73
        'region' => 'region',
74
        'section' => 'section',
75
        'table' => 'table',
76
        'table_row' => 'table_row',
77
        'xpath_element' => 'xpath_element',
78
        'form_row' => 'form_row',
79
        'group_message_header' => 'group_message_header',
80
        'group_message' => 'group_message',
81
        'autocomplete' => 'autocomplete',
82
        'iframe' => 'iframe',
83
        'option_role' => 'option_role',
84
    );
85
 
86
    /**
87
     * @var array Allowed types when using selector arguments.
88
     */
89
    protected static $allowedselectors = array(
90
        'activity' => 'activity',
91
        'actionmenu' => 'actionmenu',
92
        'badge' => 'badge',
93
        'block' => 'block',
94
        'button' => 'button',
95
        'checkbox' => 'checkbox',
96
        'combobox' => 'combobox',
97
        'css_element' => 'css_element',
98
        'dialogue' => 'dialogue',
99
        'dropdown' => 'dropdown',
100
        'dropdown_item' => 'dropdown_item',
101
        'field' => 'field',
102
        'fieldset' => 'fieldset',
103
        'file' => 'file',
104
        'filemanager' => 'filemanager',
105
        'group_message' => 'group_message',
106
        'group_message_conversation' => 'group_message_conversation',
107
        'group_message_header' => 'group_message_header',
108
        'group_message_member' => 'group_message_member',
109
        'group_message_tab' => 'group_message_tab',
110
        'group_message_list_area' => 'group_message_list_area',
111
        'group_message_message_content' => 'group_message_message_content',
112
        'heading' => 'heading',
113
        'icon_container' => 'icon_container',
114
        'icon' => 'icon',
115
        'link' => 'link',
116
        'link_or_button' => 'link_or_button',
117
        'list_item' => 'list_item',
118
        'menuitem' => 'menuitem',
119
        'optgroup' => 'optgroup',
120
        'option' => 'option',
121
        'option_role' => 'option_role',
122
        'question' => 'question',
123
        'radio' => 'radio',
124
        'region' => 'region',
125
        'section' => 'section',
126
        'select' => 'select',
127
        'table' => 'table',
128
        'table_row' => 'table_row',
129
        'text' => 'text',
130
        'xpath_element' => 'xpath_element',
131
        'form_row' => 'form_row',
132
        'autocomplete_selection' => 'autocomplete_selection',
133
        'autocomplete_suggestions' => 'autocomplete_suggestions',
134
        'autocomplete' => 'autocomplete',
135
        'iframe' => 'iframe',
136
    );
137
 
138
    /**
139
     * Behat by default comes with XPath, CSS and named selectors,
140
     * named selectors are a mapping between names (like button) and
141
     * xpaths that represents that names and includes a placeholder that
142
     * will be replaced by the locator. These are Moodle's own xpaths.
143
     *
144
     * @var array XPaths for moodle elements.
145
     */
146
    protected static $moodleselectors = array(
147
        'activity' => <<<XPATH
148
.//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][descendant::*[contains(normalize-space(.), %locator%)]]
149
XPATH
150
        , 'actionmenu' => <<<XPATH
151
.//*[
152
    contains(concat(' ', normalize-space(@class), ' '), ' action-menu ')
153
        and
154
    descendant::*[
155
        contains(concat(' ', normalize-space(@class), ' '), ' dropdown-toggle ')
156
            and
157
        (contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
158
    ]
159
]
160
XPATH
161
        , 'badge' => <<<XPATH
162
.//*[self::span or self::button][(contains(@class, 'badge')) and text()[contains(., %locator%)]]
163
XPATH
164
        , 'block' => <<<XPATH
165
.//*[@data-block][contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
166
     descendant::*[self::h2|self::h3|self::h4|self::h5][normalize-space(.) = %locator%]  or
167
     @aria-label = %locator%]
168
XPATH
169
        , 'combobox' => <<<XPATH
170
.//*[@role='combobox'][%titleMatch% or %ariaLabelMatch% or text()[contains(., %locator%)]]
171
XPATH
172
        , 'dialogue' => <<<XPATH
173
.//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
174
    not(contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hidden ')) and
175
    normalize-space(descendant::div[
176
        contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
177
        ]) = %locator%] |
178
.//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
179
    normalize-space(descendant::div[@class='hd']) = %locator%]
180
        |
181
.//div[@data-region='modal' and descendant::*[@data-region='title'] = %locator%]
182
        |
183
.//div[
184
        contains(concat(' ', normalize-space(@class), ' '), ' modal-content ')
185
            and
186
        normalize-space(descendant::*[self::h4 or self::h5][contains(concat(' ', normalize-space(@class), ' '), ' modal-title ')]) = %locator%
187
    ]
188
        |
189
.//div[
190
        contains(concat(' ', normalize-space(@class), ' '), ' modal ')
191
            and
192
        normalize-space(descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' modal-header ')]) = %locator%
193
    ]
194
XPATH
195
    , 'dropdown' => <<<XPATH
196
        .//*[
197
            contains(concat(' ', normalize-space(@class), ' '), ' dropdown-menu ')
198
                and
199
            @aria-labelledby =
200
                (//*[
201
                        contains(concat(' ', normalize-space(@class), ' '), ' dropdown-toggle ')
202
                            and
203
                        (contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
204
                ]/@id)
205
        ]
206
XPATH
207
    , 'dropdown_item' => <<<XPATH
208
        .//*[
209
            @role = 'listitem'
210
                and
211
            (contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
212
        ]
213
XPATH
214
        , 'group_message' => <<<XPATH
215
        .//*[@data-conversation-id]//img[contains(@alt, %locator%)]/..
216
XPATH
217
        , 'group_message_conversation' => <<<XPATH
218
            .//*[@data-region='message-drawer' and contains(., %locator%)]//div[@data-region='content-message-container']
219
XPATH
220
    , 'group_message_header' => <<<XPATH
221
        .//*[@data-region='message-drawer']//div[@data-region='header-content' and contains(., %locator%)]
222
XPATH
223
    , 'group_message_member' => <<<XPATH
224
        .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
225
        //div[@class='list-group' and not(contains(@class, 'hidden'))]//*[text()[contains(., %locator%)]] |
226
        .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
227
        //div[@data-region='empty-message-container' and not(contains(@class, 'hidden')) and contains(., %locator%)]
228
XPATH
229
    , 'group_message_tab' => <<<XPATH
230
        .//*[@data-region='message-drawer']//button[@data-toggle='collapse' and contains(string(), %locator%)]
231
XPATH
232
    , 'group_message_list_area' => <<<XPATH
233
        .//*[@data-region='message-drawer']//*[contains(@data-region, concat('view-overview-', %locator%))]
234
XPATH
235
    , 'group_message_message_content' => <<<XPATH
236
        .//*[@data-region='message-drawer']//*[@data-region='message' and @data-message-id and contains(., %locator%)]
237
XPATH
238
    , 'heading' => <<<XPATH
239
        .//*[self::h1 or self::h2 or self::h3 or self::h4 or self::h5 or self::h6][contains(normalize-space(.), %locator%)]
240
XPATH
241
    , 'icon_container' => <<<XPATH
242
        .//span[contains(@data-region, concat(%locator%,'-icon-container'))]
243
XPATH
244
        , 'icon' => <<<XPATH
245
.//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
246
XPATH
247
        , 'list_item' => <<<XPATH
248
.//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
249
XPATH
250
        , 'menuitem' => <<<XPATH
251
.//*[@role='menuitem'][%titleMatch% or %ariaLabelMatch% or text()[contains(., %locator%)]]
252
XPATH
253
    , 'option_role' => <<<XPATH
254
.//*[@role='option'][%titleMatch% or %ariaLabelMatch% or text()[contains(., %locator%)]] |
255
.//*[@role='option']/following-sibling::label[contains(., %locator%)]/preceding-sibling::input
256
XPATH
257
        , 'question' => <<<XPATH
258
.//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
259
    [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
260
XPATH
261
        , 'region' => <<<XPATH
262
.//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
263
XPATH
264
        , 'section' => <<<XPATH
265
.//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
266
    [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
267
    contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
268
.//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
269
    [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
270
XPATH
271
        , 'table' => <<<XPATH
272
.//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
273
XPATH
274
        , 'table_row' => <<<XPATH
275
.//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
276
XPATH
277
        , 'text' => <<<XPATH
278
.//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
279
XPATH
280
        , 'form_row' => <<<XPATH
281
.//*[contains(concat(' ', @class, ' '), ' col-form-label ')]
282
    [normalize-space(.)= %locator%]
283
    /ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
284
XPATH
285
        , 'autocomplete_selection' => <<<XPATH
286
.//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-selection', ' '))]/span[@role='option'][contains(normalize-space(.), %locator%)]
287
XPATH
288
        , 'autocomplete_suggestions' => <<<XPATH
289
.//ul[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-suggestions', ' '))]/li[@role='option'][contains(normalize-space(.), %locator%)]
290
XPATH
291
        , 'autocomplete' => <<<XPATH
292
.//descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]/ancestor::*[@data-fieldtype = 'autocomplete']
293
XPATH
294
        , 'iframe' => <<<XPATH
295
.//iframe[(%idOrNameMatch% or (contains(concat(' ', normalize-space(@class), ' '), %locator% )))]
296
XPATH
297
    );
298
 
299
    protected static $customselectors = [
300
        'field' => [
301
            'upstream' => <<<XPATH
302
.//*
303
[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
304
|
305
.//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
306
|
307
.//*
308
[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
309
|
310
.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
311
XPATH
312
        ,
313
            'filemanager' => <<<XPATH
314
.//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
315
    /descendant::input[@id = substring-before(//p[contains(normalize-space(string(.)), %locator%)]/@id, '_label')]
316
XPATH
317
        ,
318
             'passwordunmask' => <<<XPATH
319
.//*[@data-passwordunmask='wrapper']
320
    /descendant::input[@id = %locator% or @id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
321
XPATH
322
        ,
323
             'inplaceeditable' => <<<XPATH
324
.//descendant::span[@data-inplaceeditable][descendant::a[%titleMatch%]]
325
XPATH
326
        ,
327
            'date_time' => <<<XPATH
328
.//fieldset[(%idMatch% or ./legend[%exactTagTextMatch%]) and (@data-fieldtype='date' or @data-fieldtype='date_time')]
329
XPATH
330
        ,
331
            'select_menu' => <<<XPATH
332
//*[@role='combobox'][@aria-labelledby = //label[contains(normalize-space(string(.)), %locator%)]/@id]
333
XPATH
334
        ,
335
        ],
336
    ];
337
 
338
    /**
339
     * Mink comes with a number of named replacements.
340
     * Sometimes we want to add our own.
341
     *
342
     * @var array XPaths for moodle elements.
343
     */
344
    protected static $customreplacements = [
345
        '%buttonMatch%' => [
346
            'upstream' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
347
            'aria' => '%ariaLabelMatch%',
348
        ],
349
        '%ariaLabelMatch%' => [
350
            'moodle' => 'contains(./@aria-label, %locator%)',
351
        ],
352
        '%exactTagTextMatch%' => [
353
            // This is based upon the upstream tagTextMatch but performs an exact match rather than a loose match using
354
            // contains().
355
            // If possible we should only use exact matches for any new form fields that we add.
356
            'moodle' => 'normalize-space(text())=%locator%',
357
        ],
358
    ];
359
 
360
    /** @var List of deprecated selectors */
361
    protected static $deprecatedselectors = [
362
        'group_message' => 'core_message > Message',
363
        'group_message_member' => 'core_message > Message member',
364
        'group_message_tab' => 'core_message > Message tab',
365
        'group_message_list_area' => 'core_message > Message list area',
366
        'group_message_message_content' => 'core_message > Message content',
367
    ];
368
 
369
    /**
370
     * Allowed selectors getter.
371
     *
372
     * @return array
373
     */
374
    public static function get_allowed_selectors() {
375
        return static::$allowedselectors;
376
    }
377
 
378
    /**
379
     * Allowed text selectors getter.
380
     *
381
     * @return array
382
     */
383
    public static function get_allowed_text_selectors() {
384
        return static::$allowedtextselectors;
385
    }
386
}