1

I'm trying to use Behat and Mink to test a symfony 2.1 project.

My FeatureContext.php:

<?php 

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException,
    Behat\Behat\Context\Step;

use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;

use Behat\MinkExtension\Context\MinkContext;

//
// Require 3rd-party libraries here:
//
//   require_once 'PHPUnit/Autoload.php';
//   require_once 'PHPUnit/Framework/Assert/Functions.php';
//

/**
 * Features context.
 */
class FeatureContext extends Behat\MinkExtension\Context\MinkContext {

    /**
     * Initializes context.
     * Every scenario gets it's own context object.
     *
     * @param array $parameters context parameters (set them up through behat.yml)
     */
    public function __construct(array $parameters)
    {
        // Initialize your context here
    }

//
// Place your definition and hook methods here:
//
//    /**
//     * @Given /^I have done something with "([^"]*)"$/
//     */
//    public function iHaveDoneSomethingWith($argument)
//    {
//        doSomethingWith($argument);
//    }
//
}

composer.json

     "behat/behat": ">=2.2.2",
    "behat/mink":  ">=1.3.2",
    "behat/symfony2-extension":     "*",
    "behat/mink-extension":         "*",
    "behat/mink-browserkit-driver": "*",
    "behat/mink-selenium-driver":   "*"

app/config/behat.yml

default:
  extensions:
    Behat\Symfony2Extension\Extension:
      mink_driver: true
      kernel:
        env: test
        debug: true
    Behat\MinkExtension\Extension:
      base_url: 'http://localhost:8080/app_test.php/'
      default_session: symfony2
      javascript_session: selenium
      selenium:
        host: 127.0.0.1
        port: 4444

When I do ./bin/behat I get:

Feature: Login In order to login As a user I need to be able to validate the username and password

Scenario: Link to login page # features/login.feature:7 PHP Fatal error: Call to a member function getSession() on a non-object in vendor/behat/mink-extension/src/Behat/MinkExtension/Context/RawMinkContext.php on line 81

Fatal error: Call to a member function getSession() on a non-object in vendor/behat/mink-extension/src/Behat/MinkExtension/Context/RawMinkContext.php on line 81

Any idea?

v.

Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
Vittore
  • 240
  • 3
  • 15

2 Answers2

2

Your behat.yml should not be located in app/config/behat.yml , but in your/project/root/behat.yml

Dmitry Glushonkov
  • 315
  • 1
  • 2
  • 15
0

Thanks for @Stuart and @spiritoo answers here.

Firstly, move the behat.yml from /config to project root.

Secondly, content for the behat.yml:

default:
suites:
    my_suite:
        contexts:
            - FeatureContext
extensions:
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
        base_url: http://en.wikipedia.org
        goutte: ~
        sessions:
            default:
                symfony2: ~

Finally, in project root, run:

vendor/bin/behat features/{YOUR TEST FILE HERE}

My composer.json file:


"require-dev": {
    "behat/behat": "^3.3",
    "behat/mink": "^1.7",
    "behat/mink-extension": "^2.2",
    "behat/mink-browserkit-driver": "^1.3",
    "behat/mink-goutte-driver": "^1.2",
    "behat/symfony2-extension": "^2.1",
    "behat/mink-selenium2-driver": "^1.3"
  }
Community
  • 1
  • 1
Emily
  • 130
  • 1
  • 8