]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/tests/selenium/Selenium.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / maintenance / tests / selenium / Selenium.php
1 <?php
2 /**
3  * Selenium connector
4  * This is implemented as a singleton.
5  */
6
7 require( 'Testing/Selenium.php' );
8
9 class Selenium {
10         protected static $_instance = null;
11
12         public $isStarted = false;
13         public $tester;
14
15         protected $port;
16         protected $host;
17         protected $browser;
18         protected $browsers;
19         protected $logger;
20         protected $user;
21         protected $pass;
22         protected $timeout = 30000;
23         protected $verbose;
24         protected $junitlogfile; //processed by phpUnderControl
25         protected $runagainstgrid = false;
26
27         /**
28          * @todo this shouldn't have to be static
29          */
30         static protected $url;
31
32         /**
33          * Override parent
34          */
35         public function __construct() {
36                 /**
37                  * @todo this is an ugly hack to make information available to
38                  * other tests.  It should be fixed.
39                  */
40                 if ( null === self::$_instance ) {
41                         self::$_instance = $this;
42                 } else {
43                         throw new MWException( "Already have one Selenium instance." );
44                 }
45         }
46
47         public function start() {
48                 $this->tester = new Testing_Selenium( $this->browser, self::$url, $this->host,
49                         $this->port, $this->timeout );
50                 if ( method_exists( $this->tester, "setVerbose" ) ) $this->tester->setVerbose( $this->verbose );
51
52                 $this->tester->start();
53                 $this->isStarted = true;
54         }
55
56         public function stop() {
57                 $this->tester->stop();
58                 $this->tester = null;
59                 $this->isStarted = false;
60         }
61
62         public function login() {
63                 if ( strlen( $this->user ) == 0 ) {
64                         return;
65                 }
66                 $this->open( self::$url . '/index.php?title=Special:Userlogin' );
67                 $this->type( 'wpName1', $this->user );
68                 $this->type( 'wpPassword1', $this->pass );
69                 $this->click( "//input[@id='wpLoginAttempt']" );
70                 $this->waitForPageToLoad( 10000 );
71
72                 // after login we redirect to the main page. So check whether the "Prefernces" top menu item exists
73                 $value = $this->isElementPresent( "//li[@id='pt-preferences']" );
74
75                 if ( $value != true ) {
76                         throw new Testing_Selenium_Exception( "Login Failed" );
77                 }
78
79         }
80
81         public static function getInstance() {
82                 if ( null === self::$_instance ) {
83                         throw new MWException( "No instance set yet" );
84                 }
85
86                 return self::$_instance;
87         }
88
89         public function loadPage( $title, $action ) {
90                 $this->open( self::$url . '/index.php?title=' . $title . '&action=' . $action );
91         }
92
93         public function setLogger( $logger ) {
94                 $this->logger = $logger;
95         }
96
97         public function getLogger( ) {
98                 return $this->logger;
99         }
100
101         public function log( $message ) {
102                 $this->logger->write( $message );
103         }
104
105         public function setUrl( $url ) {
106                 self::$url = $url;
107         }
108
109         static public function getUrl() {
110                 return self::$url;
111         }
112
113         public function setPort( $port ) {
114                 $this->port = $port;
115         }
116
117         public function getPort() {
118                 return $this->port;
119         }
120
121         public function setUser( $user ) {
122                 $this->user = $user;
123         }
124
125         // Function to get username
126         public function getUser() {
127                 return $this->user;
128         }
129         
130
131         public function setPass( $pass ) {
132                 $this->pass = $pass;
133         }
134
135     //add function to get password    
136         public function getPass(  ) {
137                 return $this->pass;
138         }
139        
140         
141         public function setHost( $host ) {
142                 $this->host = $host;
143         }
144
145         public function setVerbose( $verbose ) {
146                 $this->verbose = $verbose;
147         }
148
149         public function setAvailableBrowsers( $availableBrowsers ) {
150                 $this->browsers = $availableBrowsers;
151         }
152
153         public function setJUnitLogfile( $junitlogfile ) {
154                 $this->junitlogfile = $junitlogfile;
155         }
156
157         public function getJUnitLogfile( ) {
158                 return $this->junitlogfile;
159         }
160
161         public function setRunAgainstGrid( $runagainstgrid ) {
162                 $this->runagainstgrid = $runagainstgrid;
163         }
164
165         public function setBrowser( $b ) {
166                 if ($this->runagainstgrid) {
167                         $this->browser = $b;
168                         return true;
169                 }
170                 if ( !isset( $this->browsers[$b] ) ) {
171                         throw new MWException( "Invalid Browser: $b.\n" );
172                 }
173
174                 $this->browser = $this->browsers[$b];
175         }
176
177         public function getAvailableBrowsers() {
178                 return $this->browsers;
179         }
180
181         public function __call( $name, $args ) {
182                 $t = call_user_func_array( array( $this->tester, $name ), $args );
183                 return $t;
184         }
185
186         // Prevent external cloning
187         protected function __clone() { }
188         // Prevent external construction
189         // protected function __construct() {}
190 }