I'm working on a test suite with behat on a Symfony 2.7 application.
It's a very simple piece of code to test the registration form, but it fails because of the CSRF token.
What's the problem with my configuration?
How can I avoid this problem?
This is my behat.yml:
default:
suites:
frontend_test_suite:
type: symfony_bundle
bundle: 'AppBundle'
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: http://application.local/app_test.php
browser_name: chrome
sessions:
default:
symfony2: ~
show_auto: true
show_cmd: echo %s
And this is my FeatureContext's class:
<?php
namespace MobileLeaves\Bundles\AppBundle\Features\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext implements SnippetAcceptingContext
{
use KernelDictionary;
const DEBUG = true; // make true if your tests have any troubles
const LOADFIXTURES = true; // activate when the project has fixtures
/**
* @var Application
*/
protected $application;
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
}
/**
* Clean the database (and schema).
*
* @Given /^database is clean$/
*/
public function cleanDatabase()
{
print "\t\tC L E A N N I N G - D A T A B A S E \n";
$this->application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->kernel);
$this->application->setAutoExit(false);
/*
* In order to work-around this error:
* PDO Error - Invalid catalog name: 1046 No database selected
* the sequence has been changed to prevent this error
*/
//$this->runCommand("doctrine:database:drop", array("--force" => null));
$this->runCommand('doctrine:database:create');
$this->runCommand('doctrine:schema:drop', array('--force' => null));
$this->runCommand('doctrine:schema:create');
if (self::LOADFIXTURES) {
$this->fixturesAreLoaded();
}
print "\t\tD A T A B A S E - C L E A N E D \n";
}
/**
* @Given /^database is clean with \"(.*?)\" context loaded$/
*/
public function cleanDatabaseWithContext($context)
{
$this->cleanDatabase($context);
}
/**
* First, force logout, then go to the login page, fill the informations and finally go to requested page.
*
* @Given /^I am connected with "([^"]*)" and "([^"]*)" on "([^"]*)"$/
*
* @param string $login
* @param string $rawPassword
* @param string $url
*/
public function iAmConnectedWithOn($login, $rawPassword, $url)
{
$this->visit('admin/logout');
$this->visit('admin/login');
$this->fillField('_username', $login);
$this->fillField('_password', $rawPassword);
$this->pressButton('_submit');
$this->visit($url);
}
/**
* @param $command
* @param array $options
*/
protected function runCommand($command, Array $options = array())
{
if (!self::DEBUG) {
$options['-q'] = null;
}
$options['--env'] = 'test';
foreach ($options as $option => $value) {
$command .= ' '.$option.($value ? '='.$value : '');
}
$output = new BufferedOutput();
$exitCode = $this->application->run(new StringInput($command), $output);
if (self::DEBUG || $exitCode) {
print sprintf("%s\t[exitCode=%d]\n%s", $command, $exitCode, $output->fetch());
}
}
/**
* @Given /^fixtures are loaded$/
*/
public function fixturesAreLoaded()
{
$this->runCommand('doctrine:fixtures:load -n');
}
/**
* {@inheritdoc}
*/
public static function getAcceptedSnippetType()
{
return 'regex';
}
/**
* Sets Kernel instance.
*
* @param KernelInterface $kernel
*/
public function setKernel(KernelInterface $kernel) {
$this->kernel = $kernel;
}
}
And this is my feature file:
Feature: Free Registration
Background:
Given database is clean
Scenario: Free user register
Given I am on "/en/register"
When I fill in the following:
| fos_user_registration_form[email] | maurocasula@gmail.com |
| fos_user_registration_form[profile][name] | NAME LASTNAME |
| fos_user_registration_form[plainPassword][first] | pass |
| fos_user_registration_form[plainPassword][second] | pass |
And I press "_submit"
And show last response
Then I should see "app.registration.confirmed_free"
And the response status code should be 200
Finally I found the problem.
There were two forms in the registration page. Removing the second form the test pass without problems.
Regards,