1 php-webdriver -- A very thin wrapper of WebDriver
2 =================================================
6 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.
8 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
10 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.
12 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.
16 * All you need as the server for this client is the selenium-server-standalone-#.jar file provided here: http://www.seleniumhq.org/download/
18 * Download and run that file, replacing # with the current server version.
20 java -jar selenium-server-standalone-#.jar
22 * Then when you create a session, be sure to pass the url to where your server is running.
24 // This would be the url of the host running the server-standalone.jar
25 $wd_host = 'http://localhost:4444/wd/hub'; // this is the default
26 $web_driver = new WebDriver($wd_host);
28 // First param to session() is the 'browserName' (default = 'firefox')
29 // Second param is a JSON object of additional 'desiredCapabilities'
32 $session = $web_driver->session('firefox');
34 * See also [wiki page for launching different browsers](https://github.com/facebook/php-webdriver/wiki/Launching-Browsers).
38 ### Note that all of these match the Protocol exactly
39 * Move to a specific spot on the screen
41 // POST /session/:sessionId/moveto
42 $session->moveto(array('xoffset' => 3, 'yoffset' => 300));
46 // GET /session/:sessionId/url
49 * Change focus to another frame
51 // POST /session/:sessionId/frame
52 $session->frame(array('id' => 'some_frame_id'));
54 * Get a list of window handles for all open windows
56 // GET /session/:sessionId/window_handles
57 $session->window_handles();
59 * Accept the currently displayed alert dialog
61 // POST /session/:sessionId/accept_alert
62 $session->accept_alert();
64 * Change asynchronous script timeout
66 // POST /session/:sessionId/timeouts/async_script
67 $session->timeouts()->async_script(array('ms' => 2000));
69 * Doubleclick an element on a touch screen
71 // POST session/:sessionId/touch/doubleclick
72 $session->touch()->doubleclick(array('element' => $element->getID())
74 * Check if two elements are equal
76 // GET /session/:sessionId/element/:id/equals/:other
77 $element->equals($other_element->getID()))
79 * Get value of a css property on element
81 // GET /session/:sessionId/element/:id/css/:propertyName
82 $element->css($property_name)
84 ## 'GET', 'POST', or 'DELETE' to the same command examples
86 ### 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'.
88 * Set landscape orientation with 'POST'
90 // POST /session/:sessionId/orientation
91 $session->postOrientation(array('orientation' => 'LANDSCAPE'));
93 * Get landscape orientation with normal 'GET'
95 // GET /session/:sessionId/orientation
96 $session->orientation();
98 * Set size of window that has $window_handle with 'POST'
100 // If excluded, $window_handle defaults to 'current'
101 // POST /session/:sessionId/window/:windowHandle/size
103 ->window($window_handle)
104 ->postSize(array('width' => 10, 'height' => 10));
106 * Get current window size with 'GET'
108 // GET /session/:sessionId/window/:windowHandle/size
109 $session->window()->size();
111 * Send keystrokes to an element with 'POST'
113 // POST /session/:sessionId/element/:id/value
114 // getValue() is deprecated; use postValue($json) or value($json)
115 $element->postValue(array("value" => str_split('some text to send to element')));
117 ## Some unavoidable exceptions to direct protocol translation.
121 // POST /session/:sessionId/url
122 $session->open('http://www.facebook.com');
124 * Dealing with the session
126 // DELETE /session/:sessionId
129 // GET /session/:sessionId
130 $session->capabilities();
134 // POST /session/:sessionId/element
135 $element = $session->element($using, $value);
137 // POST /session/:sessionId/elements
138 $session->elements($using, $value);
140 // POST /session/:sessionId/element/:id/element
141 $element->element($using, $value);
143 // POST /session/:sessionId/element/:id/elements
144 $element->elements($using, $value);
146 * To get the active element
148 // POST /session/:sessionId/element/active
149 $session->activeElement();
151 * To manipulate cookies
153 // GET /session/:sessionId/cookie
154 $session->getAllCookies();
156 // POST /session/:sessionId/cookie
157 $session->setCookie($cookie_json);
159 // DELETE /session/:sessionId/cookie
160 $session->deleteAllCookies()
162 // DELETE /session/:sessionId/cookie/:name
163 $session->deleteCookie($name)
165 * To manipulate windows
167 // POST /session/:sessionId/window
168 $session->focusWindow($window_handle);
170 // DELETE /session/:sessionId/window
171 $session->deleteWindow();
173 ## More esoteric examples
175 * To set curl options (e.g., timeout and proxy settings)
178 use WebDriver\Service\CurlService;
179 use WebDriver\ServiceFactory;
181 class MyCurlService extends CurlService
183 const PROXY = 'http://proxyHost:8080';
184 const AUTH = 'proxyUser:proxyPassword';
189 public function execute($requestMethod, $url, $parameters = null, $extraOptions = null)
191 $extraOptions = array_replace(
194 CURLOPT_CONNECTTIMEOUT => 30,
195 CURLOPT_TIMEOUT => 300,
196 CURLOPT_PROXY => self::PROXY,
197 CURLOPT_PROXYUSERPWD => self::AUTH,
201 return parent::execute($requestMethod, $url, $parameters, $extraOptions);
205 ServiceFactory::setServiceClass('service.curl', 'MyCurlService');