Laravel behat CSRF token

278 Views Asked by At

I'm trying to make first behat test, but when i run it i got TokenMis... error. I know why. Becouse it not rendering _token to my form. I check with:

$this->getSession()->getPage()->getHtml()

and I got:

<input type="hidden" name="_token" value="">

Normaly it must be:

<input type="hidden" name="_token" value="Atf3lzQ7McA4zQorUu089X3zPsrLVRE8TvQkVOtm">

So my question is what i doing wrong?

My feature looks:

Feature: User Login
 To test if user can login to system

 Scenario: Successful Authentication
   Given I sign in "email" "pw"
   Then I should be sign in

Configuration:

default:
  autoload:
    '': %paths.base%/features/bootstrap
  suites:
    default:
      contexts:
        - 'FeatureContext'
      paths:
        - %paths.base%/features/default.feature
    user_login_module:
      contexts:
        - 'UserLoginContext'
      paths:
        - %paths.base%/features/user-login.feature
  extensions:
    Laracasts\Behat:
      env_path: .env.laravel.behat
    Behat\MinkExtension:
      default_session: laravel
      base_url: http://localhost:8080
      laravel: ~

FeatureContext file (UserLoginContext):

<?php

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
use Laracasts\Behat\Context\Migrator;
use Laracasts\Behat\Context\DatabaseTransactions;
use PHPUnit_Framework_Assert as PHPUnit;

class UserLoginContext extends MinkContext implements Context
{
    use Migrator, DatabaseTransactions;

    /**
     * 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()
    {
    }

    /**
     * Loads Laravel classes
     *
     * @static
     * @beforeSuite
     * @return mixed
     */
    public static function bootstrapLaravel ()
    {
        $app = require __DIR__.'/../../bootstrap/app.php';

        $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

        return $app;
    }

    /**
     * @Given I sign in :email :password
     */
    public function iSignIn($email, $password)
    {
        $this->visit('admin-panel');

        $this->fillField('email', $email);
        $this->fillField('password', $password);
        $this->pressButton('Prisijungti');
    }

    /**
     * @Then I should be sign in
     */
    public function iShouldBeSignIn()
    {
        PHPUnit::assertTrue(Auth::guard('admin')->check());

        //$this->assertPageAddress('admin-panel/home');
    }

}

Same as and with error variables. Throws error undefined errors variable

1

There are 1 best solutions below

1
On BEST ANSWER

Please include this CSRF helper function in the blade file. This will generate the following code <input type="hidden" name="_token" value="2WXMw1BMY2o5irBKcXyzJBDeai9colsqFuVpNGXm"> automatically.

 <form  action = "{{ url('test') }}" method="POST">
 {{ csrf_field() }} 
 ....
 ....
 </form>

EDIT 1:

I think there is an issue with the behat laravel extension itself and not with your code.