/
DriverService.php
209 lines (177 loc) · 4.99 KB
/
DriverService.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
<?php
namespace Facebook\WebDriver\Remote\Service;
use Exception;
use Facebook\WebDriver\Net\URLChecker;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
/**
* Start local WebDriver service (when remote WebDriver server is not used).
* This will start new process of respective browser driver and take care of its lifecycle.
*/
class DriverService
{
/**
* @var string
*/
private $executable;
/**
* @var string
*/
private $url;
/**
* @var array
*/
private $args;
/**
* @var array
*/
private $environment;
/**
* @var Process|null
*/
private $process;
/**
* @param string $executable
* @param int $port The given port the service should use.
* @param array $args
* @param array|null $environment Use the system environment if it is null
*/
public function __construct($executable, $port, $args = [], $environment = null)
{
$this->setExecutable($executable);
$this->url = sprintf('http://localhost:%d', $port);
$this->args = $args;
$this->environment = $environment ?: $_ENV;
}
/**
* @return string
*/
public function getURL()
{
return $this->url;
}
/**
* @return DriverService
*/
public function start()
{
if ($this->process !== null) {
return $this;
}
$this->process = $this->createProcess();
$this->process->start();
$this->checkWasStarted($this->process);
$checker = new URLChecker();
$checker->waitUntilAvailable(20 * 1000, $this->url . '/status');
return $this;
}
/**
* @return DriverService
*/
public function stop()
{
if ($this->process === null) {
return $this;
}
$this->process->stop();
$this->process = null;
$checker = new URLChecker();
$checker->waitUntilUnavailable(3 * 1000, $this->url . '/shutdown');
return $this;
}
/**
* @return bool
*/
public function isRunning()
{
if ($this->process === null) {
return false;
}
return $this->process->isRunning();
}
/**
* @deprecated Has no effect. Will be removed in next major version. Executable is now checked
* when calling setExecutable().
* @param string $executable
* @return string
*/
protected static function checkExecutable($executable)
{
return $executable;
}
/**
* @param string $executable
* @throws Exception
*/
protected function setExecutable($executable)
{
if ($this->isExecutable($executable)) {
$this->executable = $executable;
return;
}
throw new Exception(
sprintf(
'"%s" is not executable. Make sure the path is correct or use environment variable to specify'
. ' location of the executable.',
$executable
)
);
}
/**
* @param Process $process
*/
protected function checkWasStarted($process)
{
usleep(10000); // wait 10ms, otherwise the asynchronous process failure may not yet be propagated
if (!$process->isRunning()) {
throw new Exception(
sprintf(
'Error starting driver executable "%s": %s',
$process->getCommandLine(),
$process->getErrorOutput()
)
);
}
}
/**
* @return Process
*/
private function createProcess()
{
// BC: ProcessBuilder deprecated since Symfony 3.4 and removed in Symfony 4.0.
if (class_exists(ProcessBuilder::class)
&& false === mb_strpos('@deprecated', (new \ReflectionClass(ProcessBuilder::class))->getDocComment())
) {
$processBuilder = (new ProcessBuilder())
->setPrefix($this->executable)
->setArguments($this->args)
->addEnvironmentVariables($this->environment);
return $processBuilder->getProcess();
}
// Safe to use since Symfony 3.3
$commandLine = array_merge([$this->executable], $this->args);
return new Process($commandLine, null, $this->environment);
}
/**
* Check whether given file is executable directly or using system PATH
*
* @param string $filename
* @return bool
*/
private function isExecutable($filename)
{
if (is_executable($filename)) {
return true;
}
if ($filename !== basename($filename)) { // $filename is an absolute path, do no try to search it in PATH
return false;
}
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
foreach ($paths as $path) {
if (is_executable($path . DIRECTORY_SEPARATOR . $filename)) {
return true;
}
}
return false;
}
}