summaryrefslogtreecommitdiff
path: root/tests/selenium/Selenium.php
blob: 07f9867125ba3446d1aa029784a2c2c98d6f2794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
 * Selenium connector
 * This is implemented as a singleton.
 */

require( 'Testing/Selenium.php' );

class Selenium {
	protected static $_instance = null;

	public $isStarted = false;
	public $tester;

	protected $port;
	protected $host;
	protected $browser;
	protected $browsers;
	protected $logger;
	protected $user;
	protected $pass;
	protected $timeout = 30000;
	protected $verbose;
	protected $junitlogfile; //processed by phpUnderControl
	protected $runagainstgrid = false;

	/**
	 * @todo this shouldn't have to be static
	 */
	static protected $url;

	/**
	 * Override parent
	 */
	public function __construct() {
		/**
		 * @todo this is an ugly hack to make information available to
		 * other tests.  It should be fixed.
		 */
		if ( null === self::$_instance ) {
			self::$_instance = $this;
		} else {
			throw new MWException( "Already have one Selenium instance." );
		}
	}

	public function start() {
		$this->tester = new Testing_Selenium( $this->browser, self::$url, $this->host,
			$this->port, $this->timeout );
		if ( method_exists( $this->tester, "setVerbose" ) ) {
			$this->tester->setVerbose( $this->verbose );
		}

		$this->tester->start();
		$this->isStarted = true;
	}

	public function stop() {
		$this->tester->stop();
		$this->tester = null;
		$this->isStarted = false;
	}

	public function login() {
		if ( strlen( $this->user ) == 0 ) {
			return;
		}
		$this->open( self::$url . '/index.php?title=Special:Userlogin' );
		$this->type( 'wpName1', $this->user );
		$this->type( 'wpPassword1', $this->pass );
		$this->click( "//input[@id='wpLoginAttempt']" );
		$this->waitForPageToLoad( 10000 );

		// after login we redirect to the main page. So check whether the "Prefernces" top menu item exists
		$value = $this->isElementPresent( "//li[@id='pt-preferences']" );

		if ( $value != true ) {
			throw new Testing_Selenium_Exception( "Login Failed" );
		}

	}

	public static function getInstance() {
		if ( null === self::$_instance ) {
			throw new MWException( "No instance set yet" );
		}

		return self::$_instance;
	}

	public function loadPage( $title, $action ) {
		$this->open( self::$url . '/index.php?title=' . $title . '&action=' . $action );
	}

	public function setLogger( $logger ) {
		$this->logger = $logger;
	}

	public function getLogger() {
		return $this->logger;
	}

	public function log( $message ) {
		$this->logger->write( $message );
	}

	public function setUrl( $url ) {
		self::$url = $url;
	}

	public static function getUrl() {
		return self::$url;
	}

	public function setPort( $port ) {
		$this->port = $port;
	}

	public function getPort() {
		return $this->port;
	}

	public function setUser( $user ) {
		$this->user = $user;
	}

	// Function to get username
	public function getUser() {
		return $this->user;
	}


	public function setPass( $pass ) {
		$this->pass = $pass;
	}

	//add function to get password
	public function getPass() {
		return $this->pass;
	}

	public function setHost( $host ) {
		$this->host = $host;
	}

	public function setVerbose( $verbose ) {
		$this->verbose = $verbose;
	}

	public function setAvailableBrowsers( $availableBrowsers ) {
		$this->browsers = $availableBrowsers;
	}

	public function setJUnitLogfile( $junitlogfile ) {
		$this->junitlogfile = $junitlogfile;
	}

	public function getJUnitLogfile() {
		return $this->junitlogfile;
	}

	public function setRunAgainstGrid( $runagainstgrid ) {
		$this->runagainstgrid = $runagainstgrid;
	}

	public function setBrowser( $b ) {
		if ( $this->runagainstgrid ) {
			$this->browser = $b;
			return true;
		}
		if ( !isset( $this->browsers[$b] ) ) {
			throw new MWException( "Invalid Browser: $b.\n" );
		}

		$this->browser = $this->browsers[$b];
	}

	public function getAvailableBrowsers() {
		return $this->browsers;
	}

	public function __call( $name, $args ) {
		$t = call_user_func_array( array( $this->tester, $name ), $args );
		return $t;
	}

	// Prevent external cloning
	protected function __clone() {}
	// Prevent external construction
	// protected function __construct() {}
}