| 1 | efrain | 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 |   | 
        
           | 11 | efrain | 19 | use Behat\Mink\Exception\DriverException;
 | 
        
           | 1 | efrain | 20 | use OAndreyev\Mink\Driver\WebDriver as UpstreamDriver;
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 | // phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | /**
 | 
        
           |  |  | 25 |  * WebDriver Driver to allow extra selenium capabilities required by Moodle.
 | 
        
           |  |  | 26 |  *
 | 
        
           |  |  | 27 |  * @package core
 | 
        
           |  |  | 28 |  * @copyright 2016 onwards Rajesh Taneja
 | 
        
           |  |  | 29 |  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 30 |  */
 | 
        
           |  |  | 31 | class WebDriver extends UpstreamDriver {
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 |     /**
 | 
        
           |  |  | 35 |      * Dirty attribute to get the browser name; $browserName is private
 | 
        
           |  |  | 36 |      * @var string
 | 
        
           |  |  | 37 |      */
 | 
        
           |  |  | 38 |     protected static $browser;
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |     /**
 | 
        
           |  |  | 41 |      * Instantiates the driver.
 | 
        
           |  |  | 42 |      *
 | 
        
           |  |  | 43 |      * @param string    $browsername Browser name
 | 
        
           |  |  | 44 |      * @param array     $desiredcapabilities The desired capabilities
 | 
        
           |  |  | 45 |      * @param string    $wdhost The WebDriver host
 | 
        
           |  |  | 46 |      * @param array     $moodleparameters Moodle parameters including our non-behat-friendly selenium capabilities
 | 
        
           |  |  | 47 |      */
 | 
        
           |  |  | 48 |     public function __construct(
 | 
        
           |  |  | 49 |         $browsername = 'chrome',
 | 
        
           |  |  | 50 |         $desiredcapabilities = null,
 | 
        
           |  |  | 51 |         $wdhost = 'http://localhost:4444/wd/hub',
 | 
        
           |  |  | 52 |         $moodleparameters = []
 | 
        
           |  |  | 53 |     ) {
 | 
        
           |  |  | 54 |         parent::__construct($browsername, $desiredcapabilities, $wdhost);
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |         // This class is instantiated by the dependencies injection system so prior to all of beforeSuite subscribers
 | 
        
           |  |  | 57 |         // which will call getBrowser*().
 | 
        
           |  |  | 58 |         self::$browser = $browsername;
 | 
        
           |  |  | 59 |     }
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |     /**
 | 
        
           |  |  | 62 |      * Returns the browser being used.
 | 
        
           |  |  | 63 |      *
 | 
        
           |  |  | 64 |      * We need to know it:
 | 
        
           |  |  | 65 |      * - To show info about the run.
 | 
        
           |  |  | 66 |      * - In case there are differences between browsers in the steps.
 | 
        
           |  |  | 67 |      *
 | 
        
           |  |  | 68 |      * @return string
 | 
        
           |  |  | 69 |      */
 | 
        
           |  |  | 70 |     public static function getBrowserName() {
 | 
        
           |  |  | 71 |         return self::$browser;
 | 
        
           |  |  | 72 |     }
 | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 |     /**
 | 
        
           |  |  | 75 |      * Post key on specified xpath.
 | 
        
           |  |  | 76 |      *
 | 
        
           |  |  | 77 |      * @param string $key
 | 
        
           |  |  | 78 |      * @param string $xpath
 | 
        
           |  |  | 79 |      */
 | 
        
           |  |  | 80 |     public function post_key($key, $xpath) {
 | 
        
           |  |  | 81 |         throw new \Exception('No longer used - please use keyDown and keyUp');
 | 
        
           |  |  | 82 |     }
 | 
        
           | 11 | efrain | 83 |   | 
        
           |  |  | 84 |     #[\Override]
 | 
        
           |  |  | 85 |     public function stop(): void {
 | 
        
           |  |  | 86 |         try {
 | 
        
           |  |  | 87 |             parent::stop();
 | 
        
           |  |  | 88 |         } catch (DriverException $e) {
 | 
        
           |  |  | 89 |             error_log($e->getMessage());
 | 
        
           | 1441 | ariadna | 90 |             $rcp = new \ReflectionProperty(parent::class, 'webDriver');
 | 
        
           |  |  | 91 |             $rcp->setValue($this, null);
 | 
        
           | 11 | efrain | 92 |         }
 | 
        
           |  |  | 93 |     }
 | 
        
           | 1 | efrain | 94 | }
 |