Skip to content

Commit a084fae

Browse files
Create HttpFoundation response when using Chrome
1 parent 52e7ea4 commit a084fae

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/Client.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Symfony\Component\Panther;
1515

16+
use Facebook\WebDriver\Exception\InvalidArgumentException;
1617
use Facebook\WebDriver\Exception\NoSuchElementException;
1718
use Facebook\WebDriver\Exception\TimeoutException;
1819
use Facebook\WebDriver\JavaScriptExecutor;
@@ -35,6 +36,7 @@
3536
use Symfony\Component\DomCrawler\Crawler;
3637
use Symfony\Component\DomCrawler\Form;
3738
use Symfony\Component\DomCrawler\Link;
39+
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;
3840
use Symfony\Component\Panther\Cookie\CookieJar;
3941
use Symfony\Component\Panther\DomCrawler\Crawler as PantherCrawler;
4042
use Symfony\Component\Panther\DomCrawler\Form as PantherForm;
@@ -143,7 +145,7 @@ public function getRequest(): object
143145

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

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

528530
$this->internalRequest = new Request($url, 'GET');
529531
$this->webDriver->get($url);
532+
533+
if ($this->webDriver instanceof JavaScriptExecutor) {
534+
$this->executeScript('window.localStorage.setItem("symfony/profiler/toolbar/displayState", "none");');
535+
}
536+
530537
$this->internalResponse = new Response($this->webDriver->getPageSource());
531538

539+
if ($this->browserManager instanceof ChromeManager) {
540+
try {
541+
$events = $this->webDriver->manage()->getLog('performance');
542+
} catch (InvalidArgumentException) {
543+
$events = [];
544+
}
545+
546+
foreach ($events as $event) {
547+
$event = json_decode($event['message'], true)['message'];
548+
549+
if ('Network.responseReceived' !== ($event['method'] ?? '')) {
550+
continue;
551+
}
552+
$response = $event['params']['response'];
553+
554+
if ($response['url'] === $url) {
555+
$this->response = new HttpFoundationResponse($this->internalResponse->getContent(), $response['status'], $response['headers']);
556+
break;
557+
}
558+
}
559+
}
560+
532561
$this->crawler = $this->createCrawler();
533562

534563
return $this;

src/ProcessManager/ChromeManager.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,23 @@ private function createProcess(string $chromeDriverBinary): Process
135135

136136
private function getDefaultOptions(): array
137137
{
138+
$chromeOptions = new ChromeOptions();
139+
$chromeOptions->setExperimentalOption('perfLoggingPrefs', [
140+
'enableNetwork' => true,
141+
]);
142+
138143
return [
139144
'scheme' => 'http',
140145
'host' => '127.0.0.1',
141146
'port' => 9515,
142147
'path' => '/status',
143148
'chromedriver_arguments' => [],
144-
'capabilities' => [],
149+
'capabilities' => [
150+
'goog:loggingPrefs' => [
151+
'performance' => 'ALL',
152+
],
153+
ChromeOptions::CAPABILITY => $chromeOptions,
154+
],
145155
];
146156
}
147157
}

0 commit comments

Comments
 (0)