Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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  namespace Moodle\BehatExtension\Driver;
  18  
  19  use Behat\MinkExtension\ServiceContainer\Driver\DriverFactory;
  20  use OAndreyev\Mink\Driver\WebDriverFactory as UpstreamFactory;
  21  use Symfony\Component\DependencyInjection\Definition;
  22  
  23  // phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
  24  
  25  /**
  26   * Driver factory for the Moodle WebDriver.
  27   *
  28   * @package    core
  29   * @copyright  2020 Andrew Nicols <andrew@nicols.co.uk>
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class WebDriverFactory extends UpstreamFactory implements DriverFactory {
  33      /**
  34       * Builds the service definition for the driver.
  35       *
  36       * @param array $config
  37       * @return Definition
  38       */
  39      public function buildDriver(array $config) {
  40          // Merge capabilities.
  41          $extracapabilities = $config['capabilities']['extra_capabilities'];
  42          unset($config['capabilities']['extra_capabilities']);
  43  
  44          // Normalise the Edge browser name.
  45          if ($config['browser'] === 'edge') {
  46              $config['browser'] = 'MicrosoftEdge';
  47          }
  48  
  49          // Ensure that the capabilites.browserName is set correctly.
  50          $config['capabilities']['browserName'] = $config['browser'];
  51  
  52          $capabilities = array_replace($extracapabilities, $config['capabilities']);
  53  
  54          // Incorrect top level capabilities lead to invalid Selenium browser selection.
  55          // See https://github.com/SeleniumHQ/selenium/issues/10410 for more information.
  56          // If any of these settings are mentioned then additional empty Capability options are created and a random
  57          // browser is chosen.
  58          $filteredcapabilities = [
  59              'tags',
  60              'ignoreZoomSetting',
  61              'marionette',
  62              'browser',
  63              'name',
  64          ];
  65  
  66          foreach ($filteredcapabilities as $capabilityname) {
  67              unset($capabilities[$capabilityname]);
  68          }
  69  
  70          // Build driver definition.
  71          return new Definition(WebDriver::class, [
  72              $config['browser'],
  73              $capabilities,
  74              $config['wd_host'],
  75          ]);
  76      }
  77  
  78      /**
  79       * Get the CapabilitiesNode.
  80       *
  81       * @return Node
  82       */
  83      protected function getCapabilitiesNode() {
  84          $node = parent::getCapabilitiesNode();
  85  
  86          // Specify chrome as the default browser.
  87          $node->find('browser')->defaultValue('chrome');
  88  
  89          return $node;
  90      }
  91  }