Skip to content

Commit

Permalink
Create HttpFoundation response when using Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed May 10, 2023
1 parent 52e7ea4 commit 8801213
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
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,
],
];
}
}

0 comments on commit 8801213

Please sign in to comment.