Add changes that I missed in r72340
[lhc/web/wiklou.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 class Selenium {
9 protected static $_instance = null;
10 public $isStarted = false;
11 public $tester;
12
13 protected $port;
14 protected $host;
15 protected $browser;
16 protected $browsers;
17 protected $logger;
18 protected $user;
19 protected $pass;
20 protected $timeout = 30000;
21 protected $verbose;
22
23 /**
24 * @todo this shouldn't have to be static
25 */
26 static protected $url;
27
28 /**
29 * Override parent
30 */
31 public function __construct() {
32 /**
33 * @todo this is an ugly hack to make information available to
34 * other tests. It should be fixed.
35 */
36 if ( null === self::$_instance ) {
37 self::$_instance = $this;
38 } else {
39 throw new MWException("Already have one Selenium instance.");
40 }
41 }
42
43 public function start() {
44 $this->tester = new Testing_Selenium( $this->browser, self::$url, $this->host,
45 $this->port, $this->timeout );
46 if ( method_exists( $this->tester, "setVerbose" ) ) $this->tester->setVerbose( $this->verbose );
47
48 $this->tester->start();
49 $this->isStarted = true;
50 }
51
52 public function stop() {
53 $this->tester->stop();
54 $this->tester = null;
55 $this->isStarted = false;
56 }
57
58 protected function setupBrowsers() {
59 /**
60 * @todo This needs to be replaced with something not hard
61 * coded. This would be entries in a .ini file or
62 * screen-scraping
63 * http://grid.tesla.usability.wikimedia.org:4444/console for
64 * example.
65 */
66 return array(
67 'firefox' => 'Firefox 3.5 on Linux',
68 'iexplorer' => '*iexploreproxy',
69 'chrome' => '*googlechrome',
70 );
71 }
72
73 public function login() {
74 $this->open( self::$url . '/index.php?title=Special:Userlogin' );
75 $this->type( 'wpName1', $this->user );
76 $this->type( 'wpPassword1', $this->pass );
77 $this->click( "//input[@id='wpLoginAttempt']" );
78 $this->waitForPageToLoad(5000);
79 //after login we redirect to the main page. So check whether the "Prefernces" top menu item exists
80 $value = $this->isElementPresent( "//li[@id='pt-preferences']" );
81 if ( $value != true ) {
82 throw new Testing_Selenium_Exception( "Login Failed" );
83 }
84
85 }
86
87 public static function getInstance() {
88 if ( null === self::$_instance ) {
89 throw new MWException( "No instance set yet" );
90 }
91
92 return self::$_instance;
93 }
94
95 public function loadPage( $title, $action ) {
96 $this->open( self::$url . '/index.php?title=' . $title . '&action=' . $action );
97 }
98
99 public function setLogger( $logger ) {
100 $this->logger = $logger;
101 }
102
103 public function getLogger( ) {
104 return $this->logger;
105 }
106
107 public function log( $message ) {
108 $this->logger->write( $message );
109 }
110
111 public function setUrl( $url ) {
112 self::$url = $url;
113 }
114
115 static public function getUrl() {
116 return self::$url;
117 }
118
119 public function setPort( $port ) {
120 $this->port = $port;
121 }
122
123 public function setUser( $user ) {
124 $this->user = $user;
125 }
126
127 public function setPass( $pass ) {
128 $this->pass = $pass;
129 }
130
131 public function setHost( $host ) {
132 $this->host = $host;
133 }
134
135 public function setVerbose( $verbose ) {
136 $this->verbose = $verbose;
137 }
138
139 public function setBrowser( $b ) {
140 $browsers = $this->setupBrowsers();
141 if ( !isset( $browsers[$b] ) ) {
142 throw new MWException( "Invalid Browser: $b.\n" );
143 }
144 $this->browser = $browsers[$b];
145 }
146
147 public function __call( $name, $args ) {
148 $t = call_user_func_array( array( $this->tester, $name), $args );
149 return $t;
150 }
151
152 public function setUrl( $url ) {
153 self::$url = $url;
154 }
155
156 static public function getUrl() {
157 return self::$url;
158 }
159
160 public function setPort( $port ) {
161 $this->port = $port;
162 }
163
164 public function setUser( $user ) {
165 $this->user = $user;
166 }
167
168 public function setPass( $pass ) {
169 $this->pass = $pass;
170 }
171
172 public function setHost( $host ) {
173 $this->host = $host;
174 }
175
176 public function setVerbose( $verbose ) {
177 $this->verbose = $verbose;
178 }
179
180 public function setBrowser( $b ) {
181 $browsers = $this->setupBrowsers();
182 if ( !isset( $browsers[$b] ) ) {
183 throw new MWException( "Invalid Browser: $b.\n" );
184 }
185 $this->browser = $browsers[$b];
186 }
187
188 public function __call( $name, $args ) {
189 $t = call_user_func_array( array( $this->tester, $name), $args );
190 return $t;
191 }
192
193 // Prevent external cloning
194 protected function __clone() { }
195 // Prevent external construction
196 // protected function __construct() {}
197 }