Home Ask Login Register

Developers Planet

Your answer is one click away!

Mauro Casula February 2016

Invalid CSRF token: Behat test for registration form on Symfony 2

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:

        type: symfony_bundle
        bundle: 'AppBundle'
    Behat\Symfony2Extension: ~
      base_url: http://application.local/app_test.php
      browser_name: chrome
          symfony2: ~
      show_auto: true
      show_cmd: echo %s

And this is my FeatureContext's class:


    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$/


Mauro Casula February 2016

Finally I found the problem.

There were two forms in the registration page. Removing the second form the test pass without problems.


Post Status

Asked in February 2016
Viewed 3,461 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life