added support for junit log file output, which is neede by phpUnderControl
[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
26 /**
27 * @todo this shouldn't have to be static
28 */
29 static protected $url;
30
31 /**
32 * Override parent
33 */
34 public function __construct() {
35 /**
36 * @todo this is an ugly hack to make information available to
37 * other tests. It should be fixed.
38 */
39 if ( null === self::$_instance ) {
40 self::$_instance = $this;
41 } else {
42 throw new MWException( "Already have one Selenium instance." );
43 }
44 }
45
46 public function start() {
47 $this->tester = new Testing_Selenium( $this->browser, self::$url, $this->host,
48 $this->port, $this->timeout );
49 if ( method_exists( $this->tester, "setVerbose" ) ) $this->tester->setVerbose( $this->verbose );
50
51 $this->tester->start();
52 $this->isStarted = true;
53 }
54
55 public function stop() {
56 $this->tester->stop();
57 $this->tester = null;
58 $this->isStarted = false;
59 }
60
61 public function login() {
62 if ( strlen( $this->user ) == 0 ) {
63 return;
64 }
65 $this->open( self::$url . '/index.php?title=Special:Userlogin' );
66 $this->type( 'wpName1', $this->user );
67 $this->type( 'wpPassword1', $this->pass );
68 $this->click( "//input[@id='wpLoginAttempt']" );
69 $this->waitForPageToLoad( 10000 );
70
71 // after login we redirect to the main page. So check whether the "Prefernces" top menu item exists
72 $value = $this->isElementPresent( "//li[@id='pt-preferences']" );
73
74 if ( $value != true ) {
75 throw new Testing_Selenium_Exception( "Login Failed" );
76 }
77
78 }
79
80 public static function getInstance() {
81 if ( null === self::$_instance ) {
82 throw new MWException( "No instance set yet" );
83 }
84
85 return self::$_instance;
86 }
87
88 public function loadPage( $title, $action ) {
89 $this->open( self::$url . '/index.php?title=' . $title . '&action=' . $action );
90 }
91
92 public function setLogger( $logger ) {
93 $this->logger = $logger;
94 }
95
96 public function getLogger( ) {
97 return $this->logger;
98 }
99
100 public function log( $message ) {
101 $this->logger->write( $message );
102 }
103
104 public function setUrl( $url ) {
105 self::$url = $url;
106 }
107
108 static public function getUrl() {
109 return self::$url;
110 }
111
112 public function setPort( $port ) {
113 $this->port = $port;
114 }
115
116 public function getPort() {
117 return $this->port;
118 }
119
120 public function setUser( $user ) {
121 $this->user = $user;
122 }
123
124 public function setPass( $pass ) {
125 $this->pass = $pass;
126 }
127
128 public function setHost( $host ) {
129 $this->host = $host;
130 }
131
132 public function setVerbose( $verbose ) {
133 $this->verbose = $verbose;
134 }
135
136 public function setAvailableBrowsers( $availableBrowsers ) {
137 $this->browsers = $availableBrowsers;
138 }
139
140 public function setJUnitLogfile( $junitlogfile ) {
141 $this->junitlogfile = $junitlogfile;
142 }
143
144 public function getJUnitLogfile( ) {
145 return $this->junitlogfile;
146 }
147
148 public function setBrowser( $b ) {
149 if ( !isset( $this->browsers[$b] ) ) {
150 throw new MWException( "Invalid Browser: $b.\n" );
151 }
152
153 $this->browser = $this->browsers[$b];
154 }
155
156 public function getAvailableBrowsers() {
157 return $this->browsers;
158 }
159
160 public function __call( $name, $args ) {
161 $t = call_user_func_array( array( $this->tester, $name ), $args );
162 return $t;
163 }
164
165 // Prevent external cloning
166 protected function __clone() { }
167 // Prevent external construction
168 // protected function __construct() {}
169 }