1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeubiQA\Application\Selenium; |
4
|
|
|
|
5
|
|
|
use BeubiQA\Application\Lib\ResponseWaitter; |
6
|
|
|
use BeubiQA\Application\Selenium\Options\SeleniumStartOptions; |
7
|
|
|
use Symfony\Component\Process\ExecutableFinder; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
class SeleniumStarter |
11
|
|
|
{ |
12
|
|
|
/** @var Process */ |
13
|
|
|
private $process; |
14
|
|
|
/** @var ResponseWaitter */ |
15
|
|
|
private $responseWaitter; |
16
|
|
|
/** @var ExecutableFinder */ |
17
|
|
|
private $exeFinder; |
18
|
|
|
/** @var SeleniumStartOptions */ |
19
|
|
|
private $seleniumOptions; |
20
|
|
|
/** @var string */ |
21
|
|
|
private $startCommand; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Process $process |
25
|
|
|
* @param ResponseWaitter $responseWaitter |
26
|
|
|
* @param ExecutableFinder $exeFinder |
27
|
|
|
*/ |
28
|
|
|
public function __construct(SeleniumStartOptions $seleniumOptions, Process $process, ResponseWaitter $responseWaitter, ExecutableFinder $exeFinder) |
29
|
|
|
{ |
30
|
|
|
$this->seleniumOptions = $seleniumOptions; |
31
|
|
|
$this->process = $process; |
32
|
|
|
$this->responseWaitter = $responseWaitter; |
33
|
|
|
$this->exeFinder = $exeFinder; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function setStartCommand($command) |
37
|
|
|
{ |
38
|
|
|
$this->startCommand = $command; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getStartCommand() |
42
|
|
|
{ |
43
|
|
|
return $this->startCommand; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function start() |
47
|
|
|
{ |
48
|
|
|
$seleniumUrl = $this->seleniumOptions->getSeleniumUrl(); |
49
|
|
|
$seleniumQuery = $this->seleniumOptions->getSeleniumQuery(); |
50
|
|
|
$seleniumJarLocation = $this->seleniumOptions->getSeleniumJarLocation(); |
51
|
|
|
if (!$seleniumUrl || !$seleniumQuery || !$seleniumJarLocation) { |
52
|
|
|
throw new \LogicException('Url, Query and Selenium Jar Location is mandatory and Jar Location should point to a .jar file.'); |
53
|
|
|
} |
54
|
|
|
if (!is_file($seleniumJarLocation)) { |
55
|
|
|
throw new \RuntimeException('Selenium jar is not a file'); |
56
|
|
|
} |
57
|
|
|
if (!is_readable($seleniumJarLocation)) { |
58
|
|
|
throw new \RuntimeException('Selenium jar not readable - '.$seleniumJarLocation); |
59
|
|
|
} |
60
|
|
|
if ($this->isSeleniumAvailable()) { |
61
|
|
|
throw new \RuntimeException('Selenium was already started'); |
62
|
|
|
} |
63
|
|
|
$this->setStartCommand($this->createStartCommand()); |
64
|
|
|
$this->process->setCommandLine($this->getStartCommand()); |
65
|
|
|
$this->process->start(); |
66
|
|
|
$this->responseWaitter->waitUntilAvailable($seleniumUrl, $seleniumQuery); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function isSeleniumAvailable() |
70
|
|
|
{ |
71
|
|
|
return $this->responseWaitter->isAvailable($this->seleniumOptions->getSeleniumUrl(), $this->seleniumOptions->getSeleniumQuery()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $options |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
private function createStartCommand() |
80
|
|
|
{ |
81
|
|
|
$cmd = $this->exeFinder->find('java').' -jar '.$this->seleniumOptions->getSeleniumJarLocation(); |
82
|
|
|
if ($this->seleniumOptions->isXvfbEnabled()) { |
83
|
|
|
$xvfbCmd = 'DISPLAY=:21'; |
84
|
|
|
$cmd = $xvfbCmd.' '.$cmd; |
85
|
|
|
} |
86
|
|
|
$seleniumExtraArgs = $this->seleniumOptions->getSeleniumExtraArguments(); |
87
|
|
|
if ($seleniumExtraArgs) { |
88
|
|
|
foreach ($seleniumExtraArgs as $optionName => $optionValue) { |
89
|
|
|
$cmd .= ' -'.$optionName.' '.$optionValue; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
$seleniumPort = $this->seleniumOptions->getSeleniumPort(); |
93
|
|
|
if ($seleniumPort) { |
94
|
|
|
$cmd .= ' -port '.$seleniumPort; |
95
|
|
|
} |
96
|
|
|
$logLocation = $this->seleniumOptions->getSeleniumLogLocation(); |
97
|
|
|
if ($logLocation) { |
98
|
|
|
$cmd .= ' > '.$logLocation.' 2> '.$logLocation; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $cmd; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return SeleniumStartOptions |
106
|
|
|
*/ |
107
|
|
|
public function getStartOptions() |
108
|
|
|
{ |
109
|
|
|
return $this->seleniumOptions; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return ResponseWaitter |
114
|
|
|
*/ |
115
|
|
|
public function getResponseWaitter() |
116
|
|
|
{ |
117
|
|
|
return $this->responseWaitter; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|