And while I'm a it:
[mediawiki.git] / maintenance / tests / RunSeleniumTests.php
blob261a0405ccb5b7f5f1a3eca3250e63031bd7e4e6
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
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 define( 'SELENIUMTEST', true );
28 require_once( dirname( dirname( __FILE__ ) )."/Maintenance.php" );
29 require_once( 'PHPUnit/Framework.php' );
30 require_once( 'PHPUnit/Extensions/SeleniumTestCase.php' );
33 class SeleniumTester extends Maintenance {
34 protected $selenium;
36 public function __construct() {
37 parent::__construct();
38 $this->mDescription = "Selenium Test Runner. For documentation, visit http://www.mediawiki.org/wiki/SeleniumFramework";
39 $this->addOption( 'port', 'Port used by selenium server. Default: 4444' );
40 $this->addOption( 'host', 'Host selenium server. Default: $wgServer . $wgScriptPath' );
41 $this->addOption( 'testBrowser', 'The browser he used during testing. Default: firefox' );
42 $this->addOption( 'wikiUrl', 'The Mediawiki installation to point to. Default: http://localhost' );
43 $this->addOption( 'username', 'The login username for sunning tests. Default: empty' );
44 $this->addOption( 'userPassword', 'The login password for running tests. Default: empty' );
45 $this->addOption( 'seleniumConfig', 'Location of the selenium config file. Default: empty' );
46 $this->addOption( 'list-browsers', 'List the available browsers.' );
47 $this->addOption( 'verbose', 'Be noisier.' );
49 $this->deleteOption( 'dbpass' );
50 $this->deleteOption( 'dbuser' );
51 $this->deleteOption( 'globals' );
52 $this->deleteOption( 'wiki' );
55 public function listBrowsers() {
56 $desc = "Available browsers:\n";
58 foreach ($this->selenium->getAvailableBrowsers() as $k => $v) {
59 $desc .= " $k => $v\n";
62 echo $desc;
65 protected function runTests( $seleniumTestSuites = array() ) {
66 $result = new PHPUnit_Framework_TestResult;
67 $result->addListener( new SeleniumTestListener( $this->selenium->getLogger() ) );
69 foreach ( $seleniumTestSuites as $testSuiteName => $testSuiteFile ) {
70 require( $testSuiteFile );
71 $suite = new $testSuiteName();
72 $suite->addTests();
74 try {
75 $suite->run( $result );
76 } catch ( Testing_Selenium_Exception $e ) {
77 $suite->tearDown();
78 throw new MWException( $e->getMessage() );
83 public function execute() {
84 global $wgServer, $wgScriptPath, $wgHooks;
86 $seleniumSettings;
87 $seleniumBrowsers;
88 $seleniumTestSuites;
90 $configFile = $this->getOption( 'seleniumConfig', '' );
91 if ( strlen( $configFile ) > 0 ) {
92 $this->output("Using Selenium Configuration file: " . $configFile . "\n");
93 SeleniumConfig::getSeleniumSettings( $seleniumSettings,
94 $seleniumBrowsers,
95 $seleniumTestSuites,
96 $configFile );
97 } else if ( !isset( $wgHooks['SeleniumSettings'] ) ) {
98 $this->output("No command line configuration file or configuration hook found.\n");
99 SeleniumConfig::getSeleniumSettings( $seleniumSettings,
100 $seleniumBrowsers,
101 $seleniumTestSuites
103 } else {
104 $this->output("Using 'SeleniumSettings' hook for configuration.\n");
105 wfRunHooks('SeleniumSettings', array( $seleniumSettings,
106 $seleniumBrowsers,
107 $seleniumTestSuites ) );
111 //set reasonable defaults if we did not find the settings
112 if ( !isset( $seleniumBrowsers ) ) $seleniumBrowsers = array ('firefox' => '*firefox');
113 if ( !isset( $seleniumSettings['host'] ) ) $seleniumSettings['host'] = $wgServer . $wgScriptPath;
114 if ( !isset( $seleniumSettings['port'] ) ) $seleniumSettings['port'] = '4444';
115 if ( !isset( $seleniumSettings['wikiUrl'] ) ) $seleniumSettings['wikiUrl'] = 'http://localhost';
116 if ( !isset( $seleniumSettings['username'] ) ) $seleniumSettings['username'] = '';
117 if ( !isset( $seleniumSettings['userPassword'] ) ) $seleniumSettings['userPassword'] = '';
118 if ( !isset( $seleniumSettings['testBrowser'] ) ) $seleniumSettings['testBrowser'] = 'firefox';
120 $this->selenium = new Selenium( );
121 $this->selenium->setAvailableBrowsers( $seleniumBrowsers );
122 $this->selenium->setUrl( $this->getOption( 'wikiUrl', $seleniumSettings['wikiUrl'] ) );
123 $this->selenium->setBrowser( $this->getOption( 'testBrowser', $seleniumSettings['testBrowser'] ) );
124 $this->selenium->setPort( $this->getOption( 'port', $seleniumSettings['port'] ) );
125 $this->selenium->setHost( $this->getOption( 'host', $seleniumSettings['host'] ) );
126 $this->selenium->setUser( $this->getOption( 'username', $seleniumSettings['username'] ) );
127 $this->selenium->setPass( $this->getOption( 'userPassword', $seleniumSettings['userPassword'] ) );
128 $this->selenium->setVerbose( $this->hasOption( 'verbose' ) );
130 if( $this->hasOption( 'list-browsers' ) ) {
131 $this->listBrowsers();
132 exit(0);
135 $logger = new SeleniumTestConsoleLogger;
136 $this->selenium->setLogger( $logger );
138 $this->runTests( $seleniumTestSuites );
142 $maintClass = "SeleniumTester";
144 require_once( DO_MAINTENANCE );