/
CustomWebDriverCommand.php
82 lines (70 loc) · 2.17 KB
/
CustomWebDriverCommand.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
<?php
namespace Facebook\WebDriver\Remote;
use Facebook\WebDriver\Exception\Internal\LogicException;
use Facebook\WebDriver\Exception\WebDriverException;
class CustomWebDriverCommand extends WebDriverCommand
{
public const METHOD_GET = 'GET';
public const METHOD_POST = 'POST';
/** @var string */
private $customUrl;
/** @var string */
private $customMethod;
/**
* @param string $session_id
* @param string $url
* @param string $method
*/
public function __construct($session_id, $url, $method, array $parameters)
{
$this->setCustomRequestParameters($url, $method);
parent::__construct($session_id, DriverCommand::CUSTOM_COMMAND, $parameters);
}
/**
* @throws WebDriverException
* @return string
*/
public function getCustomUrl()
{
if ($this->customUrl === null) {
throw LogicException::forError('URL of custom command is not set');
}
return $this->customUrl;
}
/**
* @throws WebDriverException
* @return string
*/
public function getCustomMethod()
{
if ($this->customMethod === null) {
throw LogicException::forError('Method of custom command is not set');
}
return $this->customMethod;
}
/**
* @param string $custom_url
* @param string $custom_method
* @throws WebDriverException
*/
protected function setCustomRequestParameters($custom_url, $custom_method)
{
$allowedMethods = [static::METHOD_GET, static::METHOD_POST];
if (!in_array($custom_method, $allowedMethods, true)) {
throw LogicException::forError(
sprintf(
'Invalid custom method "%s", must be one of [%s]',
$custom_method,
implode(', ', $allowedMethods)
)
);
}
$this->customMethod = $custom_method;
if (mb_strpos($custom_url, '/') !== 0) {
throw LogicException::forError(
sprintf('URL of custom command has to start with / but is "%s"', $custom_url)
);
}
$this->customUrl = $custom_url;
}
}