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 'optgroup' => 'optgroup',
71 'fieldset' => 'fieldset',
72 'css_element' => 'css_element',
73 'xpath_element' => 'xpath_element'
77 * Behat by default comes with XPath, CSS and named selectors,
78 * named selectors are a mapping between names (like button) and
79 * xpaths that represents that names and includes a placeholder that
80 * will be replaced by the locator. These are Moodle's own xpaths.
82 * @var XPaths for moodle elements.
84 protected static $moodleselectors = array(
85 'dialogue' => <<<XPATH
86 .//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ')]/descendant::h1[normalize-space(.) = %locator%]/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ')]
89 .//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 ')]
91 , 'region' => <<<XPATH
92 .//div[./@id = %locator%]
94 , 'table_row' => <<<XPATH
95 .//tr[contains(normalize-space(.), %locator%)]
100 * Returns the behat selector and locator for a given moodle selector and locator
102 * @param string $selectortype The moodle selector type, which includes moodle selectors
103 * @param string $element The locator we look for in that kind of selector
104 * @param Session $session The Mink opened session
105 * @return array Contains the selector and the locator expected by Mink.
107 public static function get_behat_selector($selectortype, $element, Behat\Mink\Session $session) {
109 // CSS and XPath selectors locator is one single argument.
110 if ($selectortype == 'css_element' || $selectortype == 'xpath_element') {
111 $selector = str_replace('_element', '', $selectortype);
114 // Named selectors uses arrays as locators including the type of named selector.
115 $locator = array($selectortype, $session->getSelectorsHandler()->xpathLiteral($element));
119 return array($selector, $locator);
123 * Adds moodle selectors as behat named selectors.
125 * @param Session $session The mink session
128 public static function register_moodle_selectors(Behat\Mink\Session $session) {
130 foreach (self::get_moodle_selectors() as $name => $xpath) {
131 $session->getSelectorsHandler()->getSelector('named')->registerNamedXpath($name, $xpath);
136 * Allowed selectors getter.
140 public static function get_allowed_selectors() {
141 return self::$allowedselectors;
145 * Allowed text selectors getter.
149 public static function get_allowed_text_selectors() {
150 return self::$allowedtextselectors;
154 * Moodle selectors attribute accessor.
158 protected static function get_moodle_selectors() {
159 return self::$moodleselectors;