php - Invalid CSRF token: Behat test for registration form on Symfony 2 -
i'm working on test suite behat on symfony 2.7 application.
it's simple piece of code test registration form, fails because of csrf token.
what's problem configuration?
how can avoid problem?
this 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 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 specific context. */ class featurecontext extends minkcontext implements snippetacceptingcontext { use kerneldictionary; const debug = true; // make true if tests have troubles const loadfixtures = true; // activate when project has fixtures /** * @var application */ protected $application; /** * initializes context. * * every scenario gets own context instance. * can pass arbitrary arguments * context constructor through behat.yml. */ public function __construct() { } /** * clean database (and schema). * * @given /^database clean$/ */ public function cleandatabase() { print "\t\tc l e n n n g - d t b s e \n"; $this->application = new \symfony\bundle\frameworkbundle\console\application($this->kernel); $this->application->setautoexit(false); /* * in order work-around error: * pdo error - invalid catalog name: 1046 no database selected * sequence has been changed prevent 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 t b s e - c l e n e d \n"; } /** * @given /^database clean \"(.*?)\" context loaded$/ */ public function cleandatabasewithcontext($context) { $this->cleandatabase($context); } /** * first, force logout, go login page, fill informations , go requested page. * * @given /^i connected "([^"]*)" , "([^"]*)" 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 $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 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 feature file:
feature: free registration background: given database clean scenario: free user register given on "/en/register" when fill in 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 | , press "_submit" , show last response should see "app.registration.confirmed_free" , response status code should 200
finally found problem.
there 2 forms in registration page. removing second form test pass without problems.
regards,
Comments
Post a Comment