svn:eol-style native
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sat, 16 Oct 2010 18:47:46 +0000 (18:47 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sat, 16 Oct 2010 18:47:46 +0000 (18:47 +0000)
maintenance/tests/selenium/SeleniumServerManager.php

index e3daaff..7abb34b 100644 (file)
-<?php\r
-/**\r
- * Selenium server manager\r
- *\r
- * @file\r
- * @ingroup Maintenance\r
- * Copyright (C) 2010 Dan Nessett <dnessett@yahoo.com>\r
- * http://citizendium.org/\r
- *\r
- * This program is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License along\r
- * with this program; if not, write to the Free Software Foundation, Inc.,\r
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
- * http://www.gnu.org/copyleft/gpl.html\r
- *\r
- * @addtogroup Maintenance\r
- */\r
-\r
-class SeleniumServerManager {\r
-       private $SeleniumStartServer = false;\r
-       private $OS = '';\r
-       private $SeleniumServerPid = 'NaN';\r
-       private $SeleniumServerPort = 4444;\r
-       private $SeleniumServerStartTimeout = 10; // 10 secs.\r
-       private $SeleniumServerExecPath;\r
-\r
-       public function __construct( $startServer,\r
-                                    $serverPort,\r
-                                    $serverExecPath ) {\r
-               $this->OS = (string) PHP_OS;\r
-               if ( isset( $startServer ) )\r
-                       $this->SeleniumStartServer = $startServer;\r
-               if ( isset( $serverPort ) )\r
-                       $this->SeleniumServerPort = $serverPort;\r
-               if ( isset( $serverExecPath ) )\r
-                       $this->SeleniumServerExecPath = $serverExecPath;\r
-               return;\r
-       }\r
-\r
-       // Getters for certain private attributes. No setters, since they\r
-       // should not change after the manager object is created.\r
-\r
-       public function getSeleniumStartServer() {\r
-               return $this->SeleniumStartServer;\r
-       }\r
-\r
-       public function getSeleniumServerPort() {\r
-               return $this->SeleniumServerPort;\r
-       }\r
-\r
-       public function getSeleniumServerPid() {\r
-               return $this->SeleniumServerPid;\r
-       }\r
-\r
-       // Changing value of SeleniumStartServer allows starting server after\r
-       // creation of the class instance. Only allow setting SeleniumStartServer\r
-       // to true, since after server is started, it is shut down by stop().\r
-\r
-       public function setSeleniumStartServer( $startServer ) {\r
-               if ( $startServer == true ) $this->SeleniumStartServer = true;\r
-       }\r
-\r
-       // return values are: 1) started - server started, 2) failed -\r
-       // server not started, 3) running - instructed to start server, but\r
-       // server already running\r
-\r
-       public function start() {\r
-\r
-               if ( !$this->SeleniumStartServer ) return 'failed';\r
-\r
-               // commented out cases are untested\r
-\r
-               switch ( $this->OS ) {\r
-                       case "Linux":\r
-#                      case' CYGWIN_NT-5.1':\r
-#                      case 'Darwin':\r
-#                      case 'FreeBSD':\r
-#                      case 'HP-UX':\r
-#                      case 'IRIX64':\r
-#                      case 'NetBSD':\r
-#                      case 'OpenBSD':\r
-#                      case 'SunOS':\r
-#                      case 'Unix':\r
-                               // *nix based OS\r
-                               return $this->startServerOnUnix();\r
-                               break;\r
-                       case "Windows":\r
-                       case "WIN32":\r
-                       case "WINNT":\r
-                               // Windows\r
-                               return $this->startServerOnWindows();\r
-                               break;\r
-                       default:\r
-                               // An untested OS\r
-                               return 'failed';\r
-                               break;\r
-               }\r
-       }\r
-\r
-       public function stop() {\r
-\r
-               // commented out cases are untested\r
-\r
-               switch ( $this->OS ) {\r
-                       case "Linux":\r
-#                      case' CYGWIN_NT-5.1':\r
-#                      case 'Darwin':\r
-#                      case 'FreeBSD':\r
-#                      case 'HP-UX':\r
-#                      case 'IRIX64':\r
-#                      case 'NetBSD':\r
-#                      case 'OpenBSD':\r
-#                      case 'SunOS':\r
-#                      case 'Unix':\r
-                               // *nix based OS\r
-                               return $this->stopServerOnUnix();\r
-                               break;\r
-                       case "Windows":\r
-                       case "WIN32":\r
-                       case "WINNT":\r
-                               // Windows\r
-                               return $this->stopServerOnWindows();\r
-                               break;\r
-                       default:\r
-                               // An untested OS\r
-                               return 'failed';\r
-                               break;\r
-               }\r
-       }\r
-\r
-       private function startServerOnUnix() {\r
-\r
-               $output = array();\r
-                $user = $_ENV['USER'];\r
-               exec("ps -U " . $user . " w | grep -i selenium-server", $output);\r
-\r
-               // Start server. If there is already a server running,\r
-               // return running.\r
-\r
-               if ( isset( $this->SeleniumServerExecPath ) ) {\r
-                       $found = 0;\r
-                       foreach ( $output as $string ) {\r
-                               $found += preg_match(\r
-                                       '~^(.*)java(.+)-jar(.+)selenium-server~',\r
-                                       $string );\r
-                       }\r
-                       if ( $found == 0 ) {\r
-\r
-                               // Didn't find the selenium server. Start it up.\r
-                               // First set up comamand line suffix.\r
-                               // NB: $! is pid of last job run in background\r
-                               // The echo guarentees it is put into $op when\r
-                               // the exec command is run.\r
-\r
-                               $commandSuffix = ' > /dev/null 2>&1'. ' & echo $!';\r
-                               $portText = ' -port ' . $this->SeleniumServerPort;\r
-                               $command = "java -jar " . \r
-                                       $this->SeleniumServerExecPath .\r
-                                       $portText . $commandSuffix;\r
-                               exec($command ,$op); \r
-                               $pid = (int)$op[0]; \r
-                               if ( $pid != "" )\r
-                                       $this->SeleniumServerPid = $pid;\r
-                               else {\r
-                                       $this->SeleniumServerPid = 'NaN';\r
-                                       // Server start failed.\r
-                                       return 'failed';\r
-                               }\r
-                               // Wait for the server to startup and listen\r
-                               // on its port. Note: this solution kinda\r
-                               // stinks, since it uses a wait loop - dnessett\r
-\r
-                               for ( $cnt = 1;\r
-                                     $cnt <= $this->SeleniumServerStartTimeout;\r
-                                     $cnt++ ) {\r
-                                       $fp = @fsockopen ( 'localhost',\r
-                                               $this->SeleniumServerPort,\r
-                                               &$errno, &$errstr, 0 );\r
-                                       if ( !$fp ) {\r
-                                               sleep( 1 );\r
-                                               continue;\r
-                                         // Server start succeeded.\r
-                                       } else {\r
-                                               fclose ( $fp );\r
-                                               return 'started';\r
-                                       }\r
-                               }\r
-                               echo ( "Starting Selenium server timed out.\n" );\r
-                               return 'failed';\r
-                       }\r
-                       // server already running.\r
-                       else return 'running';\r
-\r
-               }\r
-                // No Server execution path defined.\r
-               return 'failed';\r
-       }\r
-\r
-       private function startServerOnWindows() {\r
-               // Unimplemented. \r
-               return 'failed';\r
-       }\r
-\r
-       private function stopServerOnUnix() {\r
-\r
-               if ( !empty( $this->SeleniumServerPid ) &&\r
-                    $this->SeleniumServerPid != 'NaN' ) {\r
-                       exec( "kill -9 " . $this->SeleniumServerPid );\r
-                       return 'stopped';\r
-               }\r
-               else return 'failed';\r
-       }\r
-\r
-       private function stopServerOnWindows() {\r
-               // Unimplemented. \r
-               return 'failed';\r
-\r
-       }\r
-}\r
+<?php
+/**
+ * Selenium server manager
+ *
+ * @file
+ * @ingroup Maintenance
+ * Copyright (C) 2010 Dan Nessett <dnessett@yahoo.com>
+ * http://citizendium.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @addtogroup Maintenance
+ */
+
+class SeleniumServerManager {
+       private $SeleniumStartServer = false;
+       private $OS = '';
+       private $SeleniumServerPid = 'NaN';
+       private $SeleniumServerPort = 4444;
+       private $SeleniumServerStartTimeout = 10; // 10 secs.
+       private $SeleniumServerExecPath;
+
+       public function __construct( $startServer,
+                                    $serverPort,
+                                    $serverExecPath ) {
+               $this->OS = (string) PHP_OS;
+               if ( isset( $startServer ) )
+                       $this->SeleniumStartServer = $startServer;
+               if ( isset( $serverPort ) )
+                       $this->SeleniumServerPort = $serverPort;
+               if ( isset( $serverExecPath ) )
+                       $this->SeleniumServerExecPath = $serverExecPath;
+               return;
+       }
+
+       // Getters for certain private attributes. No setters, since they
+       // should not change after the manager object is created.
+
+       public function getSeleniumStartServer() {
+               return $this->SeleniumStartServer;
+       }
+
+       public function getSeleniumServerPort() {
+               return $this->SeleniumServerPort;
+       }
+
+       public function getSeleniumServerPid() {
+               return $this->SeleniumServerPid;
+       }
+
+       // Changing value of SeleniumStartServer allows starting server after
+       // creation of the class instance. Only allow setting SeleniumStartServer
+       // to true, since after server is started, it is shut down by stop().
+
+       public function setSeleniumStartServer( $startServer ) {
+               if ( $startServer == true ) $this->SeleniumStartServer = true;
+       }
+
+       // return values are: 1) started - server started, 2) failed -
+       // server not started, 3) running - instructed to start server, but
+       // server already running
+
+       public function start() {
+
+               if ( !$this->SeleniumStartServer ) return 'failed';
+
+               // commented out cases are untested
+
+               switch ( $this->OS ) {
+                       case "Linux":
+#                      case' CYGWIN_NT-5.1':
+#                      case 'Darwin':
+#                      case 'FreeBSD':
+#                      case 'HP-UX':
+#                      case 'IRIX64':
+#                      case 'NetBSD':
+#                      case 'OpenBSD':
+#                      case 'SunOS':
+#                      case 'Unix':
+                               // *nix based OS
+                               return $this->startServerOnUnix();
+                               break;
+                       case "Windows":
+                       case "WIN32":
+                       case "WINNT":
+                               // Windows
+                               return $this->startServerOnWindows();
+                               break;
+                       default:
+                               // An untested OS
+                               return 'failed';
+                               break;
+               }
+       }
+
+       public function stop() {
+
+               // commented out cases are untested
+
+               switch ( $this->OS ) {
+                       case "Linux":
+#                      case' CYGWIN_NT-5.1':
+#                      case 'Darwin':
+#                      case 'FreeBSD':
+#                      case 'HP-UX':
+#                      case 'IRIX64':
+#                      case 'NetBSD':
+#                      case 'OpenBSD':
+#                      case 'SunOS':
+#                      case 'Unix':
+                               // *nix based OS
+                               return $this->stopServerOnUnix();
+                               break;
+                       case "Windows":
+                       case "WIN32":
+                       case "WINNT":
+                               // Windows
+                               return $this->stopServerOnWindows();
+                               break;
+                       default:
+                               // An untested OS
+                               return 'failed';
+                               break;
+               }
+       }
+
+       private function startServerOnUnix() {
+
+               $output = array();
+                $user = $_ENV['USER'];
+               exec("ps -U " . $user . " w | grep -i selenium-server", $output);
+
+               // Start server. If there is already a server running,
+               // return running.
+
+               if ( isset( $this->SeleniumServerExecPath ) ) {
+                       $found = 0;
+                       foreach ( $output as $string ) {
+                               $found += preg_match(
+                                       '~^(.*)java(.+)-jar(.+)selenium-server~',
+                                       $string );
+                       }
+                       if ( $found == 0 ) {
+
+                               // Didn't find the selenium server. Start it up.
+                               // First set up comamand line suffix.
+                               // NB: $! is pid of last job run in background
+                               // The echo guarentees it is put into $op when
+                               // the exec command is run.
+
+                               $commandSuffix = ' > /dev/null 2>&1'. ' & echo $!';
+                               $portText = ' -port ' . $this->SeleniumServerPort;
+                               $command = "java -jar " . 
+                                       $this->SeleniumServerExecPath .
+                                       $portText . $commandSuffix;
+                               exec($command ,$op); 
+                               $pid = (int)$op[0]; 
+                               if ( $pid != "" )
+                                       $this->SeleniumServerPid = $pid;
+                               else {
+                                       $this->SeleniumServerPid = 'NaN';
+                                       // Server start failed.
+                                       return 'failed';
+                               }
+                               // Wait for the server to startup and listen
+                               // on its port. Note: this solution kinda
+                               // stinks, since it uses a wait loop - dnessett
+
+                               for ( $cnt = 1;
+                                     $cnt <= $this->SeleniumServerStartTimeout;
+                                     $cnt++ ) {
+                                       $fp = @fsockopen ( 'localhost',
+                                               $this->SeleniumServerPort,
+                                               &$errno, &$errstr, 0 );
+                                       if ( !$fp ) {
+                                               sleep( 1 );
+                                               continue;
+                                         // Server start succeeded.
+                                       } else {
+                                               fclose ( $fp );
+                                               return 'started';
+                                       }
+                               }
+                               echo ( "Starting Selenium server timed out.\n" );
+                               return 'failed';
+                       }
+                       // server already running.
+                       else return 'running';
+
+               }
+                // No Server execution path defined.
+               return 'failed';
+       }
+
+       private function startServerOnWindows() {
+               // Unimplemented. 
+               return 'failed';
+       }
+
+       private function stopServerOnUnix() {
+
+               if ( !empty( $this->SeleniumServerPid ) &&
+                    $this->SeleniumServerPid != 'NaN' ) {
+                       exec( "kill -9 " . $this->SeleniumServerPid );
+                       return 'stopped';
+               }
+               else return 'failed';
+       }
+
+       private function stopServerOnWindows() {
+               // Unimplemented. 
+               return 'failed';
+
+       }
+}