-
Notifications
You must be signed in to change notification settings - Fork 62
/
Container.php
242 lines (210 loc) · 6.2 KB
/
Container.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
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
<?php
/**
* @copyright 2004 Meta Platforms, Inc.
* @license Apache-2.0
*
* @package WebDriver
*
* @author Justin Bishop <[email protected]>
*/
namespace WebDriver;
use WebDriver\Exception as WebDriverException;
/**
* Abstract WebDriver\Container class
*
* @package WebDriver
*/
abstract class Container extends AbstractWebDriver
{
/**
* @var array
*/
private $strategies;
/**
* {@inheritdoc}
*/
public function __construct($url)
{
parent::__construct($url);
$locatorStrategy = new \ReflectionClass('WebDriver\LocatorStrategy');
$this->strategies = $locatorStrategy->getConstants();
}
/**
* Find element: /session/:sessionId/element (POST)
* Find child element: /session/:sessionId/element/:id/element (POST)
* Search for element on page, starting from the document root.
*
* @param string $using the locator strategy to use
* @param string $value the search target
*
* @return \WebDriver\Element
*
* @throws \WebDriver\Exception if element not found, or invalid XPath
*/
public function element($using = null, $value = null)
{
$locatorJson = $this->parseArgs('element', func_get_args());
try {
$result = $this->curl(
'POST',
'/element',
$locatorJson
);
} catch (WebDriverException\NoSuchElement $e) {
throw WebDriverException::factory(
WebDriverException::NO_SUCH_ELEMENT,
sprintf(
"Element not found with %s, %s\n\n%s",
$locatorJson['using'],
$locatorJson['value'],
$e->getMessage()
),
$e
);
}
$element = $this->makeElement($result['value']);
if ($element === null) {
throw WebDriverException::factory(
WebDriverException::NO_SUCH_ELEMENT,
sprintf(
"Element not found with %s, %s\n",
$locatorJson['using'],
$locatorJson['value']
)
);
}
return $element;
}
/**
* Find elements: /session/:sessionId/elements (POST)
* Find child elements: /session/:sessionId/element/:id/elements (POST)
* Search for multiple elements on page, starting from the document root.
*
* @param string $using the locator strategy to use
* @param string $value the search target
*
* @return array
*
* @throws \WebDriver\Exception if invalid XPath
*/
public function elements($using = null, $value = null)
{
$locatorJson = $this->parseArgs('elements', func_get_args());
$result = $this->curl(
'POST',
'/elements',
$locatorJson
);
if (! is_array($result['value'])) {
return array();
}
return array_filter(
array_map(
array($this, 'makeElement'),
$result['value']
)
);
}
/**
* Parse arguments allowing either separate $using and $value parameters, or
* as an array containing the JSON parameters
*
* @param string $method method name
* @param array $argv arguments
*
* @return array
*
* @throws \WebDriver\Exception if invalid number of arguments to the called method
*/
private function parseArgs($method, $argv)
{
$argc = count($argv);
switch ($argc) {
case 2:
$using = $argv[0];
$value = $argv[1];
break;
case 1:
$arg = $argv[0];
if (is_array($arg)) {
$using = $arg['using'];
$value = $arg['value'];
break;
}
// fall through
default:
throw WebDriverException::factory(
WebDriverException::JSON_PARAMETERS_EXPECTED,
sprintf('Invalid arguments to %s method: %s', $method, print_r($argv, true))
);
}
return $this->locate($using, $value);
}
/**
* Return JSON parameter for element / elements command
*
* @param string $using locator strategy
* @param string $value search target
*
* @return array
*
* @throws \WebDriver\Exception if invalid locator strategy
*/
public function locate($using, $value)
{
if (! in_array($using, $this->strategies)) {
throw WebDriverException::factory(
WebDriverException::UNKNOWN_LOCATOR_STRATEGY,
sprintf('Invalid locator strategy %s', $using)
);
}
return array(
'using' => $using,
'value' => $value,
);
}
/**
* Factory method for elements
*
* @param mixed $value
*
* @return \WebDriver\Element|null
*/
protected function makeElement($value)
{
if (array_key_exists(LegacyElement::LEGACY_ELEMENT_ID, (array) $value)) {
$identifier = $value[LegacyElement::LEGACY_ELEMENT_ID];
return new LegacyElement(
$this->getIdentifierPath($identifier),
$identifier
);
}
if (array_key_exists(Element::WEB_ELEMENT_ID, (array) $value)) {
$identifier = $value[Element::WEB_ELEMENT_ID];
return new Element(
$this->getIdentifierPath($identifier),
$identifier
);
}
return null;
}
/**
* {@inheritdoc}
*/
public function __call($name, $arguments)
{
if (count($arguments) === 1 && in_array(str_replace('_', ' ', $name), $this->strategies)) {
return $this->locate($name, $arguments[0]);
}
// fallback to executing WebDriver commands
return parent::__call($name, $arguments);
}
/**
* Get wire protocol URL for an identifier
*
* @param string $identifier
*
* @return string
*/
abstract protected function getIdentifierPath($identifier);
}