Skip to content

Commit

Permalink
Merge pull request #26 from uuf6429/feature/implement-webdriver-class…
Browse files Browse the repository at this point in the history
…ic-extension

Implement WebdriverClassicDriver factory
  • Loading branch information
stof authored Oct 3, 2024
2 parents 6e78512 + 6134879 commit 344b081
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 38 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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": "^1.0@dev"
},
"replace": {
"behat/mink-extension": "self.version"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace spec\Behat\MinkExtension\ServiceContainer\Driver;

use PhpSpec\ObjectBehavior;
use Behat\MinkExtension\ServiceContainer\Driver\DriverFactory;

class WebdriverClassicFactorySpec extends ObjectBehavior
{
public function it_is_a_driver_factory(): void
{
$this->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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Behat\MinkExtension\ServiceContainer\Driver;

/**
* @internal
*/
trait EnvironmentCapabilities
{
private function guessEnvironmentCapabilities(): array
{
switch (true) {
case (bool)getenv('TRAVIS_JOB_NUMBER'):
return [
'tunnel-identifier' => 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,
],
];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class Selenium2Factory implements DriverFactory
{
use EnvironmentCapabilities;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -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'],
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class Selenium4Factory implements DriverFactory
{
use EnvironmentCapabilities;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -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(
$this->guessEnvironmentCapabilities(),
$config['capabilities']
),
$config['wd_host'],
));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Behat\MinkExtension\ServiceContainer\Driver;

use Mink\WebdriverClassicDriver\WebdriverClassicDriver;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\Definition;

class WebdriverClassicFactory implements DriverFactory
{
use EnvironmentCapabilities;

/**
* {@inheritdoc}
*/
public function getDriverName(): string
{
return 'webdriver_classic';
}

/**
* {@inheritdoc}
*/
public function supportsJavascript(): bool
{
return true;
}

/**
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder): void
{
$builder
->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(
$this->guessEnvironmentCapabilities(),
$config['capabilities']
),
$config['wd_host'],
]);
}
}
2 changes: 2 additions & 0 deletions src/Behat/MinkExtension/ServiceContainer/MinkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 344b081

Please sign in to comment.