Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Session.php

class Session

Same name in this branch
  1. 11.1.x vendor/lullabot/php-webdriver/lib/WebDriver/Storage/Session.php \WebDriver\Storage\Session
  2. 11.1.x vendor/behat/mink/src/Session.php \Behat\Mink\Session
  3. 11.1.x vendor/symfony/http-foundation/Session/Session.php \Symfony\Component\HttpFoundation\Session\Session
  4. 11.1.x core/lib/Drupal/Core/StackMiddleware/Session.php \Drupal\Core\StackMiddleware\Session

WebDriver\Session class

@package WebDriver

@method void accept_alert() Accepts the currently displayed alert dialog. @method array deleteActions() Release actions. @method array postActions($actions) Perform actions. @method string getAlert_text() Gets the text of the currently displayed JavaScript alert(), confirm(), or prompt() dialog. @method void postAlert_text($jsonText) Sends keystrokes to a JavaScript prompt() dialog. @method void back() Navigates backward in the browser history, if possible. @method boolean getBrowser_connection() Is browser online? @method void postBrowser_connection($jsonState) Set browser online. @method void buttondown() Click and hold the left mouse button (at the coordinates set by the last moveto command). @method void buttonup() Releases the mouse button previously held (where the mouse is currently at). @method void click($jsonButton) Click any mouse button (at the coordinates set by the last moveto command). @method array getCookie() Retrieve all cookies visible to the current page. @method array postCookie($jsonCookie) Set a cookie. @method void dismiss_alert() Dismisses the currently displayed alert dialog. @method void doubleclick() Double-clicks at the current mouse coordinates (set by moveto). @method array execute_sql($jsonQuery) Execute SQL. @method array execute_async($jsonScript) Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. @method void forward() Navigates forward in the browser history, if possible. @method void keys($jsonKeys) Send a sequence of key strokes to the active element. @method array getLocation() Get the current geo location. @method void postLocation($jsonCoordinates) Set the current geo location. @method void moveto($jsonElementCoordinates) Move the mouse by an offset of the specified element. @method string getOrientation() Get the current browser orientation. @method void postOrientation($jsonOrientation) Set the current browser orientation. @method array print() Print page. @method void refresh() Refresh the current page. @method string screenshot() Take a screenshot of the current page. @method string source() Get the current page source. @method string title() Get the current page title. @method string url() Retrieve the URL of the current page. @method void postUrl($jsonUrl) Navigate to a new URL. @method string window_handle() Retrieve the current window handle. @method array window_handles() Retrieve the list of all window handles available to the session.

Hierarchy

  • class \WebDriver\AbstractWebDriver
    • class \WebDriver\Container extends \WebDriver\AbstractWebDriver
      • class \WebDriver\Session extends \WebDriver\Container

Expanded class hierarchy of Session

1 file declares its use of Session
Selenium2Driver.php in vendor/lullabot/mink-selenium2-driver/src/Selenium2Driver.php
24 string references to 'Session'
authorize.php in core/authorize.php
Administrative script for running authorized file operations.
default.services.yml in sites/default/default.services.yml
sites/default/default.services.yml
default.services.yml in core/assets/scaffold/files/default.services.yml
core/assets/scaffold/files/default.services.yml
DrupalKernel::initializeContainer in core/lib/Drupal/Core/DrupalKernel.php
Initializes the service container.
DrupalKernel::resetContainer in core/lib/Drupal/Core/DrupalKernel.php
Force a container reset.

... See full list

File

vendor/lullabot/php-webdriver/lib/WebDriver/Session.php, line 53

Namespace

WebDriver
View source
class Session extends Container {
    
    /**
     * @var array
     */
    private $capabilities = null;
    
    /**
     * @var boolean
     */
    private $w3c = null;
    
