MediaWiki  REL1_19
RunSeleniumTests.php
Go to the documentation of this file.
00001 #!/usr/bin/php
00002 <?php
00027 $IP = dirname( dirname( __FILE__ ) );
00028 
00029 define( 'SELENIUMTEST', true );
00030 
00031 //require_once( dirname( __FILE__ ) . '/../maintenance/commandLine.inc' );
00032 require( dirname( __FILE__ ) . '/../maintenance/Maintenance.php' );
00033 
00034 require_once( 'PHPUnit/Runner/Version.php' );
00035 if( version_compare( PHPUnit_Runner_Version::id(), '3.5.0', '>=' ) ) {
00036         # PHPUnit 3.5.0 introduced a nice autoloader based on class name
00037         require_once( 'PHPUnit/Autoload.php' );
00038 } else {
00039         # Keep the old pre PHPUnit 3.5.0 behaviour for compatibility
00040         require_once( 'PHPUnit/TextUI/Command.php' );
00041 }
00042 
00043 require_once( 'PHPUnit/Extensions/SeleniumTestCase.php' );
00044 include_once( 'PHPUnit/Util/Log/JUnit.php' );
00045 
00046 require_once( dirname( __FILE__ ) . "/selenium/SeleniumServerManager.php" );
00047 
00048 class SeleniumTester extends Maintenance {
00049         protected $selenium;
00050         protected $serverManager;
00051         protected $seleniumServerExecPath;
00052 
00053         public function __construct() {
00054                 parent::__construct();
00055                 $this->mDescription = "Selenium Test Runner. For documentation, visit http://www.mediawiki.org/wiki/SeleniumFramework";
00056                 $this->addOption( 'port', 'Port used by selenium server. Default: 4444', false, true );
00057                 $this->addOption( 'host', 'Host selenium server. Default: $wgServer . $wgScriptPath', false, true );
00058                 $this->addOption( 'testBrowser', 'The browser used during testing. Default: firefox', false, true );
00059                 $this->addOption( 'wikiUrl', 'The Mediawiki installation to point to. Default: http://localhost', false, true );
00060                 $this->addOption( 'username', 'The login username for sunning tests. Default: empty', false, true );
00061                 $this->addOption( 'userPassword', 'The login password for running tests. Default: empty', false, true );
00062                 $this->addOption( 'seleniumConfig', 'Location of the selenium config file. Default: empty', false, true );
00063                 $this->addOption( 'list-browsers', 'List the available browsers.' );
00064                 $this->addOption( 'verbose', 'Be noisier.' );
00065                 $this->addOption( 'startserver', 'Start Selenium Server (on localhost) before the run.' );
00066                 $this->addOption( 'stopserver', 'Stop Selenium Server (on localhost) after the run.' );
00067                 $this->addOption( 'jUnitLogFile', 'Log results in a specified JUnit log file. Default: empty', false, true );
00068                 $this->addOption( 'runAgainstGrid', 'The test will be run against a Selenium Grid. Default: false.', false, true );
00069                 $this->deleteOption( 'dbpass' );
00070                 $this->deleteOption( 'dbuser' );
00071                 $this->deleteOption( 'globals' );
00072                 $this->deleteOption( 'wiki' );
00073         }
00074 
00075         public function listBrowsers() {
00076                 $desc = "Available browsers:\n";
00077 
00078                 foreach ($this->selenium->getAvailableBrowsers() as $k => $v) {
00079                         $desc .= "  $k => $v\n";
00080                 }
00081 
00082                 echo $desc;
00083         }
00084 
00085         protected function startServer() {
00086                 if ( $this->seleniumServerExecPath == '' ) {
00087                         die ( "The selenium server exec path is not set in " .
00088                                   "selenium_settings.ini. Cannot start server \n" .
00089                                   "as requested - terminating RunSeleniumTests\n" );
00090                 }
00091                 $this->serverManager = new SeleniumServerManager( 'true',
00092                         $this->selenium->getPort(),
00093                         $this->seleniumServerExecPath );
00094                 switch ( $this->serverManager->start() ) {
00095                         case 'started':
00096                                 break;
00097                         case 'failed':
00098                                 die ( "Unable to start the Selenium Server - " .
00099                                         "terminating RunSeleniumTests\n" );
00100                         case 'running':
00101                                 echo ( "Warning: The Selenium Server is " .
00102                                         "already running\n" );
00103                                 break;
00104                 }
00105 
00106                 return;
00107         }
00108 
00109         protected function stopServer() {
00110                 if ( !isset ( $this->serverManager ) ) {
00111                         echo ( "Warning: Request to stop Selenium Server, but it was " .
00112                                 "not stared by RunSeleniumTests\n" .
00113                                 "RunSeleniumTests cannot stop a Selenium Server it " .
00114                                 "did not start\n" );
00115                 } else {
00116                         switch ( $this->serverManager->stop() ) {
00117                                 case 'stopped':
00118                                         break;
00119                                 case 'failed':
00120                                         echo ( "unable to stop the Selenium Server\n" );
00121                         }
00122                 }
00123                 return;
00124         }
00125 
00126         protected function runTests( $seleniumTestSuites = array() ) {
00127                 $result = new PHPUnit_Framework_TestResult;
00128                 $result->addListener( new SeleniumTestListener( $this->selenium->getLogger() ) );
00129                 if ( $this->selenium->getJUnitLogFile() ) {
00130                         $jUnitListener = new PHPUnit_Util_Log_JUnit( $this->selenium->getJUnitLogFile(), true );
00131                         $result->addListener( $jUnitListener );
00132                 }
00133 
00134                 foreach ( $seleniumTestSuites as $testSuiteName => $testSuiteFile ) {
00135                         require( $testSuiteFile );
00136                         $suite = new $testSuiteName();
00137                         $suite->setName( $testSuiteName );
00138                         $suite->addTests();
00139 
00140                         try {
00141                                 $suite->run( $result );
00142                         } catch ( Testing_Selenium_Exception $e ) {
00143                                 $suite->tearDown();
00144                                 throw new MWException( $e->getMessage() );
00145                         }
00146                 }
00147 
00148                 if ( $this->selenium->getJUnitLogFile() ) {
00149                         $jUnitListener->flush();
00150                 }
00151         }
00152 
00153         public function execute() {
00154                 global $wgServer, $wgScriptPath, $wgHooks;
00155 
00156                 $seleniumSettings = array();
00157                 $seleniumBrowsers = array();
00158                 $seleniumTestSuites = array();
00159 
00160                 $configFile = $this->getOption( 'seleniumConfig', '' );
00161                 if ( strlen( $configFile ) > 0 ) {
00162                         $this->output("Using Selenium Configuration file: " . $configFile . "\n");
00163                         SeleniumConfig::getSeleniumSettings( $seleniumSettings,
00164                                 $seleniumBrowsers,
00165                                 $seleniumTestSuites,
00166                                 $configFile );
00167                 } elseif ( !isset( $wgHooks['SeleniumSettings'] ) ) {
00168                         $this->output("No command line, configuration file or configuration hook found.\n");
00169                         SeleniumConfig::getSeleniumSettings( $seleniumSettings,
00170                                 $seleniumBrowsers,
00171                                 $seleniumTestSuites
00172                                                                                                         );
00173                 } else {
00174                         $this->output("Using 'SeleniumSettings' hook for configuration.\n");
00175                         wfRunHooks('SeleniumSettings', array( $seleniumSettings,
00176                                 $seleniumBrowsers,
00177                                 $seleniumTestSuites ) );
00178                 }
00179 
00180                 // State for starting/stopping the Selenium server has nothing to do with the Selenium
00181                 // class. Keep this state local to SeleniumTester class. Using getOption() is clumsy, but
00182                 // the Maintenance class does not have a setOption()
00183                 if ( ! isset( $seleniumSettings['startserver'] ) ) $this->getOption( 'startserver', true );
00184                 if ( ! isset( $seleniumSettings['stopserver'] ) ) $this->getOption( 'stopserver', true );
00185                 if ( !isset( $seleniumSettings['seleniumserverexecpath'] ) ) $seleniumSettings['seleniumserverexecpath'] = '';
00186                 $this->seleniumServerExecPath = $seleniumSettings['seleniumserverexecpath'];
00187 
00188                 //set reasonable defaults if we did not find the settings
00189                 if ( !isset( $seleniumBrowsers ) ) $seleniumBrowsers = array ('firefox' => '*firefox');
00190                 if ( !isset( $seleniumSettings['host'] ) ) $seleniumSettings['host'] = $wgServer . $wgScriptPath;
00191                 if ( !isset( $seleniumSettings['port'] ) ) $seleniumSettings['port'] = '4444';
00192                 if ( !isset( $seleniumSettings['wikiUrl'] ) ) $seleniumSettings['wikiUrl'] = 'http://localhost';
00193                 if ( !isset( $seleniumSettings['username'] ) ) $seleniumSettings['username'] = '';
00194                 if ( !isset( $seleniumSettings['userPassword'] ) ) $seleniumSettings['userPassword'] = '';
00195                 if ( !isset( $seleniumSettings['testBrowser'] ) ) $seleniumSettings['testBrowser'] = 'firefox';
00196                 if ( !isset( $seleniumSettings['jUnitLogFile'] ) ) $seleniumSettings['jUnitLogFile'] = false;
00197                 if ( !isset( $seleniumSettings['runAgainstGrid'] ) ) $seleniumSettings['runAgainstGrid'] = false;
00198 
00199                 // Setup Selenium class
00200                 $this->selenium = new Selenium( );
00201                 $this->selenium->setAvailableBrowsers( $seleniumBrowsers );
00202                 $this->selenium->setRunAgainstGrid( $this->getOption( 'runAgainstGrid', $seleniumSettings['runAgainstGrid'] ) );
00203                 $this->selenium->setUrl( $this->getOption( 'wikiUrl', $seleniumSettings['wikiUrl'] ) );
00204                 $this->selenium->setBrowser( $this->getOption( 'testBrowser', $seleniumSettings['testBrowser'] ) );
00205                 $this->selenium->setPort( $this->getOption( 'port', $seleniumSettings['port'] ) );
00206                 $this->selenium->setHost( $this->getOption( 'host', $seleniumSettings['host'] ) );
00207                 $this->selenium->setUser( $this->getOption( 'username', $seleniumSettings['username'] ) );
00208                 $this->selenium->setPass( $this->getOption( 'userPassword', $seleniumSettings['userPassword'] ) );
00209                 $this->selenium->setVerbose( $this->hasOption( 'verbose' ) );
00210                 $this->selenium->setJUnitLogFile( $this->getOption( 'jUnitLogFile', $seleniumSettings['jUnitLogFile'] ) );
00211 
00212                 if( $this->hasOption( 'list-browsers' ) ) {
00213                         $this->listBrowsers();
00214                         exit(0);
00215                 }
00216                 if ( $this->hasOption( 'startserver' ) ) {
00217                         $this->startServer();
00218                 }
00219 
00220                 $logger = new SeleniumTestConsoleLogger;
00221                 $this->selenium->setLogger( $logger );
00222 
00223                 $this->runTests( $seleniumTestSuites );
00224 
00225                 if ( $this->hasOption( 'stopserver' )  ) {
00226                         $this->stopServer();
00227                 }
00228         }
00229 }
00230 
00231 $maintClass = "SeleniumTester";
00232 
00233 require_once( RUN_MAINTENANCE_IF_MAIN );