]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/tests/RunSeleniumTests.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / maintenance / tests / RunSeleniumTests.php
1 #!/usr/bin/php
2 <?php
3 /**
4  * @file
5  * @ingroup Maintenance
6  * @copyright Copyright © Wikimedia Deuschland, 2009
7  * @author Hallo Welt! Medienwerkstatt GmbH
8  * @author Markus Glaser, Dan Nessett, Priyanka Dhanda
9  * initial idea by Daniel Kinzler
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  * http://www.gnu.org/copyleft/gpl.html
25  */
26
27 define( 'SELENIUMTEST', true );
28
29 require_once( dirname( dirname( __FILE__ ) )."/Maintenance.php" );
30 require_once( 'PHPUnit/Framework.php' );
31 require_once( 'PHPUnit/Extensions/SeleniumTestCase.php' );
32 include_once( 'PHPUnit/Util/Log/JUnit.php' );
33 require_once( dirname( __FILE__ ) . "/selenium/SeleniumServerManager.php" );
34
35 class SeleniumTester extends Maintenance {
36         protected $selenium;
37         protected $serverManager;
38         protected $seleniumServerExecPath;
39
40         public function __construct() {
41                 parent::__construct();
42                 $this->mDescription = "Selenium Test Runner. For documentation, visit http://www.mediawiki.org/wiki/SeleniumFramework";
43                 $this->addOption( 'port', 'Port used by selenium server. Default: 4444', false, true );
44                 $this->addOption( 'host', 'Host selenium server. Default: $wgServer . $wgScriptPath', false, true );
45                 $this->addOption( 'testBrowser', 'The browser used during testing. Default: firefox', false, true );
46                 $this->addOption( 'wikiUrl', 'The Mediawiki installation to point to. Default: http://localhost', false, true );
47                 $this->addOption( 'username', 'The login username for sunning tests. Default: empty', false, true );
48                 $this->addOption( 'userPassword', 'The login password for running tests. Default: empty', false, true );
49                 $this->addOption( 'seleniumConfig', 'Location of the selenium config file. Default: empty', false, true );
50                 $this->addOption( 'list-browsers', 'List the available browsers.' );
51                 $this->addOption( 'verbose', 'Be noisier.' );
52                 $this->addOption( 'startserver', 'Start Selenium Server (on localhost) before the run.' );
53                 $this->addOption( 'stopserver', 'Stop Selenium Server (on localhost) after the run.' );
54                 $this->addOption( 'jUnitLogFile', 'Log results in a specified JUnit log file. Default: empty', false, true );
55                 $this->addOption( 'runAgainstGrid', 'The test will be run against a Selenium Grid. Default: false.', false, true );
56                 $this->deleteOption( 'dbpass' );
57                 $this->deleteOption( 'dbuser' );
58                 $this->deleteOption( 'globals' );
59                 $this->deleteOption( 'wiki' );
60         }
61
62         public function listBrowsers() {
63                 $desc = "Available browsers:\n";
64
65                 foreach ($this->selenium->getAvailableBrowsers() as $k => $v) {
66                         $desc .= "  $k => $v\n";
67                 }
68
69                 echo $desc;
70         }
71
72         protected function startServer() {
73                 if ( $this->seleniumServerExecPath == '' ) {
74                         die ( "The selenium server exec path is not set in " .
75                                   "selenium_settings.ini. Cannot start server \n" .
76                                   "as requested - terminating RunSeleniumTests\n" );
77                 }
78                 $this->serverManager = new SeleniumServerManager( 'true',
79                         $this->selenium->getPort(),
80                         $this->seleniumServerExecPath );
81                 switch ( $this->serverManager->start() ) {
82                         case 'started':
83                                 break;
84                         case 'failed':
85                                 die ( "Unable to start the Selenium Server - " .
86                                         "terminating RunSeleniumTests\n" );
87                         case 'running':
88                                 echo ( "Warning: The Selenium Server is " .
89                                         "already running\n" );
90                                 break;
91                 }
92
93                 return;
94         }
95
96         protected function stopServer() {
97                 if ( !isset ( $this->serverManager ) ) {
98                         echo ( "Warning: Request to stop Selenium Server, but it was " .
99                                 "not stared by RunSeleniumTests\n" .
100                                 "RunSeleniumTests cannot stop a Selenium Server it " .
101                                 "did not start\n" );
102                 } else {
103                         switch ( $this->serverManager->stop() ) {
104                                 case 'stopped':
105                                         break;
106                                 case 'failed':
107                                         echo ( "unable to stop the Selenium Server\n" );
108                         }
109                 }
110                 return;
111         }
112
113         protected function runTests( $seleniumTestSuites = array() ) {
114                 $result = new PHPUnit_Framework_TestResult;
115                 $result->addListener( new SeleniumTestListener( $this->selenium->getLogger() ) );
116                 if ( $this->selenium->getJUnitLogFile() ) {
117                         $jUnitListener = new PHPUnit_Util_Log_JUnit( $this->selenium->getJUnitLogFile(), true );
118                         $result->addListener( $jUnitListener );
119                 }
120
121                 foreach ( $seleniumTestSuites as $testSuiteName => $testSuiteFile ) {
122                         require( $testSuiteFile );
123                         $suite = new $testSuiteName();
124                         $suite->setName( $testSuiteName );
125                         $suite->addTests();
126
127                         try {
128                                 $suite->run( $result );
129                         } catch ( Testing_Selenium_Exception $e ) {
130                                 $suite->tearDown();
131                                 throw new MWException( $e->getMessage() );
132                         }
133                 }
134
135                 if ( $this->selenium->getJUnitLogFile() ) {
136                         $jUnitListener->flush();
137                 }
138         }
139
140         public function execute() {
141                 global $wgServer, $wgScriptPath, $wgHooks;
142
143                 $seleniumSettings = array();
144                 $seleniumBrowsers = array();
145                 $seleniumTestSuites = array();
146
147                 $configFile = $this->getOption( 'seleniumConfig', '' );
148                 if ( strlen( $configFile ) > 0 ) {
149                         $this->output("Using Selenium Configuration file: " . $configFile . "\n");
150                         SeleniumConfig::getSeleniumSettings( $seleniumSettings,
151                                 $seleniumBrowsers,
152                                 $seleniumTestSuites,
153                                 $configFile );
154                 } else if ( !isset( $wgHooks['SeleniumSettings'] ) ) {
155                         $this->output("No command line configuration file or configuration hook found.\n");
156                         SeleniumConfig::getSeleniumSettings( $seleniumSettings,
157                                 $seleniumBrowsers,
158                                 $seleniumTestSuites
159                                                                                                         );
160                 } else {
161                         $this->output("Using 'SeleniumSettings' hook for configuration.\n");
162                         wfRunHooks('SeleniumSettings', array( $seleniumSettings,
163                                 $seleniumBrowsers,
164                                 $seleniumTestSuites ) );
165                 }
166
167                 // State for starting/stopping the Selenium server has nothing to do with the Selenium
168                 // class. Keep this state local to SeleniumTester class. Using getOption() is clumsy, but
169                 // the Maintenance class does not have a setOption()
170                 if ( isset( $seleniumSettings['startserver'] ) ) $this->getOption( 'startserver', true );
171                 if ( isset( $seleniumSettings['stopserver'] ) ) $this->getOption( 'stopserver', true );
172                 if ( !isset( $seleniumSettings['seleniumserverexecpath'] ) ) $seleniumSettings['seleniumserverexecpath'] = '';
173                 $this->seleniumServerExecPath = $seleniumSettings['seleniumserverexecpath'];
174
175                 //set reasonable defaults if we did not find the settings
176                 if ( !isset( $seleniumBrowsers ) ) $seleniumBrowsers = array ('firefox' => '*firefox');
177                 if ( !isset( $seleniumSettings['host'] ) ) $seleniumSettings['host'] = $wgServer . $wgScriptPath;
178                 if ( !isset( $seleniumSettings['port'] ) ) $seleniumSettings['port'] = '4444';
179                 if ( !isset( $seleniumSettings['wikiUrl'] ) ) $seleniumSettings['wikiUrl'] = 'http://localhost';
180                 if ( !isset( $seleniumSettings['username'] ) ) $seleniumSettings['username'] = '';
181                 if ( !isset( $seleniumSettings['userPassword'] ) ) $seleniumSettings['userPassword'] = '';
182                 if ( !isset( $seleniumSettings['testBrowser'] ) ) $seleniumSettings['testBrowser'] = 'firefox';
183                 if ( !isset( $seleniumSettings['jUnitLogFile'] ) ) $seleniumSettings['jUnitLogFile'] = false;
184                 if ( !isset( $seleniumSettings['runAgainstGrid'] ) ) $seleniumSettings['runAgainstGrid'] = false;
185
186                 // Setup Selenium class
187                 $this->selenium = new Selenium( );
188                 $this->selenium->setAvailableBrowsers( $seleniumBrowsers );
189                 $this->selenium->setRunAgainstGrid( $this->getOption( 'runAgainstGrid', $seleniumSettings['runAgainstGrid'] ) );
190                 $this->selenium->setUrl( $this->getOption( 'wikiUrl', $seleniumSettings['wikiUrl'] ) );
191                 $this->selenium->setBrowser( $this->getOption( 'testBrowser', $seleniumSettings['testBrowser'] ) );
192                 $this->selenium->setPort( $this->getOption( 'port', $seleniumSettings['port'] ) );
193                 $this->selenium->setHost( $this->getOption( 'host', $seleniumSettings['host'] ) );
194                 $this->selenium->setUser( $this->getOption( 'username', $seleniumSettings['username'] ) );
195                 $this->selenium->setPass( $this->getOption( 'userPassword', $seleniumSettings['userPassword'] ) );
196                 $this->selenium->setVerbose( $this->hasOption( 'verbose' ) );
197                 $this->selenium->setJUnitLogFile( $this->getOption( 'jUnitLogFile', $seleniumSettings['jUnitLogFile'] ) );
198
199                 if( $this->hasOption( 'list-browsers' ) ) {
200                         $this->listBrowsers();
201                         exit(0);
202                 }
203                 if ( $this->hasOption( 'startserver' ) ) {
204                         $this->startServer();
205                 }
206
207                 $logger = new SeleniumTestConsoleLogger;
208                 $this->selenium->setLogger( $logger );
209
210                 $this->runTests( $seleniumTestSuites );
211
212                 if ( $this->hasOption( 'stopserver' )  ) {
213                         $this->stopServer();
214                 }
215         }
216 }
217
218 $maintClass = "SeleniumTester";
219
220 require_once( DO_MAINTENANCE );