    /**
     * {@inheritdoc}
     */
    protected function methods() {
        return array(
            'actions' => array(
                'POST',
                'DELETE',
            ),
            'back' => array(
                'POST',
            ),
            'cookie' => array(
                'GET',
                'POST',
            ),
            // for DELETE, use deleteAllCookies()
'forward' => array(
                'POST',
            ),
            'print' => array(
                'POST',
            ),
            'refresh' => array(
                'POST',
            ),
            'screenshot' => array(
                'GET',
            ),
            'source' => array(
                'GET',
            ),
            'title' => array(
                'GET',
            ),
            'url' => array(
                'GET',
                'POST',
            ),
            // alternate for POST, use open($url)
            // specific to Java SeleniumServer
'file' => array(
                'POST',
            ),
            // Legacy JSON Wire Protocol
'accept_alert' => array(
                'POST',
            ),
            'alert_text' => array(
                'GET',
                'POST',
            ),
            'browser_connection' => array(
                'GET',
                'POST',
            ),
            'buttondown' => 'POST',
            'buttonup' => array(
                'POST',
            ),
            'click' => array(
                'POST',
            ),
            'dismiss_alert' => array(
                'POST',
            ),
            'doubleclick' => array(
                'POST',
            ),
            'execute_sql' => array(
                'POST',
            ),
            'keys' => array(
                'POST',
            ),
            'location' => array(
                'GET',
                'POST',
            ),
            'moveto' => array(
                'POST',
            ),
            'orientation' => array(
                'GET',
                'POST',
            ),
            'window_handle' => array(
                'GET',
            ),
            // see also getWindowHandle()
'window_handles' => array(
                'GET',
            ),
        );
    }
    
    /**
     * {@inheritdoc}
     */
    protected function obsoleteMethods() {
        return array(
            'alert' => array(
                'GET',
            ),
            'modifier' => array(
                'POST',
            ),
            'speed' => array(
                'GET',
                'POST',
            ),
            'visible' => array(
                'GET',
                'POST',
            ),
        );
    }
    
    /**
     * Constructor
     *
     * @param string     $url URL to Selenium server
     * @param array|null $capabilities
     */
    public function __construct($url, $capabilities) {
        parent::__construct($url);
        $this->capabilities = $capabilities;
        $this->w3c = !!$capabilities;
    }
    
    /**
     * Is W3C webdriver?
     *
     * @return boolean
     */
    public function isW3C() {
        return $this->w3c;
    }
    
    /**
     * Open URL: /session/:sessionId/url (POST)
     * An alternative to $session->url($url);
     *
     * @param string $url
     *
     * @return \WebDriver\Session
     */
    public function open($url) {
        $this->curl('POST', '/url', array(
            'url' => $url,
        ));
        return $this;
    }
    
    /**
     * Get browser capabilities: /session/:sessionId (GET)
     *
     * @return mixed
     */
    public function capabilities() {
        if (!isset($this->capabilities)) {
            $result = $this->curl('GET', '');
            $this->capabilities = $result['value'];
        }
        return $this->capabilities;
    }
    
    /**
     * Close session: /session/:sessionId (DELETE)
     *
     * @return mixed
     */
    public function close() {
        $result = $this->curl('DELETE', '');
        return $result['value'];
    }
    // There's a limit to our ability to exploit the dynamic nature of PHP when it
    // comes to the cookie methods because GET and DELETE request methods are indistinguishable
    // from each other: neither takes parameters.
    
    /**
     * Get all cookies: /session/:sessionId/cookie (GET)
     * Alternative to: $session->cookie();
     *
     * Note: get cookie by name not implemented in API
     *
     * @return mixed
     */
    public function getAllCookies() {
        $result = $this->curl('GET', '/cookie');
        return $result['value'];
    }
    
    /**
     * Set cookie: /session/:sessionId/cookie (POST)
     * Alternative to: $session->cookie($cookie_json);
     *
     * @param array $cookieJson
     *
     * @return \WebDriver\Session
     */
    public function setCookie($cookieJson) {
        $this->curl('POST', '/cookie', array(
            'cookie' => $cookieJson,
        ));
        return $this;
    }
    
    /**
     * Delete all cookies: /session/:sessionId/cookie (DELETE)
     *
     * @return \WebDriver\Session
     */
    public function deleteAllCookies() {
        $this->curl('DELETE', '/cookie');
        return $this;
    }
    
    /**
     * Delete a cookie: /session/:sessionId/cookie/:name (DELETE)
     *
     * @param string $cookieName
     *
     * @return \WebDriver\Session
     */
    public function deleteCookie($cookieName) {
        $this->curl('DELETE', '/cookie/' . $cookieName);
        return $this;
    }
    
    /**
     * Get window handles : /session/:sessionId/window_handles (GET)
     *                    : /session/:sessionId/window/handles (GET)
     * - $session->getWindowHandles()
     *
     * @return mixed
     */
    public function getWindowHandles() {
        $result = $this->curl('GET', $this->w3c ? '/window/handles' : '/window_handles');
        return $result['value'];
    }
    
    /**
     * Get window handle: /session/:sessionId/window (GET)
     *                  : /session/:sessionId/window_handle (GET)
     * - $session->getWindowHandle()
     *
     * An alternative to $session->window()->getHandle()
     *
     * @return mixed
     */
    public function getWindowHandle() {
        $result = $this->curl('GET', $this->w3c ? '/window' : '/window_handle');
        return $result['value'];
    }
    
    /**
     * New window: /session/:sessionId/window/new (POST)
     * - $session->newWindow($type)
     *
     * @internal "new" is a reserved keyword in PHP, so $session->window()->new() isn't possible
     *
     * @return \WebDriver\Window
     */
    public function newWindow($type) {
        $arg = func_get_arg(0);
        // json
        $result = $this->curl('POST', '/window/new', $arg);
        return $result['value'];
    }
    
    /**
     * window method chaining: /session/:sessionId/window (POST
     * - $session->window($jsonHandle) - set focus
     * - $session->window($handle)->method() - chaining
     * - $session->window()->method() - chaining
     *
     * @return \WebDriver\Session|\WebDriver\Window|\WebDriver\LegacyWindow
     */
    public function window() {
        $arg = null;
        // set window focus / switch to window
        if (func_num_args() === 1) {
            $arg = func_get_arg(0);
            // window handle or name attribute
            if (is_array($arg)) {
                $this->curl('POST', '/window', $arg);
                return $this;
            }
        }
        // chaining (with optional handle in $arg)
        return $this->w3c ? new Window($this->url . '/window', $arg) : new LegacyWindow($this->url . '/window', $arg);
    }
    
    /**
     * Close window: /session/:sessionId/window (DELETE)
     *
     * @return \WebDriver\Session
     */
    public function deleteWindow() {
        $this->curl('DELETE', '/window');
        return $this;
    }
    
    /**
     * Set focus to window: /session/:sessionId/window (POST)
     *
     * @param mixed $handle window handle (or legacy name) attribute
     *
     * @return \WebDriver\Session
     */
    public function focusWindow($handle) {
        $this->curl('POST', '/window', array(
            'handle' => $handle,
            'name' => $handle,
        ));
        return $this;
    }
    
    /**
     * frame methods: /session/:sessionId/frame (POST)
     * - $session->frame($json) - change focus to another frame on the page
     * - $session->frame()->method() - chaining
     *
     * @return \WebDriver\Session|\WebDriver\Frame
     */
    public function frame() {
        if (func_num_args() === 1) {
            $arg = func_get_arg(0);
            // json
            $this->curl('POST', '/frame', $arg);
            return $this;
        }
        // chaining
        return new Frame($this->url . '/frame');
    }
    
    /**
     * Get timeouts (W3C): /session/:sessionId/timeouts (GET)
     * - $session->getTimeouts()
     *
     * @return mixed
     */
    public function getTimeouts() {
        $result = $this->curl('GET', '/timeouts');
        return $result['value'];
    }
    
    /**
     * timeouts methods: /session/:sessionId/timeouts (POST)
     * - $session->timeouts($json) - set timeout for an operation
     * - $session->timeouts()->method() - chaining
     *
     * @return \WebDriver\Session|\WebDriver\Timeouts
     */
    public function timeouts() {
        // set timeouts
        if (func_num_args() === 1) {
            $arg = func_get_arg(0);
            // json
            $this->curl('POST', '/timeouts', $arg);
            return $this;
        }
        if (func_num_args() === 2) {
            $type = func_get_arg(0);
            // 'script', 'implicit', or 'pageLoad' (legacy: 'pageLoad')
            $timeout = func_get_arg(1);
            // timeout in milliseconds
            $arg = $this->w3c ? array(
                $type => $timeout,
            ) : array(
                'type' => $type,
                'ms' => $timeout,
            );
            $this->curl('POST', '/timeouts', $arg);
            return $this;
        }
        // chaining
        return new Timeouts($this->url . '/timeouts');
    }
    
    /**
     * ime method chaining, e.g.,
     * - $session->ime()->method()
     *
     * @return \WebDriver\Ime
     */
    public function ime() {
        return new Ime($this->url . '/ime');
    }
    
