662e282e411180e5e1ea5bf6de169c4a50025ad2
[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
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 public function setPass( $pass ) {
126 $this->pass = $pass;
127 }
128
129 public function setHost( $host ) {
130 $this->host = $host;
131 }
132
133 public function setVerbose( $verbose ) {
134 $this->verbose = $verbose;
135 }
136
137 public function setAvailableBrowsers( $availableBrowsers ) {
138 $this->browsers = $availableBrowsers;
139 }
140
141 public function setJUnitLogfile( $junitlogfile ) {
142 $this->junitlogfile = $junitlogfile;
143 }
144
145 public function getJUnitLogfile( ) {
146 return $this->junitlogfile;
147 }
148
149 public function setRunAgainstGrid( $runagainstgrid ) {
150 $this->runagainstgrid = $runagainstgrid;
151 }
152
153 public function setBrowser( $b ) {
154 if ($this->runagainstgrid) {
155 $this->browser = $b;
156 return true;
157 }
158 if ( !isset( $this->browsers[$b] ) ) {
159 throw new MWException( "Invalid Browser: $b.\n" );
160 }
161
162 $this->browser = $this->browsers[$b];
163 }
164
165 public function getAvailableBrowsers() {
166 return $this->browsers;
167 }
168
169 public function __call( $name, $args ) {
170 $t = call_user_func_array( array( $this->tester, $name ), $args );
171 return $t;
172 }
173
174 // Prevent external cloning
175 protected function __clone() { }
176 // Prevent external construction
177 // protected function __construct() {}
178 }