From 402b413da384ed49724bb6e8e9bb2fe9dbfc3ac7 Mon Sep 17 00:00:00 2001 From: robertfausk Date: Tue, 7 May 2024 09:00:29 +0200 Subject: [PATCH] Add support for `php-webdriver/webdriver >= 1.15.0` #16 --- CHANGELOG.md | 8 ++++ LICENSE | 12 ++--- .../Driver/PantherFactory.php | 24 ++++++++++ tests/Unit/Driver/PantherFactoryTest.php | 46 +++++++++++++++++++ tests/fixtures/extension_dummy.ext | 1 + 5 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/extension_dummy.ext diff --git a/CHANGELOG.md b/CHANGELOG.md index 55b8bc6..52c9b09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ vx.x.x / 2024-xx-xx =================== +v1.1.3 / 2024-05-07 +=================== + +Fixes: +* Ensure support for ```php-webdriver/webdriver >= 1.15.0``` #16 + + Thanks for report and investigation to @validaide-mark-bijl + v1.1.2 / 2024-04-18 =================== diff --git a/LICENSE b/LICENSE index 605fe43..c09d8bf 100644 --- a/LICENSE +++ b/LICENSE @@ -31,24 +31,24 @@ friends-of-behat/mink-extension v2.5.0 MIT php-webdriver/webdriver 1.15.1 MIT psr/container 1.1.2 MIT psr/log 2.0.0 MIT -robertfausk/mink-panther-driver v1.1.0 MIT -symfony/browser-kit v5.4.35 MIT +robertfausk/mink-panther-driver v1.1.1 MIT +symfony/browser-kit v5.4.39 MIT symfony/config v3.4.47 MIT symfony/console v4.4.49 MIT -symfony/css-selector v7.0.3 MIT +symfony/css-selector v7.0.7 MIT symfony/dependency-injection v3.4.47 MIT symfony/deprecation-contracts v2.5.3 MIT -symfony/dom-crawler v5.4.35 MIT +symfony/dom-crawler v5.4.39 MIT symfony/event-dispatcher v4.4.44 MIT symfony/event-dispatcher-contracts v1.10.0 MIT symfony/filesystem v4.4.42 MIT -symfony/http-client v5.4.38 MIT +symfony/http-client v5.4.39 MIT symfony/http-client-contracts v2.5.3 MIT symfony/panther v1.1.1 MIT symfony/polyfill-ctype v1.29.0 MIT symfony/polyfill-mbstring v1.29.0 MIT symfony/polyfill-php80 v1.29.0 MIT -symfony/process v5.4.36 MIT +symfony/process v5.4.39 MIT symfony/service-contracts v2.5.3 MIT symfony/translation v4.4.47 MIT symfony/translation-contracts v2.5.3 MIT diff --git a/src/ServiceContainer/Driver/PantherFactory.php b/src/ServiceContainer/Driver/PantherFactory.php index e8519e0..f029d2c 100644 --- a/src/ServiceContainer/Driver/PantherFactory.php +++ b/src/ServiceContainer/Driver/PantherFactory.php @@ -4,6 +4,7 @@ namespace Robertfausk\Behat\PantherExtension\ServiceContainer\Driver; use Behat\MinkExtension\ServiceContainer\Driver\DriverFactory; +use Facebook\WebDriver\Chrome\ChromeOptions; use Robertfausk\Behat\PantherExtension\ServiceContainer\PantherConfiguration; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\DependencyInjection\Definition; @@ -51,6 +52,29 @@ public function buildDriver(array $config) ); } + // php-webdriver expects ChromeOptions instead of array since v1.15.0 + // see: https://github.com/robertfausk/behat-panther-extension/issues/16 + if (isset($config['manager_options']['capabilities'][ChromeOptions::CAPABILITY]) && 'goog:chromeOptions' === ChromeOptions::CAPABILITY) { + $chromeOptions = new ChromeOptions(); + foreach ($config['manager_options']['capabilities'][ChromeOptions::CAPABILITY] as $name => $value) { + switch ($name) { + case 'prefs': + $chromeOptions->setExperimentalOption($name, $value); + break; + case 'args': + $chromeOptions->addArguments($value); + break; + case 'extensions': + $chromeOptions->addExtensions($value); + break; + case 'binary': + $chromeOptions->setBinary($value); + break; + } + } + $config['manager_options']['capabilities'][ChromeOptions::CAPABILITY] = $chromeOptions; + } + return new Definition( 'Behat\Mink\Driver\PantherDriver', array( diff --git a/tests/Unit/Driver/PantherFactoryTest.php b/tests/Unit/Driver/PantherFactoryTest.php index 848650a..c970b39 100644 --- a/tests/Unit/Driver/PantherFactoryTest.php +++ b/tests/Unit/Driver/PantherFactoryTest.php @@ -3,6 +3,7 @@ namespace Tests\Unit\Driver; +use Facebook\WebDriver\Chrome\ChromeOptions; use PHPUnit\Framework\TestCase; use Robertfausk\Behat\PantherExtension\ServiceContainer\Driver\PantherFactory; @@ -72,4 +73,49 @@ public function test_build_driver_without_options(): void $this->assertArrayHasKey(0, $arguments, 'Arguments of definition should not be empty.'); $this->assertSame([], $arguments[0]); } + + public function test_build_chrome_driver_with_chrome_options_as_object_instead_of_array(): void + { + $config = [ + 'manager_options' => [ + 'connection_timeout_in_ms' => '5000', + 'request_timeout_in_ms' => '120000', + 'capabilities' => [ + 'goog:chromeOptions' => [ + 'prefs' => [ + 'download.default_directory' => '/var/www/html/tests/files/Downloads', + ], + 'args' => ['start-maximized'], + 'binary' => ['/path/to/acme'], + 'extensions' => ['/var/www/html/tests/fixtures/extension_dummy.ext'], + ], + ], + ], + ]; + $pantherFactory = new PantherFactory(); + $definition = $pantherFactory->buildDriver($config); + $arguments = $definition->getArguments(); + + $this->assertArrayHasKey(2, $arguments, 'Arguments of definition should not be empty.'); + $this->assertArrayHasKey('connection_timeout_in_ms', $arguments[2]); + $this->assertSame('5000', $arguments[2]['connection_timeout_in_ms']); + $this->assertArrayHasKey('request_timeout_in_ms', $arguments[2]); + $this->assertSame('120000', $arguments[2]['request_timeout_in_ms']); + $this->assertArrayHasKey('capabilities', $arguments[2]); + $this->assertArrayHasKey('goog:chromeOptions', $arguments[2]['capabilities']); + $chromeOptions = $arguments[2]['capabilities']['goog:chromeOptions']; + + if ('goog:chromeOptions' === ChromeOptions::CAPABILITY) { + $this->assertInstanceOf(ChromeOptions::class, $chromeOptions); + $chromeOptions = $chromeOptions->toArray(); + // base64 encoded value of extension file content + $this->assertSame(['MTIzNDU2Nzg5MA=='], $chromeOptions['extensions']); + } else { + $this->assertSame(['/var/www/html/tests/fixtures/extension_dummy.ext'], $chromeOptions['extensions']); + } + + $this->assertSame(['download.default_directory' => '/var/www/html/tests/files/Downloads'], $chromeOptions['prefs']); + $this->assertSame(['start-maximized'], $chromeOptions['args']); + $this->assertSame(['/path/to/acme'], $chromeOptions['binary']); + } } diff --git a/tests/fixtures/extension_dummy.ext b/tests/fixtures/extension_dummy.ext new file mode 100644 index 0000000..6a537b5 --- /dev/null +++ b/tests/fixtures/extension_dummy.ext @@ -0,0 +1 @@ +1234567890 \ No newline at end of file