Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create HttpFoundation response when using Chrome #593

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Symfony\Component\Panther;

use Facebook\WebDriver\Exception\InvalidArgumentException;
use Facebook\WebDriver\Exception\NoSuchElementException;
use Facebook\WebDriver\Exception\TimeoutException;
use Facebook\WebDriver\JavaScriptExecutor;
Expand All @@ -35,6 +36,7 @@
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\DomCrawler\Link;
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;
use Symfony\Component\Panther\Cookie\CookieJar;
use Symfony\Component\Panther\DomCrawler\Crawler as PantherCrawler;
use Symfony\Component\Panther\DomCrawler\Form as PantherForm;
Expand Down Expand Up @@ -143,7 +145,7 @@ public function getRequest(): object

public function getResponse(): object
{
throw new \LogicException('HttpFoundation Response object is not available when using WebDriver.');
return $this->response ?? throw new \LogicException('HttpFoundation Response object is not available when using WebDriver.');
}

public function followRedirects($followRedirects = true): void
Expand Down Expand Up @@ -527,8 +529,35 @@ public function get($url): self

$this->internalRequest = new Request($url, 'GET');
$this->webDriver->get($url);

if ($this->webDriver instanceof JavaScriptExecutor) {
$this->executeScript('window.localStorage.setItem("symfony/profiler/toolbar/displayState", "none");');
}

$this->internalResponse = new Response($this->webDriver->getPageSource());

if ($this->browserManager instanceof ChromeManager) {
try {
$events = $this->webDriver->manage()->getLog('performance');
} catch (InvalidArgumentException) {
$events = [];
}

foreach ($events as $event) {
$event = json_decode($event['message'], true)['message'];

if ('Network.responseReceived' !== ($event['method'] ?? '')) {
continue;
}
$response = $event['params']['response'];

if ($response['url'] === $url) {
$this->response = new HttpFoundationResponse($this->internalResponse->getContent(), $response['status'], $response['headers']);
break;
}
}
}

$this->crawler = $this->createCrawler();

return $this;
Expand Down
12 changes: 11 additions & 1 deletion src/ProcessManager/ChromeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,23 @@ private function createProcess(string $chromeDriverBinary): Process

private function getDefaultOptions(): array
{
$chromeOptions = new ChromeOptions();
$chromeOptions->setExperimentalOption('perfLoggingPrefs', [
'enableNetwork' => true,
]);

return [
'scheme' => 'http',
'host' => '127.0.0.1',
'port' => 9515,
'path' => '/status',
'chromedriver_arguments' => [],
'capabilities' => [],
'capabilities' => [
'goog:loggingPrefs' => [
'performance' => 'ALL',
],
ChromeOptions::CAPABILITY => $chromeOptions,
],
];
}
}
28 changes: 19 additions & 9 deletions src/ProcessManager/FirefoxManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Symfony\Component\Panther\ProcessManager;

use Facebook\WebDriver\Firefox\FirefoxOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriver;
Expand Down Expand Up @@ -52,20 +53,17 @@ public function start(): WebDriver
$this->waitUntilReady($this->process, $url.$this->options['path'], 'firefox');
}

$firefoxOptions = [];
if (isset($_SERVER['PANTHER_FIREFOX_BINARY'])) {
$firefoxOptions['binary'] = $_SERVER['PANTHER_FIREFOX_BINARY'];
}
if ($this->arguments) {
$firefoxOptions['args'] = $this->arguments;
}

$capabilities = DesiredCapabilities::firefox();
$capabilities->setCapability('moz:firefoxOptions', $firefoxOptions);

foreach ($this->options['capabilities'] as $capability => $value) {
$capabilities->setCapability($capability, $value);
}
$firefoxOptions = $capabilities->getCapability(FirefoxOptions::CAPABILITY);

// if (isset($_SERVER['PANTHER_FIREFOX_BINARY'])) {
// $firefoxOptions['binary'] = $_SERVER['PANTHER_FIREFOX_BINARY'];
// }
$firefoxOptions->addArguments($this->arguments);

return RemoteWebDriver::create($url, $capabilities, $this->options['connection_timeout_in_ms'] ?? null, $this->options['request_timeout_in_ms'] ?? null);
}
Expand Down Expand Up @@ -113,12 +111,24 @@ private function getDefaultArguments(): array

private function getDefaultOptions(): array
{
$firefoxOptions = new FirefoxOptions();

// TODO: make this work - not sure why it doesn't :)
$firefoxOptions->setPreference('devtools.netmonitor.enabled', true);
$firefoxOptions->setPreference('devtools.netmonitor.har.enableAutoExportToFile', true);
$firefoxOptions->setPreference('devtools.netmonitor.har.forceExport', true);
$firefoxOptions->setPreference('devtools.netmonitor.har.defaultLogDir', '/tmp/panther-firefox/');

return [
'scheme' => 'http',
'host' => '127.0.0.1',
'port' => 4444,
'path' => '/status',
'capabilities' => [],
'capabilities' => [
'acceptInsecureCerts' => true,
FirefoxOptions::CAPABILITY => $firefoxOptions,
],
];
}
}