Merge "properly stop output buffering"
[lhc/web/wiklou.git] / 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 $IP = dirname( __DIR__ );
28
29 define( 'SELENIUMTEST', true );
30
31 //require_once( __DIR__ . '/../maintenance/commandLine.inc' );
32 require( __DIR__ . '/../maintenance/Maintenance.php' );
33
34 require_once( 'PHPUnit/Runner/Version.php' );
35 if ( version_compare( PHPUnit_Runner_Version::id(), '3.5.0', '>=' ) ) {
36 # PHPUnit 3.5.0 introduced a nice autoloader based on class name
37 require_once( 'PHPUnit/Autoload.php' );
38 } else {
39 # Keep the old pre PHPUnit 3.5.0 behaviour for compatibility
40 require_once( 'PHPUnit/TextUI/Command.php' );
41 }
42
43 require_once( 'PHPUnit/Extensions/SeleniumTestCase.php' );
44 include_once( 'PHPUnit/Util/Log/JUnit.php' );
45
46 require_once( __DIR__ . "/selenium/SeleniumServerManager.php" );
47
48 class SeleniumTester extends Maintenance {
49 protected $selenium;
50 protected $serverManager;
51 protected $seleniumServerExecPath;
52
53 public function __construct() {
54 parent::__construct();
55 $this->mDescription = "Selenium Test Runner. For documentation, visit http://www.mediawiki.org/wiki/SeleniumFramework";
56 $this->addOption( 'port', 'Port used by selenium server. Default: 4444', false, true );
57 $this->addOption( 'host', 'Host selenium server. Default: $wgServer . $wgScriptPath', false, true );
58 $this->addOption( 'testBrowser', 'The browser used during testing. Default: firefox', false, true );
59 $this->addOption( 'wikiUrl', 'The Mediawiki installation to point to. Default: http://localhost', false, true );
60 $this->addOption( 'username', 'The login username for sunning tests. Default: empty', false, true );
61 $this->addOption( 'userPassword', 'The login password for running tests. Default: empty', false, true );
62 $this->addOption( 'seleniumConfig', 'Location of the selenium config file. Default: empty', false, true );
63 $this->addOption( 'list-browsers', 'List the available browsers.' );
64 $this->addOption( 'verbose', 'Be noisier.' );
65 $this->addOption( 'startserver', 'Start Selenium Server (on localhost) before the run.' );
66 $this->addOption( 'stopserver', 'Stop Selenium Server (on localhost) after the run.' );
67 $this->addOption( 'jUnitLogFile', 'Log results in a specified JUnit log file. Default: empty', false, true );
68 $this->addOption( 'runAgainstGrid', 'The test will be run against a Selenium Grid. Default: false.', false, true );
69 $this->deleteOption( 'dbpass' );
70 $this->deleteOption( 'dbuser' );
71 $this->deleteOption( 'globals' );
72 $this->deleteOption( 'wiki' );
73 }
74
75 public function listBrowsers() {
76 $desc = "Available browsers:\n";
77
78 foreach ( $this->selenium->getAvailableBrowsers() as $k => $v ) {
79 $desc .= " $k => $v\n";
80 }
81
82 echo $desc;
83 }
84
85 protected function startServer() {
86 if ( $this->seleniumServerExecPath == '' ) {
87 die ( "The selenium server exec path is not set in " .
88 "selenium_settings.ini. Cannot start server \n" .
89 "as requested - terminating RunSeleniumTests\n" );
90 }
91 $this->serverManager = new SeleniumServerManager( 'true',
92 $this->selenium->getPort(),
93 $this->seleniumServerExecPath );
94 switch ( $this->serverManager->start() ) {
95 case 'started':
96 break;
97 case 'failed':
98 die ( "Unable to start the Selenium Server - " .
99 "terminating RunSeleniumTests\n" );
100 case 'running':
101 echo ( "Warning: The Selenium Server is " .
102 "already running\n" );
103 break;
104 }
105
106 return;
107 }
108
109 protected function stopServer() {
110 if ( !isset ( $this->serverManager ) ) {
111 echo ( "Warning: Request to stop Selenium Server, but it was " .
112 "not stared by RunSeleniumTests\n" .
113 "RunSeleniumTests cannot stop a Selenium Server it " .
114 "did not start\n" );
115 } else {
116 switch ( $this->serverManager->stop() ) {
117 case 'stopped':
118 break;
119 case 'failed':
120 echo ( "unable to stop the Selenium Server\n" );
121 }
122 }
123 return;
124 }
125
126 protected function runTests( $seleniumTestSuites = array() ) {
127 $result = new PHPUnit_Framework_TestResult;
128 $result->addListener( new SeleniumTestListener( $this->selenium->getLogger() ) );
129 if ( $this->selenium->getJUnitLogFile() ) {
130 $jUnitListener = new PHPUnit_Util_Log_JUnit( $this->selenium->getJUnitLogFile(), true );
131 $result->addListener( $jUnitListener );
132 }
133
134 foreach ( $seleniumTestSuites as $testSuiteName => $testSuiteFile ) {
135 require( $testSuiteFile );
136 $suite = new $testSuiteName();
137 $suite->setName( $testSuiteName );
138 $suite->addTests();
139
140 try {
141 $suite->run( $result );
142 } catch ( Testing_Selenium_Exception $e ) {
143 $suite->tearDown();
144 throw new MWException( $e->getMessage() );
145 }
146 }
147
148 if ( $this->selenium->getJUnitLogFile() ) {
149 $jUnitListener->flush();
150 }
151 }
152
153 public function execute() {
154 global $wgServer, $wgScriptPath, $wgHooks;
155
156 $seleniumSettings = array();
157 $seleniumBrowsers = array();
158 $seleniumTestSuites = array();
159
160 $configFile = $this->getOption( 'seleniumConfig', '' );
161 if ( strlen( $configFile ) > 0 ) {
162 $this->output( "Using Selenium Configuration file: " . $configFile . "\n" );
163 SeleniumConfig::getSeleniumSettings( $seleniumSettings,
164 $seleniumBrowsers,
165 $seleniumTestSuites,
166 $configFile );
167 } elseif ( !isset( $wgHooks['SeleniumSettings'] ) ) {
168 $this->output( "No command line, configuration file or configuration hook found.\n" );
169 SeleniumConfig::getSeleniumSettings( $seleniumSettings,
170 $seleniumBrowsers,
171 $seleniumTestSuites
172 );
173 } else {
174 $this->output( "Using 'SeleniumSettings' hook for configuration.\n" );
175 wfRunHooks( 'SeleniumSettings', array( $seleniumSettings,
176 $seleniumBrowsers,
177 $seleniumTestSuites ) );
178 }
179
180 // State for starting/stopping the Selenium server has nothing to do with the Selenium
181 // class. Keep this state local to SeleniumTester class. Using getOption() is clumsy, but
182 // the Maintenance class does not have a setOption()
183 if ( !isset( $seleniumSettings['startserver'] ) ) {
184 $this->getOption( 'startserver', true );
185 }
186 if ( !isset( $seleniumSettings['stopserver'] ) ) {
187 $this->getOption( 'stopserver', true );
188 }
189 if ( !isset( $seleniumSettings['seleniumserverexecpath'] ) ) {
190 $seleniumSettings['seleniumserverexecpath'] = '';
191 }
192 $this->seleniumServerExecPath = $seleniumSettings['seleniumserverexecpath'];
193
194 //set reasonable defaults if we did not find the settings
195 if ( !isset( $seleniumBrowsers ) ) {
196 $seleniumBrowsers = array( 'firefox' => '*firefox' );
197 }
198 if ( !isset( $seleniumSettings['host'] ) ) {
199 $seleniumSettings['host'] = $wgServer . $wgScriptPath;
200 }
201 if ( !isset( $seleniumSettings['port'] ) ) {
202 $seleniumSettings['port'] = '4444';
203 }
204 if ( !isset( $seleniumSettings['wikiUrl'] ) ) {
205 $seleniumSettings['wikiUrl'] = 'http://localhost';
206 }
207 if ( !isset( $seleniumSettings['username'] ) ) {
208 $seleniumSettings['username'] = '';
209 }
210 if ( !isset( $seleniumSettings['userPassword'] ) ) {
211 $seleniumSettings['userPassword'] = '';
212 }
213 if ( !isset( $seleniumSettings['testBrowser'] ) ) {
214 $seleniumSettings['testBrowser'] = 'firefox';
215 }
216 if ( !isset( $seleniumSettings['jUnitLogFile'] ) ) {
217 $seleniumSettings['jUnitLogFile'] = false;
218 }
219 if ( !isset( $seleniumSettings['runAgainstGrid'] ) ) {
220 $seleniumSettings['runAgainstGrid'] = false;
221 }
222
223 // Setup Selenium class
224 $this->selenium = new Selenium();
225 $this->selenium->setAvailableBrowsers( $seleniumBrowsers );
226 $this->selenium->setRunAgainstGrid( $this->getOption( 'runAgainstGrid', $seleniumSettings['runAgainstGrid'] ) );
227 $this->selenium->setUrl( $this->getOption( 'wikiUrl', $seleniumSettings['wikiUrl'] ) );
228 $this->selenium->setBrowser( $this->getOption( 'testBrowser', $seleniumSettings['testBrowser'] ) );
229 $this->selenium->setPort( $this->getOption( 'port', $seleniumSettings['port'] ) );
230 $this->selenium->setHost( $this->getOption( 'host', $seleniumSettings['host'] ) );
231 $this->selenium->setUser( $this->getOption( 'username', $seleniumSettings['username'] ) );
232 $this->selenium->setPass( $this->getOption( 'userPassword', $seleniumSettings['userPassword'] ) );
233 $this->selenium->setVerbose( $this->hasOption( 'verbose' ) );
234 $this->selenium->setJUnitLogFile( $this->getOption( 'jUnitLogFile', $seleniumSettings['jUnitLogFile'] ) );
235
236 if ( $this->hasOption( 'list-browsers' ) ) {
237 $this->listBrowsers();
238 exit( 0 );
239 }
240 if ( $this->hasOption( 'startserver' ) ) {
241 $this->startServer();
242 }
243
244 $logger = new SeleniumTestConsoleLogger;
245 $this->selenium->setLogger( $logger );
246
247 $this->runTests( $seleniumTestSuites );
248
249 if ( $this->hasOption( 'stopserver' ) ) {
250 $this->stopServer();
251 }
252 }
253 }
254
255 $maintClass = "SeleniumTester";
256
257 require_once( RUN_MAINTENANCE_IF_MAIN );