forked from Element-34/php-webdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebDriverSession.php
More file actions
249 lines (219 loc) · 7.35 KB
/
Copy pathWebDriverSession.php
File metadata and controls
249 lines (219 loc) · 7.35 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
// Copyright 2004-present Facebook. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
require_once('WebDriverContainer.php');
require_once('WebDriverSimpleItem.php');
class PHPWebDriver_WebDriverSession extends PHPWebDriver_WebDriverContainer {
const FLICK_SPEED_NORMAL = 0;
const FLICK_SPEED_FAST = 1;
protected function methods() {
return array(
'url' => 'GET', // for POST, use open($url)
'forward' => 'POST',
'back' => 'POST',
'refresh' => 'POST',
'execute' => 'POST',
'execute_async' => 'POST',
'frame' => 'POST',
'screenshot' => 'GET',
'window_handle' => 'GET',
'window_handles' => 'GET',
'source' => 'GET',
'title' => 'GET',
'keys' => 'POST',
'orientation' => array('GET', 'POST'),
'alert_text' => array('GET', 'POST'),
'accept_alert' => 'POST',
'dismiss_alert' => 'POST',
'moveto' => 'POST',
'click' => 'POST',
'buttondown' => 'POST',
'buttonup' => 'POST',
'doubleclick' => 'POST',
'location' => array('GET', 'POST'),
);
}
// /session/:sessionId/url (POST)
public function open($url, $curl_opts = array()) {
$this->curl('POST',
'/url',
array('url' => $url),
$curl_opts);
return $this;
}
// /session/:sessionId (GET)
public function capabilities($curl_opts = array()) {
$result = $this->curl('GET', '', '', $curl_opts);
return $result['value'];
}
// /session/:sessionId (DELETE)
public function close($curl_opts = array()) {
$result = $this->curl('DELETE', '', '', $curl_opts);
return $result['value'];
}
// /session/:sessionId/cookie (GET)
public function getAllCookies($curl_opts = array()) {
$result = $this->curl('GET', '/cookie', '', $curl_opts);
return $result['value'];
}
// /session/:sessionId/cookie (GET)
public function getCookie($name, $curl_opts = array()) {
$result = $this->curl('GET', '/cookie', '', $curl_opts);
foreach ($result['value'] as $cookie) {
if ($cookie["name"] == $name) {
return $cookie;
}
}
}
// /session/:sessionId/cookie (POST)
public function setCookie($cookie_json, $curl_opts = array()) {
$this->curl('POST', '/cookie', array('cookie' => $cookie_json), $curl_opts);
return $this;
}
// /session/:sessionId/cookie (DELETE)
public function deleteAllCookies($curl_opts = array()) {
$this->curl('DELETE', '/cookie', '', $curl_opts);
return $this;
}
// /session/:sessionId/cookie/:name (DELETE)
public function deleteCookie($cookie_name, $curl_opts = array()) {
$this->curl('DELETE', '/cookie/' . $cookie_name, '', $curl_opts);
return $this;
}
public function timeouts() {
trigger_error("timeouts() is deprecated; use setTimeouts() instead", E_USER_DEPRECATED);
$item = new PHPWebDriver_WebDriverSimpleItem($this->url . '/timeouts');
return $item->setMethods(array(
'async_script' => 'POST',
'implicit_wait' => 'POST',
));
}
// /session/:sessionId/timeouts (POST)
public function setTimeouts($timeout, $curl_opts = array()) {
$this->curl('POST', '/timeouts', $timeout, $curl_opts);
return $this;
}
public function implicitlyWait($s) {
$ms = $s * 1000;
$this->setTimeouts(array('type' => 'implicit', 'ms' => $ms));
return $this;
}
public function setScriptTimeout($s) {
$ms = $s * 1000;
$this->setTimeouts(array('type' => 'script', 'ms' => $ms));
return $this;
}
public function setPageLoadTimeout($s) {
$ms = $s * 1000;
$this->setTimeouts(array('type' => 'page load', 'ms' => $ms));
return $this;
}
public function ime() {
$item = new PHPWebDriver_WebDriverSimpleItem($this->url . '/ime');
return $item->setMethods(array(
'available_engines' => 'GET',
'active_engine' => 'GET',
'activated' => 'GET',
'deactivate' => 'POST',
'activate' => 'POST',
));
}
// /session/:sessionId/window (POST)
public function focusWindow($name, $curl_opts = array()) {
$this->curl('POST', '/window', array('name' => $name), $curl_opts);
return $this;
}
// /session/:sessionId/window (DELETE)
public function deleteWindow($curl_opts = array()) {
$this->curl('DELETE', '/window', $curl_opts);
return $this;
}
public function switch_to_alert() {
$this->alert();
require_once('WebDriverAlert.php');
return new PHPWebDriver_WebDriverAlert($this);
}
public function window($window_handle = 'current') {
$item = new PHPWebDriver_WebDriverSimpleItem($this->url . '/window/' . $window_handle);
return $item->setMethods(array(
'size' => array('GET', 'POST'),
'position' => array('GET', 'POST'),
'maximize' => array('POST')
));
}
// /session/:sessionId/element/active (POST)
public function activeElement($curl_opts = array()) {
$results = $this->curl('POST', '/element/active', $curl_opts);
return $this->webDriverElement($results['value']);
}
public function touch() {
$item = new PHPWebDriver_WebDriverSimpleItem($this->url . '/touch');
return $item->setMethods(array(
'click' => 'POST',
'down' => 'POST',
'up' => 'POST',
'move' => 'POST',
'scroll' => 'POST',
'doubleclick' => 'POST',
'longclick' => 'POST',
'flick' => 'POST',
));
}
public function switch_to_frame($frame = null, $curl_opts = array()) {
if (is_a($frame, "PHPWebDriver_WebDriverElement")) {
$frame_id = array("id" => array("ELEMENT" => $frame->getId()));
} elseif ($frame == null) {
$frame_id = null;
} else {
throw new InvalidArgumentException('$frame needs to be a webdriver element of a frame');
}
$results = $this->curl('POST',
'/frame',
$frame_id,
$curl_opts);
$value = null;
if (is_array($results) && array_key_exists('value', $results)) {
$value = $results['value'];
}
$message = null;
if (is_array($value) && array_key_exists('message', $value)) {
$message = $value['message'];
}
self::throwException($results['info']['http_code'], $message, $results);
}
/**
* local_storage method chaining, e.g.,
* - $session->local_storage()->method()
*
* @return PHPWebDriver_WebDriverStorage
*/
public function local_storage()
{
return PHPWebDriver_WebDriverStorage::factory('local', $this->url . '/local_storage');
}
/**
* session_storage method chaining, e.g.,
* - $session->session_storage()->method()
*
* @return PHPWebDriver_WebDriverStorage
*/
public function session_storage()
{
return PHPWebDriver_WebDriverStorage::factory('session', $this->url . '/session_storage');
}
protected function getElementPath($element_id) {
return sprintf('%s/element/%s', $this->url, $element_id);
}
}
?>