Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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  /**
  27   * Moodle selectors manager.
  28   *
  29   * @package    core
  30   * @category   test
  31   * @copyright  2013 David MonllaĆ³
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class behat_partial_named_selector extends \Behat\Mink\Selector\PartialNamedSelector {
  35  
  36      // Use the named selector trait.
  37      use behat_named_selector;
  38  
  39      /**
  40       * Creates selector instance.
  41       */
  42      public function __construct() {
  43          foreach (self::$customselectors as $alias => $selectors) {
  44              $this->registerNamedXpath($alias, implode(' | ', $selectors));
  45          }
  46  
  47          foreach (static::$moodleselectors as $name => $xpath) {
  48              $this->registerNamedXpath($name, $xpath);
  49          }
  50  
  51          foreach (self::$customreplacements as $from => $tos) {
  52              $this->registerReplacement($from, implode(' or ', $tos));
  53          }
  54  
  55          $this->registerReplacement('%iconMatch%', "(contains(concat(' ', @class, ' '), ' icon ') or self::img)");
  56          $this->registerReplacement('%imgAltMatch%', './/*[%iconMatch% and (%altMatch% or %titleMatch%)]');
  57          parent::__construct();
  58      }
  59  
  60      /**
  61       * @var array Allowed types when using text selectors arguments.
  62       */
  63      protected static $allowedtextselectors = array(
  64          'activity' => 'activity',
  65          'block' => 'block',
  66          'css_element' => 'css_element',
  67          'dialogue' => 'dialogue',
  68          'dropdown_item' => 'dropdown_item',
  69          'fieldset' => 'fieldset',
  70          'icon' => 'icon',
  71          'list_item' => 'list_item',
  72          'question' => 'question',
  73          'region' => 'region',
  74          'section' => 'section',
  75          'table' => 'table',
  76          'table_row' => 'table_row',
  77          'xpath_element' => 'xpath_element',
  78          'form_row' => 'form_row',
  79          'group_message_header' => 'group_message_header',
  80          'group_message' => 'group_message',
  81          'autocomplete' => 'autocomplete',
  82          'iframe' => 'iframe',
  83      );
  84  
  85      /**
  86       * @var array Allowed types when using selector arguments.
  87       */
  88      protected static $allowedselectors = array(
  89          'activity' => 'activity',
  90          'actionmenu' => 'actionmenu',
  91          'badge' => 'badge',
  92          'block' => 'block',
  93          'button' => 'button',
  94          'checkbox' => 'checkbox',
  95          'combobox' => 'combobox',
  96          'css_element' => 'css_element',
  97          'dialogue' => 'dialogue',
  98          'dropdown' => 'dropdown',
  99          'dropdown_item' => 'dropdown_item',
 100          'field' => 'field',
 101          'fieldset' => 'fieldset',
 102          'file' => 'file',
 103          'filemanager' => 'filemanager',
 104          'group_message' => 'group_message',
 105          'group_message_conversation' => 'group_message_conversation',
 106          'group_message_header' => 'group_message_header',
 107          'group_message_member' => 'group_message_member',
 108          'group_message_tab' => 'group_message_tab',
 109          'group_message_list_area' => 'group_message_list_area',
 110          'group_message_message_content' => 'group_message_message_content',
 111          'icon_container' => 'icon_container',
 112          'icon' => 'icon',
 113          'link' => 'link',
 114          'link_or_button' => 'link_or_button',
 115          'list_item' => 'list_item',
 116          'menuitem' => 'menuitem',
 117          'optgroup' => 'optgroup',
 118          'option' => 'option',
 119          'option_role' => 'option_role',
 120          'question' => 'question',
 121          'radio' => 'radio',
 122          'region' => 'region',
 123          'section' => 'section',
 124          'select' => 'select',
 125          'table' => 'table',
 126          'table_row' => 'table_row',
 127          'text' => 'text',
 128          'xpath_element' => 'xpath_element',
 129          'form_row' => 'form_row',
 130          'autocomplete_selection' => 'autocomplete_selection',
 131          'autocomplete_suggestions' => 'autocomplete_suggestions',
 132          'autocomplete' => 'autocomplete',
 133          'iframe' => 'iframe',
 134      );
 135  
 136      /**
 137       * Behat by default comes with XPath, CSS and named selectors,
 138       * named selectors are a mapping between names (like button) and
 139       * xpaths that represents that names and includes a placeholder that
 140       * will be replaced by the locator. These are Moodle's own xpaths.
 141       *
 142       * @var array XPaths for moodle elements.
 143       */
 144      protected static $moodleselectors = array(
 145          'activity' => <<<XPATH
 146  .//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][descendant::*[contains(normalize-space(.), %locator%)]]
 147  XPATH
 148          , 'actionmenu' => <<<XPATH
 149  .//*[
 150      contains(concat(' ', normalize-space(@class), ' '), ' action-menu ')
 151          and
 152      descendant::*[
 153          contains(concat(' ', normalize-space(@class), ' '), ' dropdown-toggle ')
 154              and
 155          (contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
 156      ]
 157  ]
 158  XPATH
 159          , 'badge' => <<<XPATH
 160  .//*[self::span or self::button][(contains(@class, 'badge')) and text()[contains(., %locator%)]]
 161  XPATH
 162          , 'block' => <<<XPATH
 163  .//*[@data-block][contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
 164       descendant::*[self::h2|self::h3|self::h4|self::h5][normalize-space(.) = %locator%]  or
 165       @aria-label = %locator%]
 166  XPATH
 167          , 'combobox' => <<<XPATH
 168  .//*[@role='combobox'][%titleMatch% or %ariaLabelMatch% or text()[contains(., %locator%)]]
 169  XPATH
 170          , 'dialogue' => <<<XPATH
 171  .//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
 172      not(contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hidden ')) and
 173      normalize-space(descendant::div[
 174          contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
 175          ]) = %locator%] |
 176  .//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
 177      normalize-space(descendant::div[@class='hd']) = %locator%]
 178          |
 179  .//div[@data-region='modal' and descendant::*[@data-region='title'] = %locator%]
 180          |
 181  .//div[
 182          contains(concat(' ', normalize-space(@class), ' '), ' modal-content ')
 183              and
 184          normalize-space(descendant::*[self::h4 or self::h5][contains(concat(' ', normalize-space(@class), ' '), ' modal-title ')]) = %locator%
 185      ]
 186          |
 187  .//div[
 188          contains(concat(' ', normalize-space(@class), ' '), ' modal ')
 189              and
 190          normalize-space(descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' modal-header ')]) = %locator%
 191      ]
 192  XPATH
 193      , 'dropdown' => <<<XPATH
 194          .//*[
 195              contains(concat(' ', normalize-space(@class), ' '), ' dropdown-menu ')
 196                  and
 197              @aria-labelledby =
 198                  (//*[
 199                          contains(concat(' ', normalize-space(@class), ' '), ' dropdown-toggle ')
 200                              and
 201                          (contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
 202                  ]/@id)
 203          ]
 204  XPATH
 205      , 'dropdown_item' => <<<XPATH
 206          .//*[
 207              @role = 'listitem'
 208                  and
 209              (contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
 210          ]
 211  XPATH
 212          , 'group_message' => <<<XPATH
 213          .//*[@data-conversation-id]//img[contains(@alt, %locator%)]/..
 214  XPATH
 215          , 'group_message_conversation' => <<<XPATH
 216              .//*[@data-region='message-drawer' and contains(., %locator%)]//div[@data-region='content-message-container']
 217  XPATH
 218      , 'group_message_header' => <<<XPATH
 219          .//*[@data-region='message-drawer']//div[@data-region='header-content' and contains(., %locator%)]
 220  XPATH
 221      , 'group_message_member' => <<<XPATH
 222          .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
 223          //div[@class='list-group' and not(contains(@class, 'hidden'))]//*[text()[contains(., %locator%)]] |
 224          .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
 225          //div[@data-region='empty-message-container' and not(contains(@class, 'hidden')) and contains(., %locator%)]
 226  XPATH
 227      , 'group_message_tab' => <<<XPATH
 228          .//*[@data-region='message-drawer']//button[@data-toggle='collapse' and contains(string(), %locator%)]
 229  XPATH
 230      , 'group_message_list_area' => <<<XPATH
 231          .//*[@data-region='message-drawer']//*[contains(@data-region, concat('view-overview-', %locator%))]
 232  XPATH
 233      , 'group_message_message_content' => <<<XPATH
 234          .//*[@data-region='message-drawer']//*[@data-region='message' and @data-message-id and contains(., %locator%)]
 235  XPATH
 236      , 'icon_container' => <<<XPATH
 237          .//span[contains(@data-region, concat(%locator%,'-icon-container'))]
 238  XPATH
 239          , 'icon' => <<<XPATH
 240  .//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
 241  XPATH
 242          , 'list_item' => <<<XPATH
 243  .//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
 244  XPATH
 245          , 'menuitem' => <<<XPATH
 246  .//*[@role='menuitem'][%titleMatch% or %ariaLabelMatch% or text()[contains(., %locator%)]]
 247  XPATH
 248      , 'option_role' => <<<XPATH
 249  .//*[@role='option'][%titleMatch% or %ariaLabelMatch% or text()[contains(., %locator%)]] |
 250  .//*[@role='option']/following-sibling::label[contains(., %locator%)]/preceding-sibling::input
 251  XPATH
 252          , 'question' => <<<XPATH
 253  .//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
 254      [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
 255  XPATH
 256          , 'region' => <<<XPATH
 257  .//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
 258  XPATH
 259          , 'section' => <<<XPATH
 260  .//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
 261      [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
 262      contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
 263  .//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
 264      [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
 265  XPATH
 266          , 'table' => <<<XPATH
 267  .//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
 268  XPATH
 269          , 'table_row' => <<<XPATH
 270  .//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
 271  XPATH
 272          , 'text' => <<<XPATH
 273  .//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
 274  XPATH
 275          , 'form_row' => <<<XPATH
 276  .//*[contains(concat(' ', @class, ' '), ' col-form-label ')]
 277      [normalize-space(.)= %locator%]
 278      /ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
 279  XPATH
 280          , 'autocomplete_selection' => <<<XPATH
 281  .//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-selection', ' '))]/span[@role='option'][contains(normalize-space(.), %locator%)]
 282  XPATH
 283          , 'autocomplete_suggestions' => <<<XPATH
 284  .//ul[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-suggestions', ' '))]/li[@role='option'][contains(normalize-space(.), %locator%)]
 285  XPATH
 286          , 'autocomplete' => <<<XPATH
 287  .//descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]/ancestor::*[@data-fieldtype = 'autocomplete']
 288  XPATH
 289          , 'iframe' => <<<XPATH
 290  .//iframe[(%idOrNameMatch% or (contains(concat(' ', normalize-space(@class), ' '), %locator% )))]
 291  XPATH
 292      );
 293  
 294      protected static $customselectors = [
 295          'field' => [
 296              'upstream' => <<<XPATH
 297  .//*
 298  [%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
 299  |
 300  .//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
 301  |
 302  .//*
 303  [%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
 304  |
 305  .//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
 306  XPATH
 307          ,
 308              'filemanager' => <<<XPATH
 309  .//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
 310      /descendant::input[@id = substring-before(//p[contains(normalize-space(string(.)), %locator%)]/@id, '_label')]
 311  XPATH
 312          ,
 313               'passwordunmask' => <<<XPATH
 314  .//*[@data-passwordunmask='wrapper']
 315      /descendant::input[@id = %locator% or @id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
 316  XPATH
 317          ,
 318               'inplaceeditable' => <<<XPATH
 319  .//descendant::span[@data-inplaceeditable][descendant::a[%titleMatch%]]
 320  XPATH
 321          ,
 322              'date_time' => <<<XPATH
 323  .//fieldset[(%idMatch% or ./legend[%exactTagTextMatch%]) and (@data-fieldtype='date' or @data-fieldtype='date_time')]
 324  XPATH
 325          ,
 326              'select_menu' => <<<XPATH
 327  //*[@role='combobox'][@aria-labelledby = //label[contains(normalize-space(string(.)), %locator%)]/@id]
 328  XPATH
 329          ,
 330          ],
 331      ];
 332  
 333      /**
 334       * Mink comes with a number of named replacements.
 335       * Sometimes we want to add our own.
 336       *
 337       * @var array XPaths for moodle elements.
 338       */
 339      protected static $customreplacements = [
 340          '%buttonMatch%' => [
 341              'upstream' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
 342              'aria' => '%ariaLabelMatch%',
 343          ],
 344          '%ariaLabelMatch%' => [
 345              'moodle' => 'contains(./@aria-label, %locator%)',
 346          ],
 347          '%exactTagTextMatch%' => [
 348              // This is based upon the upstream tagTextMatch but performs an exact match rather than a loose match using
 349              // contains().
 350              // If possible we should only use exact matches for any new form fields that we add.
 351              'moodle' => 'normalize-space(text())=%locator%',
 352          ],
 353      ];
 354  
 355      /** @var List of deprecated selectors */
 356      protected static $deprecatedselectors = [
 357          'group_message' => 'core_message > Message',
 358          'group_message_member' => 'core_message > Message member',
 359          'group_message_tab' => 'core_message > Message tab',
 360          'group_message_list_area' => 'core_message > Message list area',
 361          'group_message_message_content' => 'core_message > Message content',
 362      ];
 363  
 364      /**
 365       * Allowed selectors getter.
 366       *
 367       * @return array
 368       */
 369      public static function get_allowed_selectors() {
 370          return static::$allowedselectors;
 371      }
 372  
 373      /**
 374       * Allowed text selectors getter.
 375       *
 376       * @return array
 377       */
 378      public static function get_allowed_text_selectors() {
 379          return static::$allowedtextselectors;
 380      }
 381  }