Merge "Don't reinvent the wheel in SpecialRecentchangeslinked::getExtraOptions"
[lhc/web/wiklou.git] / 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" ) ) {
51 $this->tester->setVerbose( $this->verbose );
52 }
53
54 $this->tester->start();
55 $this->isStarted = true;
56 }
57
58 public function stop() {
59 $this->tester->stop();
60 $this->tester = null;
61 $this->isStarted = false;
62 }
63
64 public function login() {
65 if ( strlen( $this->user ) == 0 ) {
66 return;
67 }
68 $this->open( self::$url . '/index.php?title=Special:Userlogin' );
69 $this->type( 'wpName1', $this->user );
70 $this->type( 'wpPassword1', $this->pass );
71 $this->click( "//input[@id='wpLoginAttempt']" );
72 $this->waitForPageToLoad( 10000 );
73
74 // after login we redirect to the main page. So check whether the "Prefernces" top menu item exists
75 $value = $this->isElementPresent( "//li[@id='pt-preferences']" );
76
77 if ( $value != true ) {
78 throw new Testing_Selenium_Exception( "Login Failed" );
79 }
80
81 }
82
83 public static function getInstance() {
84 if ( null === self::$_instance ) {
85 throw new MWException( "No instance set yet" );
86 }
87
88 return self::$_instance;
89 }
90
91 public function loadPage( $title, $action ) {
92 $this->open( self::$url . '/index.php?title=' . $title . '&action=' . $action );
93 }
94
95 public function setLogger( $logger ) {
96 $this->logger = $logger;
97 }
98
99 public function getLogger() {
100 return $this->logger;
101 }
102
103 public function log( $message ) {
104 $this->logger->write( $message );
105 }
106
107 public function setUrl( $url ) {
108 self::$url = $url;
109 }
110
111 public static function getUrl() {
112 return self::$url;
113 }
114
115 public function setPort( $port ) {
116 $this->port = $port;
117 }
118
119 public function getPort() {
120 return $this->port;
121 }
122
123 public function setUser( $user ) {
124 $this->user = $user;
125 }
126
127 // Function to get username
128 public function getUser() {
129 return $this->user;
130 }
131
132
133 public function setPass( $pass ) {
134 $this->pass = $pass;
135 }
136
137 //add function to get password
138 public function getPass() {
139 return $this->pass;
140 }
141
142 public function setHost( $host ) {
143 $this->host = $host;
144 }
145
146 public function setVerbose( $verbose ) {
147 $this->verbose = $verbose;
148 }
149
150 public function setAvailableBrowsers( $availableBrowsers ) {
151 $this->browsers = $availableBrowsers;
152 }
153
154 public function setJUnitLogfile( $junitlogfile ) {
155 $this->junitlogfile = $junitlogfile;
156 }
157
158 public function getJUnitLogfile() {
159 return $this->junitlogfile;
160 }
161
162 public function setRunAgainstGrid( $runagainstgrid ) {
163 $this->runagainstgrid = $runagainstgrid;
164 }
165
166 public function setBrowser( $b ) {
167 if ( $this->runagainstgrid ) {
168 $this->browser = $b;
169 return true;
170 }
171 if ( !isset( $this->browsers[$b] ) ) {
172 throw new MWException( "Invalid Browser: $b.\n" );
173 }
174
175 $this->browser = $this->browsers[$b];
176 }
177
178 public function getAvailableBrowsers() {
179 return $this->browsers;
180 }
181
182 public function __call( $name, $args ) {
183 $t = call_user_func_array( array( $this->tester, $name ), $args );
184 return $t;
185 }
186
187 // Prevent external cloning
188 protected function __clone() {}
189 // Prevent external construction
190 // protected function __construct() {}
191 }