-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSessionManager.php
116 lines (103 loc) · 2.51 KB
/
SessionManager.php
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
<?php
/**
* Global session manager for connections to the Selenium 2 server
*/
class Menta_SessionManager {
/**
* @var WebDriver_Session
*/
protected static $session;
/**
* @var WebDriver
*/
protected static $webdriver;
/**
* @var string
*/
protected static $browser = 'firefox';
/**
* @var string
*/
protected static $serverUrl = 'http://localhost:4444/wd/hub';
/**
* @var array
*/
protected static $additionalCapabilities = array();
/**
* Init settings
*
* @static
* @param $serverUrl
* @param string $browser
* @param array $additionalCapabilities
* @return void
*/
public static function init($serverUrl=NULL, $browser=NULL, array $additionalCapabilities=NULL) {
if (!is_null($serverUrl)) {
self::$serverUrl = $serverUrl;
}
if (!is_null($browser)) {
self::$browser = $browser;
}
if (!is_null($additionalCapabilities)) {
self::$additionalCapabilities = $additionalCapabilities;
}
}
/**
* @static
* @return WebDriver
*/
public static function getWebdriver() {
if (is_null(self::$webdriver)) {
if (empty(self::$serverUrl)) {
throw new Exception('No serverUrl set. Call Menta_SessionManager::init() to configure first');
}
self::$webdriver = new WebDriver(self::$serverUrl);
}
return self::$webdriver;
}
/**
* @param bool $forceNew
* @static
* @return WebDriver_Session
*/
public static function getSession($forceNew=false) {
if ($forceNew) {
self::closeSession();
}
if (is_null(self::$session)) {
self::$session = self::getWebdriver()->session(self::$browser, self::$additionalCapabilities);
if (!self::$session instanceof WebDriver_Session) {
throw new Exception('Error while creating new session');
}
Menta_Events::dispatchEvent('after_session_create', array(
'session' => self::$session,
'forceNew' => $forceNew
));
}
return self::$session;
}
/**
* Check if an active session exists
*
* @static
* @return bool
*/
public static function activeSessionExists() {
return (self::$session instanceof WebDriver_Session);
}
/**
* Close existing session
*
* @static
* @return void
*/
public static function closeSession() {
if (self::activeSessionExists()) {
Menta_Events::dispatchEvent('before_session_close', array('session' => self::$session));
self::$session->close();
self::$session = NULL;
Menta_Events::dispatchEvent('after_session_close');
}
}
}