[ Index ]

PHP Cross Reference of Unnamed Project

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          'activity' => 'activity',
  43          'block' => 'block',
  44          'css_element' => 'css_element',
  45          'dialogue' => 'dialogue',
  46          'fieldset' => 'fieldset',
  47          'list_item' => 'list_item',
  48          'question' => 'question',
  49          'region' => 'region',
  50          'section' => 'section',
  51          'table' => 'table',
  52          'table_row' => 'table_row',
  53          'xpath_element' => 'xpath_element',
  54      );
  55  
  56      /**
  57       * @var Allowed types when using selector arguments.
  58       */
  59      protected static $allowedselectors = array(
  60          'activity' => 'activity',
  61          'block' => 'block',
  62          'button' => 'button',
  63          'checkbox' => 'checkbox',
  64          'css_element' => 'css_element',
  65          'dialogue' => 'dialogue',
  66          'field' => 'field',
  67          'fieldset' => 'fieldset',
  68          'file' => 'file',
  69          'filemanager' => 'filemanager',
  70          'link' => 'link',
  71          'link_or_button' => 'link_or_button',
  72          'list_item' => 'list_item',
  73          'optgroup' => 'optgroup',
  74          'option' => 'option',
  75          'question' => 'question',
  76          'radio' => 'radio',
  77          'region' => 'region',
  78          'section' => 'section',
  79          'select' => 'select',
  80          'table' => 'table',
  81          'table_row' => 'table_row',
  82          'text' => 'text',
  83          'xpath_element' => 'xpath_element'
  84      );
  85  
  86      /**
  87       * Behat by default comes with XPath, CSS and named selectors,
  88       * named selectors are a mapping between names (like button) and
  89       * xpaths that represents that names and includes a placeholder that
  90       * will be replaced by the locator. These are Moodle's own xpaths.
  91       *
  92       * @var XPaths for moodle elements.
  93       */
  94      protected static $moodleselectors = array(
  95          'activity' => <<<XPATH
  96  .//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][normalize-space(.) = %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          , 'dialogue' => <<<XPATH
 105  .//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
 106      normalize-space(descendant::div[
 107          contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
 108          ]) = %locator%] |
 109  .//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
 110      normalize-space(descendant::div[@class='hd']) = %locator%]
 111  XPATH
 112          , 'filemanager' => <<<XPATH
 113  .//div[contains(concat(' ', normalize-space(@class), ' '), ' ffilemanager ')]
 114      /descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
 115  XPATH
 116          , 'list_item' => <<<XPATH
 117  .//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
 118  XPATH
 119          , 'question' => <<<XPATH
 120  .//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
 121      [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
 122  XPATH
 123          , 'region' => <<<XPATH
 124  .//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
 125  XPATH
 126          , 'section' => <<<XPATH
 127  .//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
 128      [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
 129      contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
 130  .//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
 131      [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
 132  XPATH
 133          , 'table' => <<<XPATH
 134  .//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
 135  XPATH
 136          , 'table_row' => <<<XPATH
 137  .//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
 138  XPATH
 139          , 'text' => <<<XPATH
 140  .//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
 141  XPATH
 142      );
 143  
 144      /**
 145       * Returns the behat selector and locator for a given moodle selector and locator
 146       *
 147       * @param string $selectortype The moodle selector type, which includes moodle selectors
 148       * @param string $element The locator we look for in that kind of selector
 149       * @param Session $session The Mink opened session
 150       * @return array Contains the selector and the locator expected by Mink.
 151       */
 152      public static function get_behat_selector($selectortype, $element, Behat\Mink\Session $session) {
 153  
 154          // CSS and XPath selectors locator is one single argument.
 155          if ($selectortype == 'css_element' || $selectortype == 'xpath_element') {
 156              $selector = str_replace('_element', '', $selectortype);
 157              $locator = $element;
 158          } else {
 159              // Named selectors uses arrays as locators including the type of named selector.
 160              $locator = array($selectortype, behat_context_helper::escape($element));
 161              $selector = 'named_partial';
 162          }
 163  
 164          return array($selector, $locator);
 165      }
 166  
 167      /**
 168       * Adds moodle selectors as behat named selectors.
 169       *
 170       * @param Session $session The mink session
 171       * @return void
 172       */
 173      public static function register_moodle_selectors(Behat\Mink\Session $session) {
 174  
 175          foreach (self::get_moodle_selectors() as $name => $xpath) {
 176              $session->getSelectorsHandler()->getSelector('named_partial')->registerNamedXpath($name, $xpath);
 177          }
 178      }
 179  
 180      /**
 181       * Allowed selectors getter.
 182       *
 183       * @return array
 184       */
 185      public static function get_allowed_selectors() {
 186          return self::$allowedselectors;
 187      }
 188  
 189      /**
 190       * Allowed text selectors getter.
 191       *
 192       * @return array
 193       */
 194      public static function get_allowed_text_selectors() {
 195          return self::$allowedtextselectors;
 196      }
 197  
 198      /**
 199       * Moodle selectors attribute accessor.
 200       *
 201       * @return array
 202       */
 203      protected static function get_moodle_selectors() {
 204          return self::$moodleselectors;
 205      }
 206  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1