|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Openbuildings\Spiderling; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Connect to selenium service. |
|
7
|
|
|
* Send requests to selenium |
|
8
|
|
|
* |
|
9
|
|
|
* @package Openbuildings\Spiderling |
|
10
|
|
|
* @author Ivan Kerin |
|
11
|
|
|
* @copyright (c) 2013 OpenBuildings Ltd. |
|
12
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
|
13
|
|
|
*/ |
|
14
|
|
|
class Driver_Selenium_Connection |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* The current selenium session id |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $_session_id; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The selenium server url |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_server = 'http://localhost:4444/wd/hub/'; |
|
27
|
|
|
|
|
28
|
2 |
|
public function server($server = NULL) |
|
29
|
|
|
{ |
|
30
|
2 |
|
if ($server !== NULL) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->_server = $server; |
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
2 |
|
return $this->_server; |
|
36
|
2 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function __construct($server = NULL) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($server) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->server($server); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Start a new selenium session, based on passed desired capabilities. |
|
48
|
|
|
* Reuses the session if possible |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $desiredCapabilities |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function start(array $desiredCapabilities = NULL) |
|
53
|
|
|
{ |
|
54
|
|
|
if ( ! ($this->_session_id = $this->reuse_session())) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->_session_id = $this->new_session($desiredCapabilities); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
$this->_server .= "session/{$this->_session_id}/"; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Check if a session has been started |
|
64
|
|
|
* @return boolean |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function is_started() |
|
67
|
|
|
{ |
|
68
|
1 |
|
return (bool) $this->_session_id; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Try to reuse an existing selenium session, return the reused id |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function reuse_session() |
|
76
|
|
|
{ |
|
77
|
|
|
$sessions = $this->get('sessions'); |
|
78
|
|
|
foreach ($sessions as $session) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$id = $session['id']; |
|
81
|
|
|
try |
|
82
|
|
|
{ |
|
83
|
|
|
$this->get("session/$id/window_handle"); |
|
84
|
2 |
|
return $id; |
|
85
|
|
|
} |
|
86
|
|
|
// @codeCoverageIgnoreStart |
|
87
|
|
|
// This cannot be tested because of selenium bug (can't close main window) |
|
88
|
|
|
catch (Exception_Selenium $exception) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->delete("session/$id"); |
|
91
|
|
|
} |
|
92
|
|
|
// @codeCoverageIgnoreEnd |
|
93
|
1 |
|
} |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Initiate a new session, based on the desired capabilities |
|
98
|
|
|
* @param array $desiredCapabilities |
|
99
|
|
|
* @return new session |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function new_session(array $desiredCapabilities = NULL) |
|
102
|
|
|
{ |
|
103
|
1 |
|
if ( ! $desiredCapabilities) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$desiredCapabilities = array('browserName' => 'firefox'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$session = $this->post('session', array('desiredCapabilities' => $desiredCapabilities)); |
|
109
|
1 |
|
return $session['webdriver.remote.sessionid']; |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Perform a get request to the selenium server |
|
114
|
|
|
* @param string $command |
|
115
|
|
|
* @return mixed |
|
116
|
|
|
*/ |
|
117
|
|
|
public function get($command) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->call($command); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Perform a post request to the selenium server |
|
124
|
|
|
* @param string $command |
|
125
|
|
|
* @param array $params |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
2 |
|
public function post($command, array $params) |
|
129
|
|
|
{ |
|
130
|
2 |
|
$options = array(); |
|
131
|
2 |
|
$options[CURLOPT_POST] = TRUE; |
|
132
|
|
|
|
|
133
|
2 |
|
if ($params) { |
|
|
|
|
|
|
134
|
|
|
$options[CURLOPT_POSTFIELDS] = json_encode($params); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $this->call($command, $options); |
|
138
|
2 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Perform a delete request to the selenium server |
|
142
|
|
|
* @param string $command |
|
143
|
|
|
* @return mixed |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function delete($command) |
|
146
|
|
|
{ |
|
147
|
1 |
|
$options = array(); |
|
148
|
1 |
|
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; |
|
149
|
|
|
|
|
150
|
|
|
return $this->call($command, $options); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Perform a request to the selenium server, using curl |
|
155
|
|
|
* @param string $command |
|
156
|
|
|
* @param array $options curl options |
|
157
|
|
|
* @return mixed |
|
158
|
|
|
*/ |
|
159
|
2 |
|
public function call($command, array $options = array()) |
|
160
|
|
|
{ |
|
161
|
|
|
$curl = curl_init(); |
|
162
|
|
|
$options[CURLOPT_URL] = $this->server().$command; |
|
163
|
2 |
|
$options[CURLOPT_RETURNTRANSFER] = TRUE; |
|
164
|
2 |
|
$options[CURLOPT_FOLLOWLOCATION] = TRUE; |
|
165
|
2 |
|
$options[CURLOPT_HTTPHEADER] = array( |
|
166
|
|
|
'Content-Type: application/json;charset=UTF-8', |
|
167
|
|
|
'Accept: application/json', |
|
168
|
2 |
|
); |
|
169
|
|
|
|
|
170
|
|
|
curl_setopt_array($curl, $options); |
|
171
|
|
|
|
|
172
|
|
|
$raw = trim(curl_exec($curl)); |
|
173
|
|
|
|
|
174
|
|
|
$result = json_decode($raw, TRUE); |
|
175
|
|
|
|
|
176
|
|
|
$error = curl_error($curl); |
|
177
|
|
|
|
|
178
|
|
|
curl_close($curl); |
|
179
|
|
|
|
|
180
|
2 |
|
if ($error) |
|
181
|
|
|
throw new Exception_Driver('Curl ":command" throws exception :error', array(':command' => $command, ':error' => $error)); |
|
182
|
|
|
|
|
183
|
2 |
|
if ($result['status'] == 10) |
|
184
|
|
|
throw new Exception_Staleelement(); |
|
185
|
|
|
|
|
186
|
2 |
|
if ($result['status'] != 0) |
|
187
|
|
|
throw new Exception_Selenium($result['status']); |
|
188
|
|
|
|
|
189
|
2 |
|
return $result['value']; |
|
190
|
2 |
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..