Skip to content

Instantly share code, notes, and snippets.

@gggeek
Last active August 29, 2015 14:10
Show Gist options
  • Save gggeek/d71b10e21e0480a562cf to your computer and use it in GitHub Desktop.
Save gggeek/d71b10e21e0480a562cf to your computer and use it in GitHub Desktop.
Injecting Symfony services into Behat PageObjects
namespace Kaliop\AProject\PageObjects\Pages;
use SensioLabs\Behat\PageObjectExtension\PageObject\Page;
use Behat\Mink\Session;
use SensioLabs\Behat\PageObjectExtension\PageObject\Factory;
abstract class BasePage extends Page
{
protected $aService;
public function __construct(Session $session, Factory $factory, array $parameters = array())
{
parent::__construct($session, $factory, $parameters);
// The parameters arriving here come from behat.yml, but the Factory class from KTools adds some services
$this->aService = $parameters['services']['a_service'];
}
extensions:
Kaliop\Behat\KToolsExtension: ~
SensioLabs\Behat\PageObjectExtension:
factory:
id: kaliop.ktools_extension.page_factory.serviceaware
namespace Kaliop\Behat\KToolsExtension\PageObject\Factory;
use SensioLabs\Behat\PageObjectExtension\PageObject\Factory\DefaultFactory;
use Behat\Mink\Mink;
use SensioLabs\Behat\PageObjectExtension\PageObject\Factory\ClassNameResolver;
/**
* Allows us to pass Symfony services into PageObjects
*/
class ServiceAwareFactory extends DefaultFactory
{
public function __construct(Mink $mink, ClassNameResolver $classNameResolver, array $pageParameters, $services = array())
{
$pageParameters['services'] = $services;
parent::__construct($mink, $classNameResolver, $pageParameters);
}
}
services:
kaliop.ktools_extension.page_factory.serviceaware:
class: "%kaliop.ktools_extension.page_factory.serviceaware.class%"
arguments: [@mink, @sensio_labs.page_object_extension.class_name_resolver, %sensio_labs.page_object_extension.page_factory.page_parameters%, {a_service: @kaliop.ktools_extension.a.service}]
kaliop.ktools_extension.a.service
etc: ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment