Chris@14: php-webdriver -- A very thin wrapper of WebDriver Chris@14: ================================================= Chris@14: Chris@14: ## DESCRIPTION Chris@14: Chris@14: This client aims to be as thin as possible, abusing the dynamic nature of PHP to allow almost all API calls to be a direct transformation of what is defined in the WebDriver protocol itself. Chris@14: Chris@14: Most clients require you to first read the protocol to see what's possible, then study the client itself to see how to call it. This hopes to eliminate the latter step, and invites you to rely almost exclusively on https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol Chris@14: Chris@14: Each command is just the name of a function call, and each additional path is just another chained function call. The function parameter is then either an array() if the command takes JSON parameters, or an individual primitive if it takes a URL parameter. Chris@14: Chris@14: The function's return value is exactly what is returned from the server as part of the protocol definition. If an error is returned, the function will throw the appropriate WebDriverException instance. Chris@14: Chris@14: ## GETTING STARTED Chris@14: Chris@14: * All you need as the server for this client is the selenium-server-standalone-#.jar file provided here: http://www.seleniumhq.org/download/ Chris@14: Chris@14: * Download and run that file, replacing # with the current server version. Chris@14: Chris@14: java -jar selenium-server-standalone-#.jar Chris@14: Chris@14: * Then when you create a session, be sure to pass the url to where your server is running. Chris@14: Chris@14: // This would be the url of the host running the server-standalone.jar Chris@14: $wd_host = 'http://localhost:4444/wd/hub'; // this is the default Chris@14: $web_driver = new WebDriver($wd_host); Chris@14: Chris@14: // First param to session() is the 'browserName' (default = 'firefox') Chris@14: // Second param is a JSON object of additional 'desiredCapabilities' Chris@14: Chris@14: // POST /session Chris@14: $session = $web_driver->session('firefox'); Chris@14: Chris@14: * See also [wiki page for launching different browsers](https://github.com/facebook/php-webdriver/wiki/Launching-Browsers). Chris@14: Chris@14: ## SIMPLE EXAMPLES Chris@14: Chris@14: ### Note that all of these match the Protocol exactly Chris@14: * Move to a specific spot on the screen Chris@14: Chris@14: // POST /session/:sessionId/moveto Chris@14: $session->moveto(array('xoffset' => 3, 'yoffset' => 300)); Chris@14: Chris@14: * Get the current url Chris@14: Chris@14: // GET /session/:sessionId/url Chris@14: $session->url(); Chris@14: Chris@14: * Change focus to another frame Chris@14: Chris@14: // POST /session/:sessionId/frame Chris@14: $session->frame(array('id' => 'some_frame_id')); Chris@14: Chris@14: * Get a list of window handles for all open windows Chris@14: Chris@14: // GET /session/:sessionId/window_handles Chris@14: $session->window_handles(); Chris@14: Chris@14: * Accept the currently displayed alert dialog Chris@14: Chris@14: // POST /session/:sessionId/accept_alert Chris@14: $session->accept_alert(); Chris@14: Chris@14: * Change asynchronous script timeout Chris@14: Chris@14: // POST /session/:sessionId/timeouts/async_script Chris@14: $session->timeouts()->async_script(array('ms' => 2000)); Chris@14: Chris@14: * Doubleclick an element on a touch screen Chris@14: Chris@14: // POST session/:sessionId/touch/doubleclick Chris@14: $session->touch()->doubleclick(array('element' => $element->getID()) Chris@14: Chris@14: * Check if two elements are equal Chris@14: Chris@14: // GET /session/:sessionId/element/:id/equals/:other Chris@14: $element->equals($other_element->getID())) Chris@14: Chris@14: * Get value of a css property on element Chris@14: Chris@14: // GET /session/:sessionId/element/:id/css/:propertyName Chris@14: $element->css($property_name) Chris@14: Chris@14: ## 'GET', 'POST', or 'DELETE' to the same command examples Chris@14: Chris@14: ### When you can do multiple http methods for the same command, call the command directly for the 'GET', and prepend the http method for the 'POST' or 'DELETE'. Chris@14: Chris@14: * Set landscape orientation with 'POST' Chris@14: Chris@14: // POST /session/:sessionId/orientation Chris@14: $session->postOrientation(array('orientation' => 'LANDSCAPE')); Chris@14: Chris@14: * Get landscape orientation with normal 'GET' Chris@14: Chris@14: // GET /session/:sessionId/orientation Chris@14: $session->orientation(); Chris@14: Chris@14: * Set size of window that has $window_handle with 'POST' Chris@14: Chris@14: // If excluded, $window_handle defaults to 'current' Chris@14: // POST /session/:sessionId/window/:windowHandle/size Chris@14: $session Chris@14: ->window($window_handle) Chris@14: ->postSize(array('width' => 10, 'height' => 10)); Chris@14: Chris@14: * Get current window size with 'GET' Chris@14: Chris@14: // GET /session/:sessionId/window/:windowHandle/size Chris@14: $session->window()->size(); Chris@14: Chris@14: * Send keystrokes to an element with 'POST' Chris@14: Chris@14: // POST /session/:sessionId/element/:id/value Chris@14: // getValue() is deprecated; use postValue($json) or value($json) Chris@14: $element->postValue(array("value" => str_split('some text to send to element'))); Chris@14: Chris@14: ## Some unavoidable exceptions to direct protocol translation. Chris@14: Chris@14: * Opening pages Chris@14: Chris@14: // POST /session/:sessionId/url Chris@14: $session->open('http://www.facebook.com'); Chris@14: Chris@14: * Dealing with the session Chris@14: Chris@14: // DELETE /session/:sessionId Chris@14: $session->close(); Chris@14: Chris@14: // GET /session/:sessionId Chris@14: $session->capabilities(); Chris@14: Chris@14: * To find elements Chris@14: Chris@14: // POST /session/:sessionId/element Chris@14: $element = $session->element($using, $value); Chris@14: Chris@14: // POST /session/:sessionId/elements Chris@14: $session->elements($using, $value); Chris@14: Chris@14: // POST /session/:sessionId/element/:id/element Chris@14: $element->element($using, $value); Chris@14: Chris@14: // POST /session/:sessionId/element/:id/elements Chris@14: $element->elements($using, $value); Chris@14: Chris@14: * To get the active element Chris@14: Chris@14: // POST /session/:sessionId/element/active Chris@14: $session->activeElement(); Chris@14: Chris@14: * To manipulate cookies Chris@14: Chris@14: // GET /session/:sessionId/cookie Chris@14: $session->getAllCookies(); Chris@14: Chris@14: // POST /session/:sessionId/cookie Chris@14: $session->setCookie($cookie_json); Chris@14: Chris@14: // DELETE /session/:sessionId/cookie Chris@14: $session->deleteAllCookies() Chris@14: Chris@14: // DELETE /session/:sessionId/cookie/:name Chris@14: $session->deleteCookie($name) Chris@14: Chris@14: * To manipulate windows Chris@14: Chris@14: // POST /session/:sessionId/window Chris@14: $session->focusWindow($window_handle); Chris@14: Chris@14: // DELETE /session/:sessionId/window Chris@14: $session->deleteWindow(); Chris@14: Chris@14: ## More esoteric examples Chris@14: Chris@14: * To set curl options (e.g., timeout and proxy settings) Chris@14: Chris@14: ``` Chris@14: use WebDriver\Service\CurlService; Chris@14: use WebDriver\ServiceFactory; Chris@14: Chris@14: class MyCurlService extends CurlService Chris@14: { Chris@14: const PROXY = 'http://proxyHost:8080'; Chris@14: const AUTH = 'proxyUser:proxyPassword'; Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function execute($requestMethod, $url, $parameters = null, $extraOptions = null) Chris@14: { Chris@14: $extraOptions = array_replace( Chris@14: $extraOptions, Chris@14: array( Chris@14: CURLOPT_CONNECTTIMEOUT => 30, Chris@14: CURLOPT_TIMEOUT => 300, Chris@14: CURLOPT_PROXY => self::PROXY, Chris@14: CURLOPT_PROXYUSERPWD => self::AUTH, Chris@14: ) Chris@14: ); Chris@14: Chris@14: return parent::execute($requestMethod, $url, $parameters, $extraOptions); Chris@14: } Chris@14: } Chris@14: Chris@14: ServiceFactory::setServiceClass('service.curl', 'MyCurlService'); Chris@14: ``` Chris@14: Chris@14: