Refactored the configuration of sessions

The configuration now makes sessions a first-class citizen instead of
being centered around drivers. This allows defining several session based
on the same driver type.
Instead of allowing other extensions to register their own sessions to add
new drivers, they can now register a DriverFactory in the MinkExtension
during the extension initialization to make a new driver type available.
This commit is contained in:
Christophe Coevoet
2014-01-11 00:04:23 +01:00
parent 589cd05897
commit 3ee16c4b87
18 changed files with 943 additions and 399 deletions

View File

@@ -0,0 +1,52 @@
<?php
/*
* This file is part of the Behat MinkExtension.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\MinkExtension\ServiceContainer\Driver;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\Definition;
/**
* @author Christophe Coevoet <stof@notk.org>
*/
interface DriverFactory
{
/**
* Gets the name of the driver being configured.
*
* This will be the key of the configuration for the driver.
*
* @return string
*/
public function getDriverName();
/**
* Defines whether a session using this driver is eligible as default javascript session
*
* @return boolean
*/
public function supportsJavascript();
/**
* Setups configuration for the driver factory.
*
* @param ArrayNodeDefinition $builder
*/
public function configure(ArrayNodeDefinition $builder);
/**
* Builds the service definition for the driver.
*
* @param array $config
*
* @return Definition
*/
public function buildDriver(array $config);
}

View File

@@ -0,0 +1,89 @@
<?php
/*
* This file is part of the Behat MinkExtension.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\MinkExtension\ServiceContainer\Driver;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\Definition;
/**
* @author Christophe Coevoet <stof@notk.org>
*/
class GoutteFactory implements DriverFactory
{
/**
* {@inheritdoc}
*/
public function getDriverName()
{
return 'goutte';
}
/**
* {@inheritdoc}
*/
public function supportsJavascript()
{
return false;
}
/**
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder)
{
$builder
->children()
->arrayNode('server_parameters')
->useAttributeAsKey('key')
->prototype('variable')->end()
->end()
->arrayNode('guzzle_parameters')
->useAttributeAsKey('key')
->prototype('variable')->end()
->validate()
->always()
->then(function ($v) {
$v['redirect.disable'] = true;
return $v;
})
->end()
->end()
->end()
;
}
/**
* {@inheritdoc}
*/
public function buildDriver(array $config)
{
if (!class_exists('Behat\Mink\Driver\GoutteDriver')) {
throw new \RuntimeException(
'Install MinkGoutteDriver in order to use goutte driver.'
);
}
$clientDefinition = new Definition('Behat\Mink\Driver\Goutte\Client', array(
$config['server_parameters'],
));
$clientDefinition->addMethodCall('setClient', array(
new Definition('Guzzle\Http\Client', array(
null,
$config['guzzle_parameters'],
)),
));
return new Definition('Behat\Mink\Driver\GoutteDriver', array(
$clientDefinition,
));
}
}

View File

@@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Behat MinkExtension.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\MinkExtension\ServiceContainer\Driver;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\Definition;
class SahiFactory implements DriverFactory
{
/**
* {@inheritdoc}
*/
public function getDriverName()
{
return 'sahi';
}
/**
* {@inheritdoc}
*/
public function supportsJavascript()
{
return true;
}
/**
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder)
{
$builder
->children()
->scalarNode('sid')->defaultNull()->end()
->scalarNode('host')->defaultValue('localhost')->end()
->scalarNode('port')->defaultValue(9999)->end()
->scalarNode('browser')->defaultNull()->end()
->scalarNode('limit')->defaultValue(600)->end()
->end()
;
}
/**
* {@inheritdoc}
*/
public function buildDriver(array $config)
{
if (!class_exists('Behat\Mink\Driver\SahiDriver')) {
throw new \RuntimeException(
'Install MinkSahiDriver in order to use sahi driver.'
);
}
return new Definition('Behat\Mink\Driver\SahiDriver', array(
'%mink.browser_name%',
new Definition('Behat\SahiClient\Client', array(
new Definition('Behat\SahiClient\Connection', array(
$config['sid'],
$config['host'],
$config['port'],
$config['browser'],
$config['limit'],
)),
)),
));
}
}

View File

@@ -0,0 +1,92 @@
<?php
/*
* This file is part of the Behat MinkExtension.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\MinkExtension\ServiceContainer\Driver;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\Definition;
class SaucelabsFactory implements DriverFactory
{
/**
* {@inheritdoc}
*/
public function getDriverName()
{
return 'saucelabs';
}
/**
* {@inheritdoc}
*/
public function supportsJavascript()
{
return true;
}
/**
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder)
{
$builder
->children()
->scalarNode('username')->defaultValue(getenv('SAUCE_USERNAME'))->end()
->scalarNode('access_key')->defaultValue(getenv('SAUCE_ACCESS_KEY'))->end()
->booleanNode('connect')->defaultFalse()->end()
->scalarNode('browser')->defaultValue('firefox')->end()
->arrayNode('capabilities')
->addDefaultsIfNotSet()
->normalizeKeys(false)
->children()
->scalarNode('name')->defaultValue('Behat feature suite')->end()
->scalarNode('platform')->defaultValue('Linux')->end()
->scalarNode('version')->defaultValue('21')->end()
->scalarNode('selenium-version')->defaultValue('2.31.0')->end()
->scalarNode('max-duration')->defaultValue('300')->end()
->scalarNode('deviceType')->defaultNull()->end()
->scalarNode('deviceOrientation')->defaultNull()->end()
->end()
->end()
->end()
;
}
/**
* {@inheritdoc}
*/
public function buildDriver(array $config)
{
if (!class_exists('Behat\Mink\Driver\Selenium2Driver')) {
throw new \RuntimeException(
'Install MinkSelenium2Driver in order to use saucelabs driver.'
);
}
$capabilities = $config['capabilities'];
$capabilities['tags'] = array(php_uname('n'), 'PHP '.phpversion());
if (getenv('TRAVIS_JOB_NUMBER')) {
$capabilities['tunnel-identifier'] = getenv('TRAVIS_JOB_NUMBER');
$capabilities['build'] = getenv('TRAVIS_BUILD_NUMBER');
$capabilities['tags'] = array('Travis-CI', 'PHP '.phpversion());
}
$host = 'ondemand.saucelabs.com';
if ($config['connect']) {
$host = 'localhost:4445';
}
return new Definition('Behat\Mink\Driver\Selenium2Driver', array(
$config['browser'],
$capabilities,
sprintf('%s:%s@%s/wd/hub', $config['username'], $config['access_key'], $host),
));
}
}

View File

@@ -0,0 +1,130 @@
<?php
/*
* This file is part of the Behat MinkExtension.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\MinkExtension\ServiceContainer\Driver;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\Definition;
class Selenium2Factory implements DriverFactory
{
/**
* {@inheritdoc}
*/
public function getDriverName()
{
return 'selenium2';
}
/**
* {@inheritdoc}
*/
public function supportsJavascript()
{
return true;
}
/**
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder)
{
$builder
->children()
->scalarNode('browser')->defaultValue('%mink.browser_name%')->end()
->arrayNode('capabilities')
->addDefaultsIfNotSet()
->normalizeKeys(false)
->children()
->scalarNode('browserName')->defaultValue('firefox')->end()
->scalarNode('version')->defaultValue('9')->end()
->scalarNode('platform')->defaultValue('ANY')->end()
->scalarNode('browserVersion')->defaultValue('9')->end()
->scalarNode('browser')->defaultValue('firefox')->end()
->scalarNode('ignoreZoomSetting')->defaultValue('false')->end()
->scalarNode('name')->defaultValue('Behat Test')->end()
->scalarNode('deviceOrientation')->defaultValue('portrait')->end()
->scalarNode('deviceType')->defaultValue('tablet')->end()
->scalarNode('selenium-version')->defaultValue('2.31.0')->end()
->scalarNode('max-duration')->defaultValue('300')->end()
->booleanNode('javascriptEnabled')->end()
->booleanNode('databaseEnabled')->end()
->booleanNode('locationContextEnabled')->end()
->booleanNode('applicationCacheEnabled')->end()
->booleanNode('browserConnectionEnabled')->end()
->booleanNode('webStorageEnabled')->end()
->booleanNode('rotatable')->end()
->booleanNode('acceptSslCerts')->end()
->booleanNode('nativeEvents')->end()
->booleanNode('passed')->end()
->booleanNode('record-video')->end()
->booleanNode('record-screenshots')->end()
->booleanNode('capture-html')->end()
->booleanNode('disable-popup-handler')->end()
->arrayNode('proxy')
->children()
->scalarNode('proxyType')->end()
->scalarNode('proxyAuthconfigUrl')->end()
->scalarNode('ftpProxy')->end()
->scalarNode('httpProxy')->end()
->scalarNode('sslProxy')->end()
->end()
->validate()
->ifTrue(function ($v) {
return empty($v);
})
->thenUnset()
->end()
->end()
->arrayNode('firefox')
->children()
->scalarNode('profile')
->validate()
->ifTrue(function ($v) {
return !file_exists($v);
})
->thenInvalid('Cannot find profile zip file %s')
->end()
->end()
->scalarNode('binary')->end()
->end()
->end()
->arrayNode('chrome')
->children()
->arrayNode('switches')->prototype('scalar')->end()->end()
->scalarNode('binary')->end()
->arrayNode('extensions')->prototype('scalar')->end()->end()
->end()
->end()
->end()
->end()
->scalarNode('wd_host')->defaultValue('http://localhost:4444/wd/hub')->end()
->end()
;
}
/**
* {@inheritdoc}
*/
public function buildDriver(array $config)
{
if (!class_exists('Behat\Mink\Driver\Selenium2Driver')) {
throw new \RuntimeException(
'Install MinkSelenium2Driver in order to use selenium2 driver.'
);
}
return new Definition('Behat\Mink\Driver\Selenium2Driver', array(
$config['browser'],
$config['capabilities'],
$config['wd_host'],
));
}
}

