2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Moodle-specific selectors.
22 * @copyright 2013 David Monllaó
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 * Moodle selectors manager.
33 * @copyright 2013 David Monllaó
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class behat_selectors {
39 * @var Allowed types when using text selectors arguments.
41 protected static $allowedtextselectors = array(
42 'dialogue' => 'dialogue',
45 'table_row' => 'table_row',
47 'fieldset' => 'fieldset',
48 'css_element' => 'css_element',
49 'xpath_element' => 'xpath_element'
53 * @var Allowed types when using selector arguments.
55 protected static $allowedselectors = array(
56 'dialogue' => 'dialogue',
59 'table_row' => 'table_row',
62 'link_or_button' => 'link_or_button',
64 'checkbox' => 'checkbox',
67 'filemanager' => 'filemanager',
68 'optgroup' => 'optgroup',
72 'fieldset' => 'fieldset',
74 'css_element' => 'css_element',
75 'xpath_element' => 'xpath_element'
79 * Behat by default comes with XPath, CSS and named selectors,
80 * named selectors are a mapping between names (like button) and
81 * xpaths that represents that names and includes a placeholder that
82 * will be replaced by the locator. These are Moodle's own xpaths.
84 * @var XPaths for moodle elements.
86 protected static $moodleselectors = array(
88 //*[contains(., %locator%)][count(./descendant::*[contains(., %locator%)]) = 0]
90 , 'dialogue' => <<<XPATH
91 //div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
92 normalize-space(descendant::div[
93 contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
95 //div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
96 normalize-space(descendant::div[@class='hd']) = %locator%]
99 //div[contains(concat(' ', normalize-space(@class), ' '), ' block ') and
100 (contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
101 descendant::h2[normalize-space(.) = %locator%] or
102 @aria-label = %locator%)]
104 , 'region' => <<<XPATH
105 //*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
107 , 'table_row' => <<<XPATH
108 .//tr[contains(normalize-space(.), %locator%)]
110 , 'filemanager' => <<<XPATH
111 //div[contains(concat(' ', normalize-space(@class), ' '), ' ffilemanager ')]
112 /descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
114 , 'table' => <<<XPATH
115 .//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
120 * Returns the behat selector and locator for a given moodle selector and locator
122 * @param string $selectortype The moodle selector type, which includes moodle selectors
123 * @param string $element The locator we look for in that kind of selector
124 * @param Session $session The Mink opened session
125 * @return array Contains the selector and the locator expected by Mink.
127 public static function get_behat_selector($selectortype, $element, Behat\Mink\Session $session) {
129 // CSS and XPath selectors locator is one single argument.
130 if ($selectortype == 'css_element' || $selectortype == 'xpath_element') {
131 $selector = str_replace('_element', '', $selectortype);
134 // Named selectors uses arrays as locators including the type of named selector.
135 $locator = array($selectortype, $session->getSelectorsHandler()->xpathLiteral($element));
139 return array($selector, $locator);
143 * Adds moodle selectors as behat named selectors.
145 * @param Session $session The mink session
148 public static function register_moodle_selectors(Behat\Mink\Session $session) {
150 foreach (self::get_moodle_selectors() as $name => $xpath) {
151 $session->getSelectorsHandler()->getSelector('named')->registerNamedXpath($name, $xpath);
156 * Allowed selectors getter.
160 public static function get_allowed_selectors() {
161 return self::$allowedselectors;
165 * Allowed text selectors getter.
169 public static function get_allowed_text_selectors() {
170 return self::$allowedtextselectors;
174 * Moodle selectors attribute accessor.
178 protected static function get_moodle_selectors() {
179 return self::$moodleselectors;