[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/behat/classes/ -> behat_selectors.php (source)

   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  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Moodle selectors manager.
  30   *
  31   * @package    core
  32   * @category   test
  33   * @copyright  2013 David Monllaó
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class behat_selectors {
  37  
  38      /**
  39       * @var Allowed types when using text selectors arguments.
  40       */
  41      protected static $allowedtextselectors = array(
  42          'dialogue' => 'dialogue',
  43          'block' => 'block',
  44          'region' => 'region',
  45          'table_row' => 'table_row',
  46          'table' => 'table',
  47          'fieldset' => 'fieldset',
  48          'css_element' => 'css_element',
  49          'xpath_element' => 'xpath_element'
  50      );
  51  
  52      /**
  53       * @var Allowed types when using selector arguments.
  54       */
  55      protected static $allowedselectors = array(
  56          'dialogue' => 'dialogue',
  57          'block' => 'block',
  58          'region' => 'region',
  59          'table_row' => 'table_row',
  60          'link' => 'link',
  61          'button' => 'button',
  62          'link_or_button' => 'link_or_button',
  63          'select' => 'select',
  64          'checkbox' => 'checkbox',
  65          'radio' => 'radio',
  66          'file' => 'file',
  67          'filemanager' => 'filemanager',
  68          'optgroup' => 'optgroup',
  69          'option' => 'option',
  70          'table' => 'table',
  71          'field' => 'field',
  72          'fieldset' => 'fieldset',
  73          'text' => 'text',
  74          'css_element' => 'css_element',
  75          'xpath_element' => 'xpath_element'
  76      );
  77  
  78      /**
  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.
  83       *
  84       * @var XPaths for moodle elements.
  85       */
  86      protected static $moodleselectors = array(
  87          'text' => <<<XPATH
  88  //*[contains(., %locator%)][count(./descendant::*[contains(., %locator%)]) = 0]
  89  XPATH
  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 ')
  94          ]) = %locator%] |
  95  //div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
  96      normalize-space(descendant::div[@class='hd']) = %locator%]
  97  XPATH
  98          , 'block' => <<<XPATH
  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%)]
 103  XPATH
 104          , 'region' => <<<XPATH
 105  //*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
 106  XPATH
 107          , 'table_row' => <<<XPATH
 108  .//tr[contains(normalize-space(.), %locator%)]
 109  XPATH
 110          , 'filemanager' => <<<XPATH
 111  //div[contains(concat(' ', normalize-space(@class), ' '), ' ffilemanager ')]
 112      /descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
 113  XPATH
 114          , 'table' => <<<XPATH
 115  .//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
 116  XPATH
 117      );
 118  
 119      /**
 120       * Returns the behat selector and locator for a given moodle selector and locator
 121       *
 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.
 126       */
 127      public static function get_behat_selector($selectortype, $element, Behat\Mink\Session $session) {
 128  
 129          // CSS and XPath selectors locator is one single argument.
 130          if ($selectortype == 'css_element' || $selectortype == 'xpath_element') {
 131              $selector = str_replace('_element', '', $selectortype);
 132              $locator = $element;
 133          } else {
 134              // Named selectors uses arrays as locators including the type of named selector.
 135              $locator = array($selectortype, $session->getSelectorsHandler()->xpathLiteral($element));
 136              $selector = 'named';
 137          }
 138  
 139          return array($selector, $locator);
 140      }
 141  
 142      /**
 143       * Adds moodle selectors as behat named selectors.
 144       *
 145       * @param Session $session The mink session
 146       * @return void
 147       */
 148      public static function register_moodle_selectors(Behat\Mink\Session $session) {
 149  
 150          foreach (self::get_moodle_selectors() as $name => $xpath) {
 151              $session->getSelectorsHandler()->getSelector('named')->registerNamedXpath($name, $xpath);
 152          }
 153      }
 154  
 155      /**
 156       * Allowed selectors getter.
 157       *
 158       * @return array
 159       */
 160      public static function get_allowed_selectors() {
 161          return self::$allowedselectors;
 162      }
 163  
 164      /**
 165       * Allowed text selectors getter.
 166       *
 167       * @return array
 168       */
 169      public static function get_allowed_text_selectors() {
 170          return self::$allowedtextselectors;
 171      }
 172  
 173      /**
 174       * Moodle selectors attribute accessor.
 175       *
 176       * @return array
 177       */
 178      protected static function get_moodle_selectors() {
 179          return self::$moodleselectors;
 180      }
 181  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1