From 18a3f770db963f38b7d73d7061b9ffd0e0cda457 Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Mon, 4 Mar 2024 20:31:01 +0100 Subject: [PATCH 1/6] Implement WebdriverClassicDriver factory --- composer.json | 3 +- .../Driver/WebdriverClassicFactorySpec.php | 24 +++++++ .../Driver/EnvironmentCapabilities.php | 43 ++++++++++++ .../Driver/Selenium2Factory.php | 22 +------ .../Driver/Selenium4Factory.php | 24 ++----- .../Driver/WebdriverClassicFactory.php | 66 +++++++++++++++++++ .../ServiceContainer/MinkExtension.php | 2 + 7 files changed, 146 insertions(+), 38 deletions(-) create mode 100644 spec/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactorySpec.php create mode 100644 src/Behat/MinkExtension/ServiceContainer/Driver/EnvironmentCapabilities.php create mode 100644 src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php diff --git a/composer.json b/composer.json index 386a574..8028807 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ }, "require-dev": { "behat/mink-goutte-driver": "^1.1 || ^2.0", - "phpspec/phpspec": "^6.0 || ^7.0 || 7.1.x-dev" + "phpspec/phpspec": "^6.0 || ^7.0 || 7.1.x-dev", + "mink/webdriver-classic-driver": "dev-main" }, "extra": { "branch-alias": { diff --git a/spec/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactorySpec.php b/spec/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactorySpec.php new file mode 100644 index 0000000..64d334b --- /dev/null +++ b/spec/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactorySpec.php @@ -0,0 +1,24 @@ +shouldHaveType(DriverFactory::class); + } + + public function it_is_named_webdriver_classic(): void + { + $this->getDriverName()->shouldReturn('webdriver-classic'); + } + + public function it_supports_javascript(): void + { + $this->supportsJavascript()->shouldBe(true); + } +} diff --git a/src/Behat/MinkExtension/ServiceContainer/Driver/EnvironmentCapabilities.php b/src/Behat/MinkExtension/ServiceContainer/Driver/EnvironmentCapabilities.php new file mode 100644 index 0000000..ddba7a4 --- /dev/null +++ b/src/Behat/MinkExtension/ServiceContainer/Driver/EnvironmentCapabilities.php @@ -0,0 +1,43 @@ + getenv('TRAVIS_JOB_NUMBER'), + 'build' => getenv('TRAVIS_BUILD_NUMBER'), + 'tags' => [ + 'Travis-CI', + 'PHP ' . PHP_VERSION, + ], + ]; + + case (bool)getenv('JENKINS_HOME'): + return [ + 'tunnel-identifier' => getenv('JOB_NAME'), + 'build' => getenv('BUILD_NUMBER'), + 'tags' => [ + 'Jenkins', + 'PHP ' . PHP_VERSION, + getenv('BUILD_TAG'), + ], + ]; + + default: + return [ + 'tags' => [ + php_uname('n'), + 'PHP ' . PHP_VERSION, + ], + ]; + } + } +} diff --git a/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium2Factory.php b/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium2Factory.php index 0da0564..1c57529 100644 --- a/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium2Factory.php +++ b/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium2Factory.php @@ -15,6 +15,8 @@ class Selenium2Factory implements DriverFactory { + use EnvironmentCapabilities; + /** * {@inheritdoc} */ @@ -60,27 +62,9 @@ public function buildDriver(array $config) $extraCapabilities = $config['capabilities']['extra_capabilities']; unset($config['capabilities']['extra_capabilities']); - if (getenv('TRAVIS_JOB_NUMBER')) { - $guessedCapabilities = array( - 'tunnel-identifier' => getenv('TRAVIS_JOB_NUMBER'), - 'build' => getenv('TRAVIS_BUILD_NUMBER'), - 'tags' => array('Travis-CI', 'PHP '.phpversion()), - ); - } elseif (getenv('JENKINS_HOME')) { - $guessedCapabilities = array( - 'tunnel-identifier' => getenv('JOB_NAME'), - 'build' => getenv('BUILD_NUMBER'), - 'tags' => array('Jenkins', 'PHP '.phpversion(), getenv('BUILD_TAG')), - ); - } else { - $guessedCapabilities = array( - 'tags' => array(php_uname('n'), 'PHP '.phpversion()), - ); - } - return new Definition('Behat\Mink\Driver\Selenium2Driver', array( $config['browser'], - array_replace($guessedCapabilities, $extraCapabilities, $config['capabilities']), + array_replace($this->guessEnvironmentCapabilities(), $extraCapabilities, $config['capabilities']), $config['wd_host'], )); } diff --git a/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium4Factory.php b/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium4Factory.php index 9e3a9f2..84bc8f9 100644 --- a/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium4Factory.php +++ b/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium4Factory.php @@ -15,6 +15,8 @@ class Selenium4Factory implements DriverFactory { + use EnvironmentCapabilities; + /** * {@inheritdoc} */ @@ -58,26 +60,12 @@ public function buildDriver(array $config) )); } - $args = array( - 'capabilities' => $config['capabilities'], - 'tags' => array(php_uname('n'), 'PHP '.phpversion()) - ); - - if (getenv('TRAVIS_JOB_NUMBER')) { - $args['tunnel-identifier'] = getenv('TRAVIS_JOB_NUMBER'); - $args['build'] = getenv('TRAVIS_BUILD_NUMBER'); - $args['tags'] = array('Travis-CI', 'PHP '.phpversion()); - } - - if (getenv('JENKINS_HOME')) { - $args['tunnel-identifier'] = getenv('JOB_NAME'); - $args['build'] = getenv('BUILD_NUMBER'); - $args['tags'] = array('Jenkins', 'PHP '.phpversion(), getenv('BUILD_TAG')); - } - return new Definition('Behat\Mink\Driver\Selenium4Driver', array( $config['browser'], - $args, + array_merge( + ['capabilities' => $config['capabilities']], + $this->guessEnvironmentCapabilities() + ), $config['wd_host'], )); } diff --git a/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php b/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php new file mode 100644 index 0000000..499d951 --- /dev/null +++ b/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php @@ -0,0 +1,66 @@ +children() + ->scalarNode('browser')->defaultValue('%mink.browser_name%')->end() + ->scalarNode('wd_host')->defaultValue('http://localhost:4444/wd/hub')->end() + ->arrayNode('capabilities') + ->normalizeKeys(false) + ->useAttributeAsKey('name') + ->prototype('variable')->end() + ->end() + ->end(); + } + + /** + * {@inheritdoc} + */ + public function buildDriver(array $config): Definition + { + if (!class_exists(WebdriverClassicDriver::class)) { + throw new \RuntimeException( + "Install mink/webdriver-classic-driver in order to use the {$this->getDriverName()} driver." + ); + } + + return new Definition(WebdriverClassicDriver::class, [ + $config['browser'], + array_merge( + ['capabilities' => $config['capabilities']], + $this->guessEnvironmentCapabilities() + ), + $config['wd_host'], + ]); + } +} diff --git a/src/Behat/MinkExtension/ServiceContainer/MinkExtension.php b/src/Behat/MinkExtension/ServiceContainer/MinkExtension.php index a1209f8..7a75ce5 100644 --- a/src/Behat/MinkExtension/ServiceContainer/MinkExtension.php +++ b/src/Behat/MinkExtension/ServiceContainer/MinkExtension.php @@ -21,6 +21,7 @@ use Behat\MinkExtension\ServiceContainer\Driver\Selenium2Factory; use Behat\MinkExtension\ServiceContainer\Driver\Selenium4Factory; use Behat\MinkExtension\ServiceContainer\Driver\SeleniumFactory; +use Behat\MinkExtension\ServiceContainer\Driver\WebdriverClassicFactory; use Behat\MinkExtension\ServiceContainer\Driver\ZombieFactory; use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension; use Behat\Testwork\ServiceContainer\Exception\ProcessingException; @@ -62,6 +63,7 @@ public function __construct() $this->registerDriverFactory(new BrowserStackFactory()); $this->registerDriverFactory(new ZombieFactory()); $this->registerDriverFactory(new AppiumFactory()); + $this->registerDriverFactory(new WebdriverClassicFactory()); } public function registerDriverFactory(DriverFactory $driverFactory) From aa631e6b671c1b25f1521237ddcc9713ef8bac8c Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Fri, 12 Apr 2024 23:13:30 +0200 Subject: [PATCH 2/6] Use underscore for driver key --- .../ServiceContainer/Driver/WebdriverClassicFactorySpec.php | 2 +- .../ServiceContainer/Driver/WebdriverClassicFactory.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactorySpec.php b/spec/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactorySpec.php index 64d334b..ddb0ca9 100644 --- a/spec/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactorySpec.php +++ b/spec/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactorySpec.php @@ -14,7 +14,7 @@ public function it_is_a_driver_factory(): void public function it_is_named_webdriver_classic(): void { - $this->getDriverName()->shouldReturn('webdriver-classic'); + $this->getDriverName()->shouldReturn('webdriver_classic'); } public function it_supports_javascript(): void diff --git a/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php b/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php index 499d951..d173d5b 100644 --- a/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php +++ b/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php @@ -15,7 +15,7 @@ class WebdriverClassicFactory implements DriverFactory */ public function getDriverName(): string { - return 'webdriver-classic'; + return 'webdriver_classic'; } /** From 0876261a411209b31760ce9aa0427ee269985dca Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Tue, 6 Aug 2024 18:21:33 +0200 Subject: [PATCH 3/6] Fix capabilities array bug --- .../ServiceContainer/Driver/WebdriverClassicFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php b/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php index d173d5b..f11a6e4 100644 --- a/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php +++ b/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php @@ -57,7 +57,7 @@ public function buildDriver(array $config): Definition return new Definition(WebdriverClassicDriver::class, [ $config['browser'], array_merge( - ['capabilities' => $config['capabilities']], + $config['capabilities'], $this->guessEnvironmentCapabilities() ), $config['wd_host'], From 4073c2bb38388f3b5829a8ef0186d93653d4a4f4 Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Tue, 6 Aug 2024 18:23:05 +0200 Subject: [PATCH 4/6] Respect/prefer user-overriden capabilities --- .../ServiceContainer/Driver/WebdriverClassicFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php b/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php index f11a6e4..f83ff42 100644 --- a/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php +++ b/src/Behat/MinkExtension/ServiceContainer/Driver/WebdriverClassicFactory.php @@ -57,8 +57,8 @@ public function buildDriver(array $config): Definition return new Definition(WebdriverClassicDriver::class, [ $config['browser'], array_merge( - $config['capabilities'], - $this->guessEnvironmentCapabilities() + $this->guessEnvironmentCapabilities(), + $config['capabilities'] ), $config['wd_host'], ]); From ea2d7bd352cdbaf75928959a046b3681180f5813 Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Tue, 6 Aug 2024 19:28:01 +0200 Subject: [PATCH 5/6] Apply fix to Selenium4Factory too --- .../ServiceContainer/Driver/Selenium4Factory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium4Factory.php b/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium4Factory.php index 84bc8f9..58a008a 100644 --- a/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium4Factory.php +++ b/src/Behat/MinkExtension/ServiceContainer/Driver/Selenium4Factory.php @@ -63,8 +63,8 @@ public function buildDriver(array $config) return new Definition('Behat\Mink\Driver\Selenium4Driver', array( $config['browser'], array_merge( - ['capabilities' => $config['capabilities']], - $this->guessEnvironmentCapabilities() + $this->guessEnvironmentCapabilities(), + $config['capabilities'] ), $config['wd_host'], )); From 6134879ff6c42c523d417142cc3673454acc9026 Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Tue, 6 Aug 2024 20:12:17 +0200 Subject: [PATCH 6/6] Use branch alias version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8028807..c97a429 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "require-dev": { "behat/mink-goutte-driver": "^1.1 || ^2.0", "phpspec/phpspec": "^6.0 || ^7.0 || 7.1.x-dev", - "mink/webdriver-classic-driver": "dev-main" + "mink/webdriver-classic-driver": "^1.0@dev" }, "extra": { "branch-alias": {