Advertisement
Guest User

webdriver

a guest
Dec 8th, 2011
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.23 KB | None | 0 0
  1. <?php
  2.  
  3. class WebDriver_Driver {
  4. protected $session_id;
  5. protected $server_url;
  6. protected $browser;
  7.  
  8. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#Response_Status_Codes
  9. private static $status_codes = array(
  10. 0 => array("Success", "The command executed successfully."),
  11. 7 => array("NoSuchElement", "An element could not be located on the page using the given search parameters."),
  12. 8 => array("NoSuchFrame", "A request to switch to a frame could not be satisfied because the frame could not be found."),
  13. 9 => array("UnknownCommand", "The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource."),
  14. 10 => array("StaleElementReference", "An element command failed because the referenced element is no longer attached to the DOM."),
  15. 11 => array("ElementNotVisible", "An element command could not be completed because the element is not visible on the page."),
  16. 12 => array("InvalidElementState", "An element command could not be completed because the element is in an invalid state (e.g. attempting to click a disabled element)."),
  17. 13 => array("UnknownError", "An unknown server-side error occurred while processing the command."),
  18. 15 => array("ElementIsNotSelectable", "An attempt was made to select an element that cannot be selected."),
  19. 17 => array("JavaScriptError", "An error occurred while executing user supplied JavaScript."),
  20. 19 => array("XPathLookupError", "An error occurred while searching for an element by XPath."),
  21. 21 => array("Timeout", "An operation did not complete before its timeout expired."),
  22. 23 => array("NoSuchWindow", "A request to switch to a different window could not be satisfied because the window could not be found."),
  23. 24 => array("InvalidCookieDomain", "An illegal attempt was made to set a cookie under a different domain than the current page."),
  24. 25 => array("UnableToSetCookie", "A request to set a cookie's value could not be satisfied."),
  25. 26 => array("UnexpectedAlertOpen", "A modal dialog was open, blocking this operation"),
  26. 27 => array("NoAlertOpenError", "An attempt was made to operate on a modal dialog when one was not open."),
  27. 28 => array("ScriptTimeout", "A script did not complete before its timeout expired."),
  28. 29 => array("InvalidElementCoordinates", "The coordinates provided to an interactions operation are invalid."),
  29. 30 => array("IMENotAvailable", "IME was not available."),
  30. 31 => array("IMEEngineActivationFailed", "An IME engine could not be started."),
  31. 32 => array("InvalidSelector", "Argument was an invalid selector (e.g. XPath/CSS)."),
  32. );
  33.  
  34. protected function __construct($server_url, $capabilities) {
  35. $this->server_url = $server_url;
  36. $this->browser = $capabilities['browserName'];
  37.  
  38. $payload = array("desiredCapabilities" => $capabilities);
  39. $response = $this->execute("POST", "/session", $payload);
  40.  
  41. // Parse out session id
  42. preg_match("/\nLocation:.*\/(.*)\n/", $response['header'], $matches);
  43. if (!empty($response['body'])) {
  44. $additional_info = $response['body'];
  45. } else if (!empty($response['header'])) {
  46. $additional_info = $response['header'];
  47. } else {
  48. $additional_info = "No response from server.";
  49. }
  50. PHPUnit_Framework_Assert::assertEquals(2, count($matches), "Did not get a session id from $server_url\n$additional_info");
  51. $this->session_id = trim($matches[1]);
  52. }
  53.  
  54. public static function InitAtSauce($sauce_username, $sauce_key, $os, $browser, $version = false, $additional_options = array()) {
  55. $capabilities = array_merge(array(
  56. 'javascriptEnabled' => true,
  57. 'platform' => strtoupper($os),
  58. 'browserName' => $browser,
  59. ), $additional_options);
  60. if ($version) {
  61. $capabilities["version"] = $version;
  62. }
  63. return new WebDriver_Driver("http://" . $sauce_username . ":" . $sauce_key . "@ondemand.saucelabs.com:80/wd/hub", $capabilities);
  64. }
  65.  
  66. public static function InitAtHost($host, $port, $browser, $additional_options = array()) {
  67. $capabilities = array_merge(array(
  68. 'javascriptEnabled' => true,
  69. 'browserName' => $browser,
  70. ), $additional_options);
  71. if (strcasecmp($browser, "iphone") == 0 || strcasecmp($browser, "android") == 0) {
  72. return new WebDriver_Driver("http://$host:$port/hub", $capabilities);
  73. } else {
  74. return new WebDriver_Driver("http://$host:$port/wd/hub", $capabilities);
  75. }
  76. }
  77.  
  78. public static function InitAtLocal($port, $browser, $additional_options = array()) {
  79. return self::InitAtHost('localhost', $port, $browser, $additional_options);
  80. }
  81.  
  82. public function running_at_sauce() {
  83. return (strpos($this->server_url, "saucelabs.com") !== false);
  84. }
  85.  
  86. public function sauce_url() {
  87. if ($this->running_at_sauce()) {
  88. return "https://saucelabs.com/jobs/{$this->session_id}";
  89. } else {
  90. return false;
  91. }
  92. }
  93.  
  94. public function execute($http_type, $relative_url, $payload = null) {
  95. if ($payload !== null) {
  96. $payload = json_encode($payload);
  97. }
  98. $relative_url = str_replace(':sessionId', $this->session_id, $relative_url);
  99. $full_url = $this->server_url . $relative_url;
  100.  
  101. $response = WebDriver::Curl($http_type, $full_url, $payload);
  102. if (isset($response['body'])) {
  103. $command_info = $http_type . " - " . $full_url . " - " . print_r($payload, true);
  104. $this->check_response_status($response['body'], $command_info);
  105. }
  106. return $response;
  107. }
  108.  
  109. private function check_response_status($body, $command_info) {
  110. $array = json_decode(trim($body), true);
  111. if (!is_null($array)) {
  112. $response_status_code = $array["status"];
  113. PHPUnit_Framework_Assert::assertArrayHasKey($response_status_code, self::$status_codes, "Unknown status code $response_status_code returned from server.\n$body");
  114. $response_info = $response_status_code . " - " . self::$status_codes[$response_status_code][0] . " - " . self::$status_codes[$response_status_code][1];
  115. $additional_info = isset($array['value']['message']) ? "Message: " . $array['value']['message'] : "Response: " . $body;
  116. PHPUnit_Framework_Assert::assertEquals(0, $response_status_code, "Unsuccessful WebDriver command: $response_info\nCommand: $command_info\n$additional_info");
  117. }
  118. }
  119.  
  120. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId
  121. public function quit() {
  122. $this->execute("DELETE", "/session/:sessionId");
  123. }
  124.  
  125. /********************************************************************
  126. * Getters
  127. */
  128.  
  129. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId
  130. public function get_capabilities() {
  131. $response = $this->execute("GET", "/session/:sessionId");
  132. return WebDriver::GetJSONValue($response);
  133. }
  134.  
  135. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/url
  136. public function get_url() {
  137. $response = $this->execute("GET", "/session/:sessionId/url");
  138. return WebDriver::GetJSONValue($response);
  139. }
  140.  
  141. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/title
  142. public function get_title() {
  143. $response = $this->execute("GET", "/session/:sessionId/title");
  144. return WebDriver::GetJSONValue($response);
  145. }
  146.  
  147. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/source
  148. public function get_source() {
  149. $response = $this->execute("GET", "/session/:sessionId/source");
  150. return WebDriver::GetJSONValue($response);
  151. }
  152.  
  153. public function get_text() {
  154. return $this->get_element("tag name=body")->get_text();
  155. }
  156.  
  157. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/screenshot
  158. public function get_screenshot() {
  159. $response = $this->execute("GET", "/session/:sessionId/screenshot");
  160. $base64_encoded_png = WebDriver::GetJSONValue($response);
  161. return base64_decode($base64_encoded_png);
  162. }
  163.  
  164. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/ime/available_engines
  165. // Not supported as of Selenium 2.0b3
  166. public function get_all_ime_engines() {
  167. $response = $this->execute("GET", "/session/:sessionId/ime/available_engines");
  168. return WebDriver::GetJSONValue($response);
  169. }
  170.  
  171. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/ime/active_engine
  172. // Not supported as of Selenium 2.0b3
  173. public function get_ime_engine() {
  174. $response = $this->execute("GET", "/session/:sessionId/ime/active_engine");
  175. return WebDriver::GetJSONValue($response);
  176. }
  177.  
  178. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/ime/activated
  179. // Not supported as of Selenium 2.0b3
  180. public function is_ime_active() {
  181. $response = $this->execute("GET", "/session/:sessionId/ime/activated");
  182. return WebDriver::GetJSONValue($response);
  183. }
  184.  
  185. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element
  186. public function get_element($locator) {
  187. $payload = WebDriver::ParseLocator($locator);
  188. $response = $this->execute("POST", "/session/:sessionId/element", $payload);
  189. $element_id = WebDriver::GetJSONValue($response, "ELEMENT");
  190. return new WebDriver_WebElement($this, $element_id, $locator);
  191. }
  192.  
  193. // WebDriver can do implicit waits for AJAX elements, but sometimes you need explicit reloads
  194. // Note: is_element_present() will use the wait time, if any, that you've set with set_implicit_wait()
  195. public function get_element_reload($locator, $max_wait_minutes = 2) {
  196. $start_time = time();
  197. $end_time = $start_time + $max_wait_minutes * 60;
  198. while (time() < $end_time && $this->is_element_present($locator) == false) {
  199. $this->reload();
  200. }
  201. return $this->get_element($locator);
  202. }
  203.  
  204. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/elements
  205. public function get_all_elements($locator) {
  206. $payload = WebDriver::ParseLocator($locator);
  207. $response = $this->execute("POST", "/session/:sessionId/elements", $payload);
  208. $element_ids = WebDriver::GetJSONValue($response, "ELEMENT");
  209. $elements = array();
  210. foreach ($element_ids as $element_id) {
  211. $elements[] = new WebDriver_WebElement($this, $element_id, $locator);
  212. }
  213. return $elements;
  214. }
  215.  
  216. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/active
  217. public function get_active_element() {
  218. $response = $this->execute("POST", "/session/:sessionId/element/active");
  219. $element_id = WebDriver::GetJSONValue($response, "ELEMENT");
  220. return new WebDriver_WebElement($this, $element_id, "active=true");
  221. }
  222.  
  223. public function is_element_present($locator) {
  224. try {
  225. $this->get_element($locator);
  226. $is_element_present = true;
  227. } catch (Exception $e) {
  228. $is_element_present = false;
  229. }
  230. return $is_element_present;
  231. }
  232.  
  233. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/window_handle
  234. public function get_window_handle() {
  235. $response = $this->execute("GET", "/session/:sessionId/window_handle");
  236. return WebDriver::GetJSONValue($response);
  237. }
  238.  
  239. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/window_handles
  240. public function get_all_window_handles() {
  241. $response = $this->execute("GET", "/session/:sessionId/window_handles");
  242. return WebDriver::GetJSONValue($response);
  243. }
  244.  
  245. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/speed
  246. // Not supported as of Selenium 2.0b3
  247. public function get_input_speed() {
  248. $response = $this->execute("GET", "/session/:sessionId/speed");
  249. return WebDriver::GetJSONValue($response);
  250. }
  251.  
  252. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/cookie
  253. public function get_all_cookies() {
  254. $response = $this->execute("GET", "/session/:sessionId/cookie");
  255. return WebDriver::GetJSONValue($response);
  256. }
  257.  
  258. public function get_cookie($name, $property = null) {
  259. $all_cookies = $this->get_all_cookies();
  260. foreach ($all_cookies as $cookie) {
  261. if ($cookie['name'] == $name) {
  262. if (is_null($property)) {
  263. return $cookie;
  264. } else {
  265. return $cookie[$property];
  266. }
  267. }
  268. }
  269. }
  270.  
  271. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/orientation
  272. // Not supported in iPhone as of Selenium 2.0b3
  273. private function get_orientation() {
  274. $response = $this->execute("GET", "/session/:sessionId/orientation");
  275. return WebDriver::GetJSONValue($response);
  276. }
  277. public function is_landscape() { return $this->get_orientation() == "LANDSCAPE"; }
  278. public function is_portrait() { return $this->get_orientation() == "PORTRAIT"; }
  279.  
  280. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/alert_text
  281. public function get_alert_text() {
  282. $response = $this->execute("GET", "/session/:sessionId/alert_text");
  283. return WebDriver::GetJSONValue($response);
  284. }
  285.  
  286. /********************************************************************
  287. * Setters
  288. */
  289.  
  290. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/timeouts/async_script
  291. public function set_async_timeout($milliseconds) {
  292. $payload = array("ms" => $milliseconds);
  293. $this->execute("POST", "/session/:sessionId/timeouts/async_script", $payload);
  294. }
  295.  
  296. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/timeouts/implicit_wait
  297. public function set_implicit_wait($milliseconds) {
  298. WebDriver::$ImplicitWaitMS = $milliseconds;
  299. $payload = array("ms" => $milliseconds);
  300. $this->execute("POST", "/session/:sessionId/timeouts/implicit_wait", $payload);
  301. }
  302.  
  303. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/url
  304. public function load($url) {
  305. $payload = array("url" => $url);
  306. $this->execute("POST", "/session/:sessionId/url", $payload);
  307. }
  308.  
  309. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/forward
  310. public function go_forward() {
  311. $this->execute("POST", "/session/:sessionId/forward");
  312. }
  313.  
  314. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/back
  315. public function go_back() {
  316. $this->execute("POST", "/session/:sessionId/back");
  317. }
  318.  
  319. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/refresh
  320. public function reload() {
  321. $this->execute("POST", "/session/:sessionId/refresh");
  322. }
  323.  
  324. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/window
  325. // IE appends the anchor tag to the window title, but only when it's done loading
  326. // Example: $driver->select_window("My Cool Page", "#chapter7") finds the window called "My Cool Page#chapter7" or "My Cool Page" in IE, and "My Cool Page" in all other browsers
  327. public function select_window($window_title, $ie_hash = '') {
  328. $all_window_handles = $this->get_all_window_handles();
  329. $all_titles = array();
  330. $current_title = "";
  331. $found_window = false;
  332. foreach ($all_window_handles as $window_handle) {
  333. $payload = array("name" => $window_handle);
  334. $this->execute("POST", "/session/:sessionId/window", $payload);
  335. $current_title = $this->get_title();
  336. $all_titles[] = $current_title;
  337. if ($current_title == $window_title || ($this->browser == 'internet explorer' && $current_title == $window_title . $ie_hash)) {
  338. $found_window = true;
  339. break;
  340. }
  341. }
  342. PHPUnit_Framework_Assert::assertTrue($found_window, "Could not find window with title <$window_title> and optional hash <$ie_hash>. Found " . count($all_titles) . " windows: " . implode("; ", $all_titles));
  343. }
  344.  
  345. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/window
  346. public function close_window() {
  347. $this->execute("DELETE", "/session/:sessionId/window");
  348. }
  349.  
  350. public function maximize_window() {
  351. $this->execute_js_sync("window.moveTo(0,0)");
  352. $this->execute_js_sync("window.resizeTo(screen.width,screen.height)");
  353. $this->execute_js_sync("window.focus()");
  354. }
  355.  
  356. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/ime/deactivate
  357. // Not supported as of Selenium 2.0b3
  358. public function deactivate_ime() {
  359. $this->execute("POST", "/session/:sessionId/ime/deactivate");
  360. }
  361.  
  362. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/ime/activate
  363. // Not supported as of Selenium 2.0b3
  364. public function activate_ime() {
  365. $this->execute("POST", "/session/:sessionId/ime/activate");
  366. }
  367.  
  368. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/frame
  369. public function select_frame($identifier = null) {
  370. if ($identifier !== null) {
  371. $this->get_element($identifier); // POST /session/:sessionId/frame does not use implicit wait but POST /session/:sessionId/element does
  372. }
  373. $payload = array("id" => $identifier);
  374. $this->execute("POST", "/session/:sessionId/frame", $payload);
  375. }
  376.  
  377. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/cookie
  378. public function set_cookie($name, $value, $path = null, $domain = null, $secure = false, $expiry = null) {
  379. $payload = array(
  380. 'cookie' => array(
  381. 'name' => $name,
  382. 'value' => $value,
  383. 'secure' => $secure, // The documentation says this is optional, but selenium server 2.0b1 throws a NullPointerException if it's not provided
  384. )
  385. );
  386. if (!is_null($path)) {
  387. $payload['cookie']['path'] = $path;
  388. }
  389. if (!is_null($domain)) {
  390. $payload['cookie']['domain'] = $domain;
  391. }
  392. if (!is_null($expiry)) {
  393. $payload['cookie']['expiry'] = $expiry;
  394. }
  395. $this->execute("POST", "/session/:sessionId/cookie", $payload);
  396. }
  397.  
  398. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/cookie
  399. public function delete_all_cookies() {
  400. $this->execute("DELETE", "/session/:sessionId/cookie");
  401. }
  402.  
  403. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/cookie/:name
  404. public function delete_cookie($name) {
  405. $this->execute("DELETE", "/session/:sessionId/cookie/" . $name);
  406. }
  407.  
  408. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/execute
  409. public function execute_js_sync($javascript, $arguments = array()) {
  410. $payload = array(
  411. "script" => $javascript,
  412. "args" => $arguments,
  413. );
  414. return $this->execute("POST", "/session/:sessionId/execute", $payload);
  415. }
  416.  
  417. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/execute_async
  418. public function execute_js_async($javascript, $arguments = array()) {
  419. $payload = array(
  420. "script" => $javascript,
  421. "args" => $arguments,
  422. );
  423. return $this->execute("POST", "/session/:sessionId/execute_async", $payload);
  424. }
  425.  
  426. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/speed
  427. // Not supported as of Selenium 2.0b3
  428. public function set_input_speed($speed) {
  429. $payload = array("speed" => $speed);
  430. $this->execute("POST", "/session/:sessionId/speed", $payload);
  431. }
  432.  
  433. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/modifier
  434. private function send_modifier($modifier_code, $is_down) {
  435. $payload = array(
  436. 'value' => $modifier_code,
  437. 'isdown' => $is_down
  438. );
  439. $this->execute("POST", "/session/:sessionId/modifier", $payload);
  440. }
  441. public function ctrl_down() { send_modifier("U+E009", true); }
  442. public function ctrl_up() { send_modifier("U+E009", false); }
  443. public function shift_down() { send_modifier("U+E008", true); }
  444. public function shift_up() { send_modifier("U+E008", false); }
  445. public function alt_down() { send_modifier("U+E00A", true); }
  446. public function alt_up() { send_modifier("U+E00A", false); }
  447. public function command_down() { send_modifier("U+E03D", true); }
  448. public function command_up() { send_modifier("U+E03D", false); }
  449.  
  450. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/orientation
  451. // Not supported as of Selenium 2.0b3
  452. private function set_orientation($new_orientation) {
  453. $payload = array("orientation", $new_orientation);
  454. $this->execute("POST", "/session/:sessionId/orientation", $payload);
  455. }
  456. public function rotate_landscape() { $this->set_orientation("LANDSCAPE"); }
  457. public function rotate_portrait() { $this->set_orientation("PORTRAIT"); }
  458.  
  459. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/moveto
  460. public function move_cursor($right, $down) {
  461. $payload = array(
  462. "xoffset" => $right,
  463. "yoffset" => $down
  464. );
  465. $this->execute("POST", "/session/:sessionId/moveto", $payload);
  466. }
  467.  
  468. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/click
  469. private function click_mouse($button) {
  470. $payload = array("button" => $button);
  471. $this->execute("POST", "/session/:sessionId/click", $payload);
  472. }
  473. public function click() { $this->click_mouse(0); }
  474. public function middle_click() { $this->click_mouse(1); }
  475. public function right_click() { $this->click_mouse(2); }
  476.  
  477. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/buttondown
  478. public function click_and_hold() {
  479. $this->execute("POST", "/session/:sessionId/buttondown");
  480. }
  481.  
  482. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/buttonup
  483. public function release_click() {
  484. $this->execute("POST", "/session/:sessionId/buttonup");
  485. }
  486.  
  487. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/doubleclick
  488. public function double_click() {
  489. $this->execute("POST", "/session/:sessionId/doubleclick");
  490. }
  491.  
  492. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/alert_text
  493. public function type_alert($text) {
  494. $payload = array("keysToSend" => $text);
  495. $this->execute("POST", "/session/:sessionId/alert_text", $payload);
  496. }
  497.  
  498. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/accept_alert
  499. public function accept_alert() {
  500. $this->execute("POST", "/session/:sessionId/accept_alert");
  501. }
  502.  
  503. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/dismiss_alert
  504. public function dismiss_alert() {
  505. $this->execute("POST", "/session/:sessionId/dismiss_alert");
  506. }
  507.  
  508. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/touch/click
  509. public function single_tap($element_id) {
  510. $payload = array("element" => $element_id);
  511. $this->execute("POST", "/session/:sessionId/touch/click", $payload);
  512. }
  513.  
  514. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/touch/doubleclick
  515. public function double_tap($element_id) {
  516. $payload = array("element" => $element_id);
  517. $this->execute("POST", "/session/:sessionId/touch/doubleclick", $payload);
  518. }
  519.  
  520. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/touch/longclick
  521. public function long_tap($element_id) {
  522. $payload = array("element" => $element_id);
  523. $this->execute("POST", "/session/:sessionId/touch/longclick", $payload);
  524. }
  525.  
  526. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/touch/down
  527. public function touch_down($x_coordinate, $y_coordinate) {
  528. $payload = array(
  529. "x" => $x_coordinate,
  530. "y" => $y_coordinate,
  531. );
  532. $this->execute("POST", "/session/:sessionId/touch/down", $payload);
  533. }
  534.  
  535. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/touch/up
  536. public function touch_up($x_coordinate, $y_coordinate) {
  537. $payload = array(
  538. "x" => $x_coordinate,
  539. "y" => $y_coordinate,
  540. );
  541. $this->execute("POST", "/session/:sessionId/touch/up", $payload);
  542. }
  543.  
  544. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/touch/move
  545. public function touch_move($x_coordinate, $y_coordinate) {
  546. $payload = array(
  547. "x" => $x_coordinate,
  548. "y" => $y_coordinate,
  549. );
  550. $this->execute("POST", "/session/:sessionId/touch/move", $payload);
  551. }
  552.  
  553. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/touch/scroll
  554. public function touch_scroll_at($start_element_id, $pixels_offset_x, $pixels_offset_y) {
  555. $payload = array(
  556. "element" => $start_element_id,
  557. "xOffset" => $pixels_offset_x,
  558. "yOffset" => $pixels_offset_y,
  559. );
  560. $this->execute("POST", "/session/:sessionId/touch/scroll", $payload);
  561. }
  562.  
  563. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/touch/scroll
  564. public function touch_scroll($pixels_offset_x, $pixels_offset_y) {
  565. $payload = array(
  566. "xOffset" => $pixels_offset_x,
  567. "yOffset" => $pixels_offset_y,
  568. );
  569. $this->execute("POST", "/session/:sessionId/touch/scroll", $payload);
  570. }
  571.  
  572. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#session/:sessionId/touch/flick
  573. public function touch_flick_at($start_element_id, $pixels_offset_x, $pixels_offset_y, $pixels_per_second) {
  574. $payload = array(
  575. "element" => $start_element_id,
  576. "xOffset" => $pixels_offset_x,
  577. "yOffset" => $pixels_offset_y,
  578. "speed" => $pixels_per_second,
  579. );
  580. $this->execute("POST", "/session/:sessionId/touch/flick", $payload);
  581. }
  582.  
  583. // See http://code.google.com/p/selenium/wiki/JsonWireProtocol#session/:sessionId/touch/flick
  584. public function touch_flick($pixels_per_second_x, $pixels_per_second_y) {
  585. $payload = array(
  586. "xSpeed" => $pixels_per_second_x,
  587. "ySpeed" => $pixels_per_second_y,
  588. );
  589. $this->execute("POST", "/session/:sessionId/touch/flick", $payload);
  590. }
  591.  
  592. // See https://saucelabs.com/docs/sauce-ondemand#alternative-annotation-methods
  593. public function set_sauce_context($field, $value) {
  594. if ($this->running_at_sauce()) {
  595. $payload = json_encode(array($field => $value));
  596. $url_parts = parse_url($this->server_url);
  597. WebDriver::Curl("PUT", "http://" . $url_parts['user'] . ":" . $url_parts['pass'] . "@saucelabs.com/rest/v1/" . $url_parts['user'] . "/jobs/" . $this->session_id, $payload);
  598. }
  599. }
  600.  
  601. /********************************************************************
  602. * Asserters
  603. */
  604.  
  605. public function assert_url($expected_url) {
  606. PHPUnit_Framework_Assert::assertEquals($expected_url, $this->get_url(), "Failed asserting that URL is <$expected_url>.");
  607. }
  608.  
  609. // IE appends the anchor tag to the window title, but only when it's done loading
  610. // Example: $driver->assert_title("My Cool Page", "#chapter7") asserts that the page title is "My Cool Page#chapter7" in IE, and "My Cool Page" in all other browsers
  611. // WebDriver does not wait for the page to finish loading before returning the title, so we check repeatedly
  612. public function assert_title($expected_title, $ie_hash = '') {
  613. $start_time = time();
  614. $end_time = $start_time + WebDriver::$ImplicitWaitMS/1000;
  615. $title_matched = false;
  616. do {
  617. $actual_title = $this->get_title();
  618. $title_matched = ($this->browser == 'internet explorer' && $actual_title == $expected_title . $ie_hash) || ($actual_title == $expected_title);
  619. } while (time() < $end_time && !$title_matched);
  620. PHPUnit_Framework_Assert::assertTrue($title_matched, "Failed asserting that <$actual_title> is <$expected_title> with optional hash <$ie_hash>.");
  621. }
  622.  
  623. public function assert_element_present($element_locator) {
  624. PHPUnit_Framework_Assert::assertTrue($this->is_element_present($element_locator), "Failed asserting that <$element_locator> is present");
  625. }
  626.  
  627. public function assert_element_not_present($element_locator) {
  628. PHPUnit_Framework_Assert::assertFalse($this->is_element_present($element_locator), "Failed asserting that <$element_locator> is not present");
  629. }
  630.  
  631. public function assert_string_present($expected_string) {
  632. $page_text = $this->get_text();
  633. PHPUnit_Framework_Assert::assertContains($expected_string, $page_text, "Failed asserting that page text contains <$expected_string>.\n$page_text");
  634. }
  635.  
  636. public function assert_string_present_in($expected_string, $string){
  637. PHPUnit_Framework_Assert::assertContains($expected_string, $string, "Failed asserting that string contains <$expected_string>.\n$string");
  638. }
  639.  
  640. public function assert_string_not_present($expected_missing_string) {
  641. $page_text = $this->get_text();
  642. PHPUnit_Framework_Assert::assertNotContains($expected_missing_string, $page_text, "Failed asserting that page text does not contain <$expected_missing_string>.\n$page_text");
  643. }
  644.  
  645. public function assert_alert_text($expected_text) {
  646. $actual_text = $this->get_alert_text();
  647. PHPUnit_Framework_Assert::assertEquals($expected_text, $actual_text, "Failed asserting that alert text is <$expected_text>.");
  648. }
  649. }
  650.  
  651.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement