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