1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeubiQA\Application\Selenium; |
4
|
|
|
|
5
|
|
|
use BeubiQA\Application\Lib\ResponseWaitter; |
6
|
|
|
use BeubiQA\Application\Selenium\Options\SeleniumStopOptions; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GuzzleHttp\Exception\ConnectException; |
9
|
|
|
|
10
|
|
|
class SeleniumStopper |
11
|
|
|
{ |
12
|
|
|
/** @var ResponseWaitter */ |
13
|
|
|
private $responseWaitter; |
14
|
|
|
|
15
|
|
|
/** @var Client */ |
16
|
|
|
private $httpClient; |
17
|
|
|
|
18
|
|
|
/** @var SeleniumStopOptions */ |
19
|
|
|
private $seleniumOptions; |
20
|
|
|
|
21
|
|
|
public function __construct(SeleniumStopOptions $seleniumOptions, ResponseWaitter $responseWaitter, Client $httpClient) |
22
|
|
|
{ |
23
|
|
|
$this->seleniumOptions = $seleniumOptions; |
24
|
|
|
$this->responseWaitter = $responseWaitter; |
25
|
|
|
$this->httpClient = $httpClient; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function stop() |
29
|
|
|
{ |
30
|
|
|
$seleniumPort = $this->seleniumOptions->getSeleniumPort(); |
31
|
|
|
$seleniumUrl = $this->seleniumOptions->getSeleniumUrl(); |
32
|
|
|
$seleniumQuery = $this->seleniumOptions->getSeleniumQuery(); |
33
|
|
|
$seleniumShutdownUrl = $this->seleniumOptions->getSeleniumShutdownUrl(); |
34
|
|
|
$seleniumShutdownOptions = $this->seleniumOptions->getSeleniumShutDownOptions(); |
35
|
|
|
|
36
|
|
|
if (!$seleniumShutdownOptions || !$seleniumShutdownUrl || !$seleniumPort || !$seleniumUrl || !$seleniumQuery) { |
37
|
|
|
throw new \LogicException('Port, Url, Shutdown Url, Shutdown Options, and Query are mandatory.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->sendShutdownCmd($this->seleniumOptions->getSeleniumPort()); |
41
|
|
|
$this->responseWaitter->waitUntilNotAvailable($seleniumUrl, $seleniumQuery); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function sendShutdownCmd() |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
|
|
$this->httpClient->get( |
48
|
|
|
$this->seleniumOptions->getSeleniumShutdownUrl(), |
49
|
|
|
$this->seleniumOptions->getSeleniumShutDownOptions() |
50
|
|
|
); |
51
|
|
|
} catch (ConnectException $exc) { |
52
|
|
|
throw new \RuntimeException($exc->getMessage().PHP_EOL.'Probably selenium is already stopped.'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return SeleniumStopOptions |
58
|
|
|
*/ |
59
|
|
|
public function getStopOptions() |
60
|
|
|
{ |
61
|
|
|
return $this->seleniumOptions; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return ResponseWaitter |
66
|
|
|
*/ |
67
|
|
|
public function getResponseWaitter() |
68
|
|
|
{ |
69
|
|
|
return $this->responseWaitter; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|