View File

@@ -0,0 +1,68 @@
<?php
/*
* This file is part of the Behat MinkExtension.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\MinkExtension\ServiceContainer\Driver;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\Definition;
class SeleniumFactory implements DriverFactory
{
/**
* {@inheritdoc}
*/
public function getDriverName()
{
return 'selenium';
}
/**
* {@inheritdoc}
*/
public function supportsJavascript()
{
return true;
}
/**
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder)
{
$builder
->children()
->scalarNode('host')->defaultValue('127.0.0.1')->end()
->scalarNode('port')->defaultValue(4444)->end()
->scalarNode('browser')->defaultValue('*%mink.browser_name%')->end()
->end()
;
}
/**
* {@inheritdoc}
*/
public function buildDriver(array $config)
{
if (!class_exists('Behat\Mink\Driver\SeleniumDriver')) {
throw new \RuntimeException(
'Install MinkSeleniumDriver in order to activate selenium session.'
);
}
return new Definition('Behat\Mink\Driver\SeleniumDriver', array(
$config['browser'],
'%mink.base_url%',
new Definition('Selenium\Client', array(
$config['host'],
$config['port'],
)),
));
}
}

View File

@@ -0,0 +1,79 @@
<?php
/*
* This file is part of the Behat MinkExtension.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\MinkExtension\ServiceContainer\Driver;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\Definition;
class ZombieFactory implements DriverFactory
{
/**
* {@inheritdoc}
*/
public function getDriverName()
{
return 'zombie';
}
/**
* {@inheritdoc}
*/
public function supportsJavascript()
{
return true;
}
/**
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder)
{
$builder
->children()
->scalarNode('host')->defaultValue('127.0.0.1')->end()
->scalarNode('port')->defaultValue(8124)->end()
->booleanNode('auto_server')->defaultTrue()->end()
->scalarNode('node_bin')->defaultValue('node')->end()
->scalarNode('server_path')->defaultNull()->end()
->scalarNode('threshold')->defaultValue(2000000)->end()
->scalarNode('node_modules_path')->defaultValue('')->end()
->end()
;
}
/**
* {@inheritdoc}
*/
public function buildDriver(array $config)
{
if (!class_exists('Behat\Mink\Driver\ZombieDriver')) {
throw new \RuntimeException(
'Install MinkZombieDriver in order to use zombie driver.'
);
}
return new Definition('Behat\Mink\Driver\ZombieDriver', array(
new Definition('Behat\Mink\Driver\NodeJS\Server\ZombieServer', array(
$config['host'],
$config['port'],
$config['node_bin'],
$config['server_path'],
$config['threshold'],
$config['node_modules_path'],
)),
new Definition('Behat\Mink\Driver\NodeJS\Connection', array(
$config['host'],
$config['port'],
)),
$config['auto_server'],
));
}
}