forked from php-webdriver/php-webdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebDriverOptions.php
180 lines (157 loc) · 4.78 KB
/
WebDriverOptions.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
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
<?php
namespace Facebook\WebDriver;
use Facebook\WebDriver\Exception\NoSuchCookieException;
use Facebook\WebDriver\Remote\DriverCommand;
use Facebook\WebDriver\Remote\ExecuteMethod;
use InvalidArgumentException;
/**
* Managing stuff you would do in a browser.
*/
class WebDriverOptions
{
/**
* @var ExecuteMethod
*/
protected $executor;
/**
* @var bool
*/
protected $isW3cCompliant;
public function __construct(ExecuteMethod $executor, $isW3cCompliant = false)
{
$this->executor = $executor;
$this->isW3cCompliant = $isW3cCompliant;
}
/**
* Add a specific cookie.
*
* @see Cookie for description of possible cookie properties
* @param Cookie|array $cookie Cookie object. May be also created from array for compatibility reasons.
* @return WebDriverOptions The current instance.
*/
public function addCookie($cookie)
{
if (is_array($cookie)) { // @todo @deprecated remove in 2.0
$cookie = Cookie::createFromArray($cookie);
}
if (!$cookie instanceof Cookie) {
throw new InvalidArgumentException('Cookie must be set from instance of Cookie class or from array.');
}
$this->executor->execute(
DriverCommand::ADD_COOKIE,
['cookie' => $cookie->toArray()]
);
return $this;
}
/**
* Delete all the cookies that are currently visible.
*
* @return WebDriverOptions The current instance.
*/
public function deleteAllCookies()
{
$this->executor->execute(DriverCommand::DELETE_ALL_COOKIES);
return $this;
}
/**
* Delete the cookie with the given name.
*
* @param string $name
* @return WebDriverOptions The current instance.
*/
public function deleteCookieNamed($name)
{
$this->executor->execute(
DriverCommand::DELETE_COOKIE,
[':name' => $name]
);
return $this;
}
/**
* Get the cookie with a given name.
*
* @param string $name
* @throws NoSuchCookieException In W3C compliant mode if no cookie with the given name is present
* @return Cookie|null The cookie, or null in JsonWire mode if no cookie with the given name is present
*/
public function getCookieNamed($name)
{
if ($this->isW3cCompliant) {
$cookieArray = $this->executor->execute(
DriverCommand::GET_NAMED_COOKIE,
[':name' => $name]
);
if (!is_array($cookieArray)) { // Microsoft Edge returns null even in W3C mode => emulate proper behavior
throw new NoSuchCookieException('no such cookie');
}
return Cookie::createFromArray($cookieArray);
}
$cookies = $this->getCookies();
foreach ($cookies as $cookie) {
if ($cookie['name'] === $name) {
return $cookie;
}
}
return null;
}
/**
* Get all the cookies for the current domain.
*
* @return Cookie[] The array of cookies presented.
*/
public function getCookies()
{
$cookieArrays = $this->executor->execute(DriverCommand::GET_ALL_COOKIES);
if (!is_array($cookieArrays)) { // Microsoft Edge returns null if there are no cookies...
return [];
}
$cookies = [];
foreach ($cookieArrays as $cookieArray) {
$cookies[] = Cookie::createFromArray($cookieArray);
}
return $cookies;
}
/**
* Return the interface for managing driver timeouts.
*
* @return WebDriverTimeouts
*/
public function timeouts()
{
return new WebDriverTimeouts($this->executor, $this->isW3cCompliant);
}
/**
* An abstraction allowing the driver to manipulate the browser's window
*
* @return WebDriverWindow
* @see WebDriverWindow
*/
public function window()
{
return new WebDriverWindow($this->executor, $this->isW3cCompliant);
}
/**
* Get the log for a given log type. Log buffer is reset after each request.
*
* @param string $log_type The log type.
* @return array The list of log entries.
* @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#log-type
*/
public function getLog($log_type)
{
return $this->executor->execute(
DriverCommand::GET_LOG,
['type' => $log_type]
);
}
/**
* Get available log types.
*
* @return array The list of available log types.
* @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#log-type
*/
public function getAvailableLogTypes()
{
return $this->executor->execute(DriverCommand::GET_AVAILABLE_LOG_TYPES);
}
}