Per wikitech-l discussion: Move tests from maintenance/tests/ to tests/. They're...
[lhc/web/wiklou.git] / maintenance / tests / selenium / SeleniumServerManager.php
index 7abb34b..ae5ea68 100644 (file)
@@ -34,8 +34,8 @@ class SeleniumServerManager {
        private $SeleniumServerExecPath;
 
        public function __construct( $startServer,
-                                    $serverPort,
-                                    $serverExecPath ) {
+                                        $serverPort,
+                                        $serverExecPath ) {
                $this->OS = (string) PHP_OS;
                if ( isset( $startServer ) )
                        $this->SeleniumStartServer = $startServer;
@@ -82,7 +82,7 @@ class SeleniumServerManager {
                switch ( $this->OS ) {
                        case "Linux":
 #                      case' CYGWIN_NT-5.1':
-#                      case 'Darwin':
+                       case 'Darwin':
 #                      case 'FreeBSD':
 #                      case 'HP-UX':
 #                      case 'IRIX64':
@@ -113,7 +113,7 @@ class SeleniumServerManager {
                switch ( $this->OS ) {
                        case "Linux":
 #                      case' CYGWIN_NT-5.1':
-#                      case 'Darwin':
+                       case 'Darwin':
 #                      case 'FreeBSD':
 #                      case 'HP-UX':
 #                      case 'IRIX64':
@@ -140,8 +140,17 @@ class SeleniumServerManager {
        private function startServerOnUnix() {
 
                $output = array();
-                $user = $_ENV['USER'];
-               exec("ps -U " . $user . " w | grep -i selenium-server", $output);
+               $user = $_ENV['USER'];
+               // @fixme this should be a little more generalized :)
+               if (PHP_OS == 'Darwin') {
+                       // Mac OS X's ps barfs on the 'w' param, but doesn't need it.
+                       $ps = "ps -U %s";
+               } else {
+                       // Good on Linux
+                       $ps = "ps -U %s w";
+               }
+               $psCommand = sprintf($ps, escapeshellarg($user));
+               exec($psCommand . " | grep -i selenium-server", $output);
 
                // Start server. If there is already a server running,
                // return running.
@@ -163,11 +172,11 @@ class SeleniumServerManager {
 
                                $commandSuffix = ' > /dev/null 2>&1'. ' & echo $!';
                                $portText = ' -port ' . $this->SeleniumServerPort;
-                               $command = "java -jar " . 
-                                       $this->SeleniumServerExecPath .
+                               $command = "java -jar " .
+                                       escapeshellarg($this->SeleniumServerExecPath) .
                                        $portText . $commandSuffix;
-                               exec($command ,$op); 
-                               $pid = (int)$op[0]; 
+                               exec($command ,$op);
+                               $pid = (int)$op[0];
                                if ( $pid != "" )
                                        $this->SeleniumServerPid = $pid;
                                else {
@@ -179,12 +188,13 @@ class SeleniumServerManager {
                                // on its port. Note: this solution kinda
                                // stinks, since it uses a wait loop - dnessett
 
+                               wfSuppressWarnings();
                                for ( $cnt = 1;
-                                     $cnt <= $this->SeleniumServerStartTimeout;
-                                     $cnt++ ) {
-                                       $fp = @fsockopen ( 'localhost',
+                                         $cnt <= $this->SeleniumServerStartTimeout;
+                                         $cnt++ ) {
+                                       $fp = fsockopen ( 'localhost',
                                                $this->SeleniumServerPort,
-                                               &$errno, &$errstr, 0 );
+                                               $errno, $errstr, 0 );
                                        if ( !$fp ) {
                                                sleep( 1 );
                                                continue;
@@ -194,6 +204,7 @@ class SeleniumServerManager {
                                                return 'started';
                                        }
                                }
+                               wfRestoreWarnings();
                                echo ( "Starting Selenium server timed out.\n" );
                                return 'failed';
                        }
@@ -201,19 +212,19 @@ class SeleniumServerManager {
                        else return 'running';
 
                }
-                // No Server execution path defined.
+                               // No Server execution path defined.
                return 'failed';
        }
 
        private function startServerOnWindows() {
-               // Unimplemented. 
+               // Unimplemented.
                return 'failed';
        }
 
        private function stopServerOnUnix() {
 
                if ( !empty( $this->SeleniumServerPid ) &&
-                    $this->SeleniumServerPid != 'NaN' ) {
+                        $this->SeleniumServerPid != 'NaN' ) {
                        exec( "kill -9 " . $this->SeleniumServerPid );
                        return 'stopped';
                }
@@ -221,7 +232,7 @@ class SeleniumServerManager {
        }
 
        private function stopServerOnWindows() {
-               // Unimplemented. 
+               // Unimplemented.
                return 'failed';
 
        }