remote-control.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. *
  4. * Based on the Domain51_Testing_Selenium class available at
  5. * http://domain51.googlecode.com/svn/Domain51/trunk/
  6. *
  7. * @author Travis Swicegood <development [at] domain51 [dot] com>
  8. *
  9. */
  10. class SimpleSeleniumRemoteControl
  11. {
  12. private $_browser = '';
  13. private $_browserUrl = '';
  14. private $_host = 'localhost';
  15. private $_port = 4444;
  16. private $_timeout = 30000;
  17. private $_sessionId = null;
  18. private $_commandMap = array(
  19. 'bool' => array(
  20. 'verify',
  21. 'verifyTextPresent',
  22. 'verifyTextNotPresent',
  23. 'verifyValue'
  24. ),
  25. 'string' => array(
  26. 'getNewBrowserSession',
  27. ),
  28. );
  29. public function __construct($browser, $browserUrl, $host = 'localhost', $port = 4444, $timeout = 30000) {
  30. $this->_browser = $browser;
  31. $this->_browserUrl = $browserUrl;
  32. $this->_host = $host;
  33. $this->_port = $port;
  34. $this->_timeout = $timeout;
  35. }
  36. public function sessionIdParser($response) {
  37. return substr($response, 3);
  38. }
  39. public function start() {
  40. $response = $this->cmd('getNewBrowserSession', array($this->_browser, $this->_browserUrl));
  41. $this->_sessionId = $this->sessionIdParser($response);
  42. }
  43. public function stop() {
  44. $this->cmd('testComplete');
  45. $this->_sessionId = null;
  46. }
  47. public function __call($method, $arguments) {
  48. $response = $this->cmd($method, $arguments);
  49. foreach ($this->_commandMap as $type => $commands) {
  50. if (!in_array($method, $commands)) {
  51. continue;
  52. $type = null;
  53. }
  54. break;
  55. }
  56. switch ($type) {
  57. case 'bool' :
  58. return substr($response, 0, 2) == 'OK' ? true : false;
  59. break;
  60. case 'string' :
  61. default:
  62. return $response;
  63. }
  64. }
  65. private function _server() {
  66. return "http://{$this->_host}:{$this->_port}/selenium-server/driver/";
  67. }
  68. public function buildUrlCmd($method, $arguments = array()) {
  69. $params = array(
  70. 'cmd=' . urlencode($method),
  71. );
  72. $i = 1;
  73. foreach ($arguments as $param) {
  74. $params[] = $i++ . '=' . urlencode(trim($param));
  75. }
  76. if (isset($this->_sessionId)) {
  77. $params[] = 'sessionId=' . $this->_sessionId;
  78. }
  79. return $this->_server()."?".implode('&', $params);
  80. }
  81. public function cmd($method, $arguments = array()) {
  82. $url = $this->buildUrlCmd($method, $arguments);
  83. $response = $this->_sendRequest($url);
  84. return $response;
  85. }
  86. public function isUp() {
  87. return (bool)@fsockopen($this->_host, $this->_port, $errno, $errstr, 30);
  88. }
  89. private function _initCurl($url) {
  90. if (!function_exists('curl_init')) {
  91. throw new Exception('this code currently requires the curl extension');
  92. }
  93. if (!$ch = curl_init($url)) {
  94. throw new Exception('Unable to setup curl');
  95. }
  96. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  97. curl_setopt($ch, CURLOPT_TIMEOUT, floor($this->_timeout));
  98. return $ch;
  99. }
  100. private function _sendRequest($url) {
  101. $ch = $this->_initCurl($url);
  102. $result = curl_exec($ch);
  103. if (($errno = curl_errno($ch)) != 0) {
  104. throw new Exception('Curl returned non-null errno ' . $errno . ':' . curl_error($ch));
  105. }
  106. curl_close($ch);
  107. return $result;
  108. }
  109. }