    /**
     * Get active element (i.e., has focus): /session/:sessionId/element/active (POST)
     * - $session->activeElement()
     *
     * @return mixed
     */
    public function activeElement() {
        $result = $this->curl('POST', '/element/active');
        return $this->makeElement($result['value']);
    }
    
    /**
     * touch method chaining, e.g.,
     * - $session->touch()->method()
     *
     * @return \WebDriver\Touch
     *
     */
    public function touch() {
        return new Touch($this->url . '/touch');
    }
    
    /**
     * local_storage method chaining, e.g.,
     * - $session->local_storage()->method()
     *
     * @return \WebDriver\Storage\Local
     */
    public function localStorage() {
        return new Storage\Local($this->url . '/local_storage');
    }
    
    /**
     * session_storage method chaining, e.g.,
     * - $session->session_storage()->method()
     *
     * @return \WebDriver\Storage\Session
     */
    public function sessionStorage() {
        return new Storage\Session($this->url . '/session_storage');
    }
    
    /**
     * application cache chaining, e.g.,
     * - $session->application_cache()->method() - chaining
     *
     * @return \WebDriver\ApplicationCache
     */
    public function applicationCache() {
        return new ApplicationCache($this->url . '/application_cache');
    }
    
    /**
     * log methods: /session/:sessionId/log (POST)
     * - $session->log($type) - get log for given log type
     * - $session->log()->method() - chaining
     *
     * @return mixed
     */
    public function log() {
        // get log for given log type
        if (func_num_args() === 1) {
            $arg = func_get_arg(0);
            if (is_string($arg)) {
                $arg = array(
                    'type' => $arg,
                );
            }
            $result = $this->curl('POST', '/log', $arg);
            return $result['value'];
        }
        // chaining
        return new Log($this->url . '/log');
    }
    
    /**
     * alert method chaining, e.g.,
     * - $session->alert()->method() - chaining
     *
     * @return mixed
     */
    public function alert() {
        return new Alert($this->url . '/alert');
    }
    
    /**
     * script execution method chaining, e.g.,
     * - $session->execute($jsonScript) - fallback for legacy JSON Wire Protocol
     * - $session->execute()->method() - chaining
     *
     * @return mixed
     */
    public function execute() {
        // execute script
        if (func_num_args() > 0) {
            $execute = $this->w3c ? new Execute($this->url . '/execute') : new LegacyExecute($this->url);
            $result = $execute->sync(func_get_arg(0));
            return $result;
        }
        // W3C method chaining
        return new Execute($this->url . '/execute');
    }
    
    /**
     * async script execution
     * - $session->execute_async($jsonScript)
     *
     * @return mixed
     */
    public function executeAsync() {
        $execute = $this->w3c ? new Execute($this->url . '/execute') : new LegacyExecute($this->url);
        $result = $execute->async(func_get_arg(0));
        return $result;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getIdentifierPath($identifier) {
        return sprintf('%s/element/%s', $this->url, $identifier);
    }
    
    /**
     * Upload a file: /session/:sessionId/se/file (POST)
     *              : /session/:sessionId/file (POST)
     *
     * @param array $arguments
     *   An array with a single key/value. The key should be 'file' and the
     *   value should be a string containing base64 encoded contents of a file.
     *
     * @return mixed
     */
    public function file(array $arguments) {
        // Since Selenium 4.17 the file URL has been prefixed with /se because
        // it is not a W3C command. See
        // https://github.com/w3c/webdriver/issues/1355 for discussions about
        // the W3C spec and file uploads.
        if ($this->isW3C()) {
            try {
                $result = $this->curl('POST', '/se/file', $arguments);
            } catch (Exception $e) {
            }
        }
        // Fallback to pre Selenium 4.17 behaviour and non W3C behaviour.
        if (!isset($result)) {
            $result = $this->curl('POST', '/file', $arguments);
        }
        return $result['value'];
    }
    
    /**
     * {@inheritdoc}
     */
    public function __call($name, $arguments) {
        $map = [
            'application_cache' => 'applicationCache',
            'execute_async' => 'executeAsync',
            'local_storage' => 'localStorage',
            'session_storage' => 'sessionStorage',
        ];
        if (array_key_exists($name, $map)) {
            return call_user_func_array([
                $this,
                $map[$name],
            ], $arguments);
        }
        // fallback to executing WebDriver commands
        return parent::__call($name, $arguments);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractWebDriver::$curlService private property Curl service
AbstractWebDriver::$transientOptions private property Transient options
AbstractWebDriver::$url protected property URL
AbstractWebDriver::assertSerializable private function Sanity check
AbstractWebDriver::curl protected function Curl request to webdriver server.
AbstractWebDriver::getCurlService public function Get curl service
AbstractWebDriver::getRequestMethod private function Get default HTTP request method for a given WebDriver command
AbstractWebDriver::getTransientOptions public function
AbstractWebDriver::getURL public function Returns URL to Selenium server
AbstractWebDriver::offsetGet private function Extract value from result
AbstractWebDriver::setCurlService public function Set curl service
AbstractWebDriver::setTransientOptions public function Set transient options
AbstractWebDriver::__toString public function Magic method which returns URL to Selenium server
Container::$strategies private property
Container::element public function Find element: /session/:sessionId/element (POST)
Find child element: /session/:sessionId/element/:id/element (POST)
Search for element on page, starting from the document root.
Container::elements public function Find elements: /session/:sessionId/elements (POST)
Find child elements: /session/:sessionId/element/:id/elements (POST)
Search for multiple elements on page, starting from the document root.
Container::locate public function Return JSON parameter for element / elements command
Container::makeElement protected function Factory method for elements
Container::parseArgs private function Parse arguments allowing either separate $using and $value parameters, or
as an array containing the JSON parameters
Session::$capabilities private property
Session::$w3c private property
Session::activeElement public function Get active element (i.e., has focus): /session/:sessionId/element/active (POST)
Session::alert public function alert method chaining, e.g.,
Session::applicationCache public function application cache chaining, e.g.,
Session::capabilities public function Get browser capabilities: /session/:sessionId (GET)
Session::close public function Close session: /session/:sessionId (DELETE)
Session::deleteAllCookies public function Delete all cookies: /session/:sessionId/cookie (DELETE)
Session::deleteCookie public function Delete a cookie: /session/:sessionId/cookie/:name (DELETE)
Session::deleteWindow public function Close window: /session/:sessionId/window (DELETE)
Session::execute public function script execution method chaining, e.g.,
Session::executeAsync public function async script execution
Session::file public function Upload a file: /session/:sessionId/se/file (POST)
: /session/:sessionId/file (POST)
Session::focusWindow public function Set focus to window: /session/:sessionId/window (POST)
Session::frame public function frame methods: /session/:sessionId/frame (POST)
Session::getAllCookies public function Get all cookies: /session/:sessionId/cookie (GET)
Alternative to: $session->cookie();
Session::getIdentifierPath protected function Get wire protocol URL for an identifier Overrides Container::getIdentifierPath
Session::getTimeouts public function Get timeouts (W3C): /session/:sessionId/timeouts (GET)
Session::getWindowHandle public function Get window handle: /session/:sessionId/window (GET)
: /session/:sessionId/window_handle (GET)
Session::getWindowHandles public function Get window handles : /session/:sessionId/window_handles (GET)
: /session/:sessionId/window/handles (GET)
Session::ime public function ime method chaining, e.g.,
Session::isW3C public function Is W3C webdriver?
Session::localStorage public function local_storage method chaining, e.g.,
Session::log public function log methods: /session/:sessionId/log (POST)
Session::methods protected function Return array of supported method names and corresponding HTTP request methods Overrides AbstractWebDriver::methods
Session::newWindow public function New window: /session/:sessionId/window/new (POST)
Session::obsoleteMethods protected function Return array of obsolete method names and corresponding HTTP request methods Overrides AbstractWebDriver::obsoleteMethods
Session::open public function Open URL: /session/:sessionId/url (POST)
An alternative to $session->url($url);
Session::sessionStorage public function session_storage method chaining, e.g.,
Session::setCookie public function Set cookie: /session/:sessionId/cookie (POST)
Alternative to: $session->cookie($cookie_json);
Session::timeouts public function timeouts methods: /session/:sessionId/timeouts (POST)
Session::touch public function touch method chaining, e.g.,
Session::window public function window method chaining: /session/:sessionId/window (POST
Session::__call public function Magic method that maps calls to class methods to execute WebDriver commands Overrides Container::__call
Session::__construct public function Constructor Overrides Container::__construct

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal