/src/Selenium/Driver.php

https://github.com/alexandresalome/PHP-Selenium · PHP · 231 lines · 98 code · 33 blank · 100 comment · 12 complexity · 6c7385d3d80f91d8f4b8e0ba586af4c1 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of PHP Selenium Library.
  4. * (c) Alexandre Salomé <alexandre.salome@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Selenium;
  10. /**
  11. * Driver for communication with Selenium server
  12. *
  13. * @author Alexandre Salomé <alexandre.salome@gmail.com>
  14. */
  15. class Driver
  16. {
  17. /**
  18. * URL to the server
  19. *
  20. * @var string
  21. */
  22. protected $url;
  23. /**
  24. * Timeout of the server
  25. *
  26. * @var int
  27. */
  28. protected $timeout;
  29. /**
  30. * Current session ID
  31. *
  32. * @var string
  33. */
  34. protected $sessionId;
  35. /**
  36. * Instantiates the driver.
  37. *
  38. * @param string $url The URL of the server
  39. * @param int $timeout Timeout
  40. */
  41. public function __construct($url, $timeout)
  42. {
  43. $this->url = $url;
  44. $this->timeout = $timeout;
  45. }
  46. /**
  47. * Starts a new session.
  48. *
  49. * @param string $type Type of browser
  50. * @param string $startUrl Start URL for the browser
  51. */
  52. public function start($type = '*firefox', $startUrl = 'http://localhost')
  53. {
  54. if (null !== $this->sessionId) {
  55. throw new Exception("Session already started");
  56. }
  57. $response = $this->doExecute('getNewBrowserSession', $type, $startUrl);
  58. if (preg_match('/^OK,(.*)$/', $response, $vars)) {
  59. $this->sessionId = $vars[1];
  60. } else {
  61. throw new Exception("Invalid response from server : $response");
  62. }
  63. }
  64. /**
  65. * Executes an action
  66. *
  67. * @param string $command Command to execute
  68. * @param string $target First parameter
  69. * @param string $value Second parameter
  70. *
  71. * @return void
  72. */
  73. public function action($command, $target = null, $value = null)
  74. {
  75. $result = $this->doExecute($command, $target, $value);
  76. if ($result !== 'OK') {
  77. throw new Exception("Unexpected response from Selenium server : ".$result);
  78. }
  79. }
  80. /**
  81. * Executes a command on the server and returns a string.
  82. *
  83. * @param string $command The command to execute
  84. * @param string $target First parameter
  85. * @param string $value Second parameter
  86. *
  87. * @return string The result of the command as a string
  88. */
  89. public function getString($command, $target = null, $value = null)
  90. {
  91. $result = $this->doExecute($command, $target, $value);
  92. if (!preg_match('/^OK,/', $result)) {
  93. throw new Exception("Unexpected response from Selenium server : ".$result);
  94. }
  95. return strlen($result) > 3 ? substr($result, 3) : '';
  96. }
  97. /**
  98. * Executes a command on the server and returns an array of string.
  99. *
  100. * @param string $command Command to execute
  101. * @param string $target First parameter
  102. * @param string $value Second parameter
  103. *
  104. * @return array The result of the command as an array of string
  105. */
  106. public function getStringArray($command, $target = null, $value = null)
  107. {
  108. $string = $this->getString($command, $target, $value);
  109. $results = preg_split('/(?<!\\\),/', $string);
  110. foreach ($results as &$result) {
  111. $result = str_replace('\,', ',', $result);
  112. }
  113. return $results;
  114. }
  115. /**
  116. * Executes a command on the server and returns a number.
  117. *
  118. * @param string $command The command to execute
  119. * @param string $target First parameter
  120. * @param string $value Second parameter
  121. *
  122. * @return int The result of the command as a number
  123. */
  124. public function getNumber($command, $target = null, $value = null)
  125. {
  126. $string = $this->getString($command, $target, $value);
  127. return (int) $string;
  128. }
  129. /**
  130. * Executes a command on the server and returns a boolean.
  131. *
  132. * @param string $command The command to execute
  133. * @param string $target First parameter
  134. * @param string $value Second parameter
  135. *
  136. * @return boolean The result of the command as a boolean
  137. */
  138. public function getBoolean($command, $target = null, $value = null)
  139. {
  140. $string = $this->getString($command, $target, $value);
  141. return $string == 'true';
  142. }
  143. /**
  144. * Stops the session.
  145. *
  146. * @return void
  147. */
  148. public function stop()
  149. {
  150. if (null === $this->sessionId) {
  151. throw new Exception("Session not started");
  152. }
  153. $this->doExecute('testComplete');
  154. $this->sessionId = null;
  155. }
  156. /**
  157. * Executes a raw command on the server and integrate the current session
  158. * identifier if available.
  159. *
  160. * @param string $command Command to execute
  161. * @param string $target First argument
  162. * @param string $value Second argument
  163. *
  164. * @return string The raw result of the command
  165. */
  166. protected function doExecute($command, $target = null, $value = null)
  167. {
  168. $postFields = array();
  169. $query = array('cmd' => $command);
  170. if ($target !== null) {
  171. $postFields[1] = $target;
  172. }
  173. if ($value !== null) {
  174. $postFields[2] = $value;
  175. }
  176. if (null !== $this->sessionId) {
  177. $query['sessionId'] = $this->sessionId;
  178. }
  179. $query = http_build_query($query);
  180. $url = $this->url.'?'.$query;
  181. //open connection
  182. $ch = curl_init($url);
  183. curl_setopt($ch, CURLOPT_POST, count($postFields));
  184. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
  185. curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
  186. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  187. $result = curl_exec($ch);
  188. $curlErrno = curl_errno($ch);
  189. curl_close($ch);
  190. if ($curlErrno > 0) {
  191. throw new Exception("Unable to connect ! ");
  192. }
  193. if (false === $result) {
  194. throw new Exception("Connection refused");
  195. }
  196. return $result;
  197. }
  198. }