MediaWiki  REL1_19
Selenium.php
Go to the documentation of this file.
00001 <?php
00007 require( 'Testing/Selenium.php' );
00008 
00009 class Selenium {
00010         protected static $_instance = null;
00011 
00012         public $isStarted = false;
00013         public $tester;
00014 
00015         protected $port;
00016         protected $host;
00017         protected $browser;
00018         protected $browsers;
00019         protected $logger;
00020         protected $user;
00021         protected $pass;
00022         protected $timeout = 30000;
00023         protected $verbose;
00024         protected $junitlogfile; //processed by phpUnderControl
00025         protected $runagainstgrid = false;
00026 
00030         static protected $url;
00031 
00035         public function __construct() {
00040                 if ( null === self::$_instance ) {
00041                         self::$_instance = $this;
00042                 } else {
00043                         throw new MWException( "Already have one Selenium instance." );
00044                 }
00045         }
00046 
00047         public function start() {
00048                 $this->tester = new Testing_Selenium( $this->browser, self::$url, $this->host,
00049                         $this->port, $this->timeout );
00050                 if ( method_exists( $this->tester, "setVerbose" ) ) $this->tester->setVerbose( $this->verbose );
00051 
00052                 $this->tester->start();
00053                 $this->isStarted = true;
00054         }
00055 
00056         public function stop() {
00057                 $this->tester->stop();
00058                 $this->tester = null;
00059                 $this->isStarted = false;
00060         }
00061 
00062         public function login() {
00063                 if ( strlen( $this->user ) == 0 ) {
00064                         return;
00065                 }
00066                 $this->open( self::$url . '/index.php?title=Special:Userlogin' );
00067                 $this->type( 'wpName1', $this->user );
00068                 $this->type( 'wpPassword1', $this->pass );
00069                 $this->click( "//input[@id='wpLoginAttempt']" );
00070                 $this->waitForPageToLoad( 10000 );
00071 
00072                 // after login we redirect to the main page. So check whether the "Prefernces" top menu item exists
00073                 $value = $this->isElementPresent( "//li[@id='pt-preferences']" );
00074 
00075                 if ( $value != true ) {
00076                         throw new Testing_Selenium_Exception( "Login Failed" );
00077                 }
00078 
00079         }
00080 
00081         public static function getInstance() {
00082                 if ( null === self::$_instance ) {
00083                         throw new MWException( "No instance set yet" );
00084                 }
00085 
00086                 return self::$_instance;
00087         }
00088 
00089         public function loadPage( $title, $action ) {
00090                 $this->open( self::$url . '/index.php?title=' . $title . '&action=' . $action );
00091         }
00092 
00093         public function setLogger( $logger ) {
00094                 $this->logger = $logger;
00095         }
00096 
00097         public function getLogger( ) {
00098                 return $this->logger;
00099         }
00100 
00101         public function log( $message ) {
00102                 $this->logger->write( $message );
00103         }
00104 
00105         public function setUrl( $url ) {
00106                 self::$url = $url;
00107         }
00108 
00109         static public function getUrl() {
00110                 return self::$url;
00111         }
00112 
00113         public function setPort( $port ) {
00114                 $this->port = $port;
00115         }
00116 
00117         public function getPort() {
00118                 return $this->port;
00119         }
00120 
00121         public function setUser( $user ) {
00122                 $this->user = $user;
00123         }
00124 
00125         // Function to get username
00126         public function getUser() {
00127                 return $this->user;
00128         }
00129         
00130 
00131         public function setPass( $pass ) {
00132                 $this->pass = $pass;
00133         }
00134 
00135     //add function to get password    
00136         public function getPass(  ) {
00137                 return $this->pass;
00138         }
00139        
00140         
00141         public function setHost( $host ) {
00142                 $this->host = $host;
00143         }
00144 
00145         public function setVerbose( $verbose ) {
00146                 $this->verbose = $verbose;
00147         }
00148 
00149         public function setAvailableBrowsers( $availableBrowsers ) {
00150                 $this->browsers = $availableBrowsers;
00151         }
00152 
00153         public function setJUnitLogfile( $junitlogfile ) {
00154                 $this->junitlogfile = $junitlogfile;
00155         }
00156 
00157         public function getJUnitLogfile( ) {
00158                 return $this->junitlogfile;
00159         }
00160 
00161         public function setRunAgainstGrid( $runagainstgrid ) {
00162                 $this->runagainstgrid = $runagainstgrid;
00163         }
00164 
00165         public function setBrowser( $b ) {
00166                 if ($this->runagainstgrid) {
00167                         $this->browser = $b;
00168                         return true;
00169                 }
00170                 if ( !isset( $this->browsers[$b] ) ) {
00171                         throw new MWException( "Invalid Browser: $b.\n" );
00172                 }
00173 
00174                 $this->browser = $this->browsers[$b];
00175         }
00176 
00177         public function getAvailableBrowsers() {
00178                 return $this->browsers;
00179         }
00180 
00181         public function __call( $name, $args ) {
00182                 $t = call_user_func_array( array( $this->tester, $name ), $args );
00183                 return $t;
00184         }
00185 
00186         // Prevent external cloning
00187         protected function __clone() { }
00188         // Prevent external construction
00189         // protected function __construct() {}
00190 }