diff --git a/src/PantherTestCaseTrait.php b/src/PantherTestCaseTrait.php index 5c8ea2b1..6caf6e96 100644 --- a/src/PantherTestCaseTrait.php +++ b/src/PantherTestCaseTrait.php @@ -74,7 +74,7 @@ public static function stopWebServer(): void } if (null !== self::$pantherClient) { - foreach (self::$pantherClients as $i => $pantherClient) { + foreach (self::$pantherClients as $pantherClient) { // Stop ChromeDriver only when all sessions are already closed $pantherClient->quit(false); } @@ -178,16 +178,21 @@ protected static function createPantherClient(array $options = [], array $kernel self::startWebServer($options); + $browserArguments = $options['browser_arguments'] ?? null; + if (null !== $browserArguments && !\is_array($browserArguments)) { + throw new \TypeError(sprintf('Expected key "browser_arguments" to be an array or null, "%s" given.', get_debug_type($browserArguments))); + } + if (PantherTestCase::FIREFOX === $browser) { - self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, null, $managerOptions, self::$baseUri); + self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, $browserArguments, $managerOptions, self::$baseUri); } else { try { - self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, null, $managerOptions, self::$baseUri); + self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, $browserArguments, $managerOptions, self::$baseUri); } catch (\RuntimeException $e) { if (PantherTestCase::CHROME === $browser) { throw $e; } - self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, null, $managerOptions, self::$baseUri); + self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, $browserArguments, $managerOptions, self::$baseUri); } if (null === $browser) { @@ -229,9 +234,14 @@ protected static function createHttpBrowserClient(array $options = [], array $ke self::startWebServer($options); if (null === self::$httpBrowserClient) { - // The ScopingHttpClient cant't be used cause the HttpBrowser only supports absolute URLs, + $httpClientOptions = $options['http_client_options'] ?? []; + if (!\is_array($httpClientOptions)) { + throw new \TypeError(sprintf('Expected key "http_client_options" to be an array, "%s" given.', get_debug_type($httpClientOptions))); + } + + // The ScopingHttpClient can't be used cause the HttpBrowser only supports absolute URLs, // https://github.com/symfony/symfony/pull/35177 - self::$httpBrowserClient = new HttpBrowserClient(HttpClient::create()); + self::$httpBrowserClient = new HttpBrowserClient(HttpClient::create($httpClientOptions)); } if (is_a(self::class, KernelTestCase::class, true)) { diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 265befae..bad02d69 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -26,7 +26,9 @@ use Symfony\Component\Panther\Client; use Symfony\Component\Panther\Cookie\CookieJar; use Symfony\Component\Panther\DomCrawler\Crawler; +use Symfony\Component\Panther\PantherTestCase; use Symfony\Component\Panther\ProcessManager\ChromeManager; +use Symfony\Contracts\HttpClient\HttpClientInterface; /** * @author Kévin Dunglas @@ -450,4 +452,60 @@ public function testPing(): void self::stopWebServer(); $this->assertFalse($client->ping()); } + + public function testCreatePantherClientWithBrowserArguments(): void + { + $client = self::createPantherClient([ + 'browser' => PantherTestCase::CHROME, + 'browser_arguments' => ['--window-size=1400,900'], + ]); + $this->assertInstanceOf(AbstractBrowser::class, $client); + $this->assertInstanceOf(WebDriver::class, $client); + $this->assertInstanceOf(JavaScriptExecutor::class, $client); + $this->assertInstanceOf(KernelInterface::class, self::$kernel); + + self::stopWebServer(); + } + + public function testCreatePantherClientWithInvalidBrowserArguments(): void + { + $this->expectException(\TypeError::class); + + self::createPantherClient([ + 'browser_arguments' => 'bad browser arguments data type', + ]); + } + + public function testCreateHttpBrowserClientWithHttpClientOptions(): void + { + $client = self::createHttpBrowserClient([ + 'http_client_options' => [ + 'auth_basic' => ['foo', 'bar'], + 'on_progress' => $closure = static function () {}, + 'cafile' => '/foo/bar', + ], + ]); + + ($httpClientRef = new \ReflectionProperty($client, 'client'))->setAccessible(true); + /** @var HttpClientInterface $httpClient */ + $httpClient = $httpClientRef->getValue($client); + + ($httpClientOptionsRef = new \ReflectionProperty($httpClient, 'defaultOptions'))->setAccessible(true); + $httpClientOptions = $httpClientOptionsRef->getValue($httpClient); + + $this->assertSame('foo:bar', $httpClientOptions['auth_basic']); + $this->assertSame($closure, $httpClientOptions['on_progress']); + $this->assertSame('/foo/bar', $httpClientOptions['cafile']); + + self::stopWebServer(); + } + + public function testCreateHttpBrowserClientWithInvalidHttpClientOptions(): void + { + $this->expectException(\TypeError::class); + + self::createHttpBrowserClient([ + 'http_client_options' => 'bad http client option data type', + ]); + } }