mirrored from git://git.moodle.org/moodle.git
/
behat_selectors.php
161 lines (147 loc) · 5.3 KB
/
behat_selectors.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Moodle-specific selectors.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Moodle selectors manager.
*
* @package core
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_selectors {
/**
* @var Allowed types when using text selectors arguments.
*/
protected static $allowedtextselectors = array(
'dialogue' => 'dialogue',
'block' => 'block',
'region' => 'region',
'table_row' => 'table_row',
'table' => 'table',
'fieldset' => 'fieldset',
'css_element' => 'css_element',
'xpath_element' => 'xpath_element'
);
/**
* @var Allowed types when using selector arguments.
*/
protected static $allowedselectors = array(
'dialogue' => 'dialogue',
'block' => 'block',
'region' => 'region',
'table_row' => 'table_row',
'link' => 'link',
'button' => 'button',
'link_or_button' => 'link_or_button',
'select' => 'select',
'checkbox' => 'checkbox',
'radio' => 'radio',
'file' => 'file',
'optgroup' => 'optgroup',
'option' => 'option',
'table' => 'table',
'field' => 'field',
'fieldset' => 'fieldset',
'css_element' => 'css_element',
'xpath_element' => 'xpath_element'
);
/**
* Behat by default comes with XPath, CSS and named selectors,
* named selectors are a mapping between names (like button) and
* xpaths that represents that names and includes a placeholder that
* will be replaced by the locator. These are Moodle's own xpaths.
*
* @var XPaths for moodle elements.
*/
protected static $moodleselectors = array(
'dialogue' => <<<XPATH
.//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ')]/descendant::h1[normalize-space(.) = %locator%]/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ')]
XPATH
, 'block' => <<<XPATH
.//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' '))] | .//div[contains(concat(' ', normalize-space(@class), ' '), ' block ')]/descendant::h2[normalize-space(.) = %locator%]/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' block ')]
XPATH
, 'region' => <<<XPATH
.//div[./@id = %locator%]
XPATH
, 'table_row' => <<<XPATH
.//tr[contains(normalize-space(.), %locator%)]
XPATH
);
/**
* Returns the behat selector and locator for a given moodle selector and locator
*
* @param string $selectortype The moodle selector type, which includes moodle selectors
* @param string $element The locator we look for in that kind of selector
* @param Session $session The Mink opened session
* @return array Contains the selector and the locator expected by Mink.
*/
public static function get_behat_selector($selectortype, $element, Behat\Mink\Session $session) {
// CSS and XPath selectors locator is one single argument.
if ($selectortype == 'css_element' || $selectortype == 'xpath_element') {
$selector = str_replace('_element', '', $selectortype);
$locator = $element;
} else {
// Named selectors uses arrays as locators including the type of named selector.
$locator = array($selectortype, $session->getSelectorsHandler()->xpathLiteral($element));
$selector = 'named';
}
return array($selector, $locator);
}
/**
* Adds moodle selectors as behat named selectors.
*
* @param Session $session The mink session
* @return void
*/
public static function register_moodle_selectors(Behat\Mink\Session $session) {
foreach (self::get_moodle_selectors() as $name => $xpath) {
$session->getSelectorsHandler()->getSelector('named')->registerNamedXpath($name, $xpath);
}
}
/**
* Allowed selectors getter.
*
* @return array
*/
public static function get_allowed_selectors() {
return self::$allowedselectors;
}
/**
* Allowed text selectors getter.
*
* @return array
*/
public static function get_allowed_text_selectors() {
return self::$allowedtextselectors;
}
/**
* Moodle selectors attribute accessor.
*
* @return array
*/
protected static function get_moodle_selectors() {
return self::$moodleselectors;
}
}