-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathFirefoxManager.php
148 lines (124 loc) · 5.03 KB
/
FirefoxManager.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
<?php
/*
* This file is part of the Panther project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Symfony\Component\Panther\ProcessManager;
use Facebook\WebDriver\Firefox\FirefoxOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriver;
use Symfony\Component\Panther\Exception\RuntimeException;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
/**
* @author Kévin Dunglas <[email protected]>
*/
final class FirefoxManager implements BrowserManagerInterface
{
use WebServerReadinessProbeTrait;
private Process $process;
private array $arguments;
private array $options;
/**
* @throws RuntimeException
*/
public function __construct(?string $geckodriverBinary = null, ?array $arguments = null, array $options = [])
{
$this->options = array_merge($this->getDefaultOptions(), $options);
$this->process = new Process([$geckodriverBinary ?: $this->findGeckodriverBinary(), '--port='.$this->options['port']], null, null, null, null);
$this->arguments = $arguments ?? $this->getDefaultArguments();
}
/**
* @throws RuntimeException
*/
public function start(): WebDriver
{
$url = $this->options['scheme'].'://'.$this->options['host'].':'.$this->options['port'];
if (!$this->process->isRunning()) {
$this->checkPortAvailable($this->options['host'], $this->options['port']);
$this->process->start();
$this->waitUntilReady($this->process, $url.$this->options['path'], 'firefox');
}
return RemoteWebDriver::create($url, $this->buildCapabilities(), $this->options['connection_timeout_in_ms'] ?? null, $this->options['request_timeout_in_ms'] ?? null);
}
public function quit(): void
{
$this->process->stop();
}
/**
* @throws RuntimeException
*/
private function findGeckodriverBinary(): string
{
if ($binary = (new ExecutableFinder())->find('geckodriver', null, ['./drivers'])) {
return $binary;
}
throw new RuntimeException('"geckodriver" binary not found. Install it using the package manager of your operating system or by running "composer require --dev dbrekelmans/bdi && vendor/bin/bdi detect drivers".');
}
private function getDefaultArguments(): array
{
$args = [];
// Enable the headless mode unless PANTHER_NO_HEADLESS is defined
if (!filter_var($_SERVER['PANTHER_NO_HEADLESS'] ?? false, \FILTER_VALIDATE_BOOLEAN)) {
$args[] = '--headless';
}
// Enable devtools for debugging
if (filter_var($_SERVER['PANTHER_DEVTOOLS'] ?? true, \FILTER_VALIDATE_BOOLEAN)) {
$args[] = '--devtools';
}
// Add custom arguments with PANTHER_FIREFOX_ARGUMENTS
if ($_SERVER['PANTHER_FIREFOX_ARGUMENTS'] ?? false) {
$arguments = explode(' ', $_SERVER['PANTHER_FIREFOX_ARGUMENTS']);
$args = array_merge($args, $arguments);
}
return $args;
}
private function buildCapabilities(): DesiredCapabilities
{
$capabilities = DesiredCapabilities::firefox();
$firefoxOptions = $capabilities->getCapability(FirefoxOptions::CAPABILITY);
if (isset($_SERVER['PANTHER_FIREFOX_BINARY'])) {
$firefoxOptions->setOption('binary', $_SERVER['PANTHER_FIREFOX_BINARY']);
}
if ($this->arguments) {
$firefoxOptions->addArguments($this->arguments);
}
// Prefer reduced motion, see https://developer.mozilla.org/fr/docs/Web/CSS/@media/prefers-reduced-motion
if (!filter_var($_SERVER['PANTHER_NO_REDUCED_MOTION'] ?? false, \FILTER_VALIDATE_BOOLEAN)) {
$firefoxOptions->setPreference('ui.prefersReducedMotion', 1);
} else {
$firefoxOptions->setPreference('ui.prefersReducedMotion', 0);
}
foreach ($this->options['capabilities'] as $capability => $value) {
if (FirefoxOptions::CAPABILITY !== $capability) {
$capabilities->setCapability($capability, $value);
continue;
}
if (isset($value[FirefoxOptions::OPTION_ARGS])) {
$firefoxOptions->addArguments($value[FirefoxOptions::OPTION_ARGS]);
}
if (isset($value[FirefoxOptions::OPTION_PREFS])) {
foreach ($value[FirefoxOptions::OPTION_PREFS] as $preference => $preferenceValue) {
$firefoxOptions->setPreference($preference, $preferenceValue);
}
}
}
return $capabilities;
}
private function getDefaultOptions(): array
{
return [
'scheme' => 'http',
'host' => '127.0.0.1',
'port' => 4444,
'path' => '/status',
'capabilities' => [],
];
}
}