Andy Hall
Andy Hall

Reputation: 464

Getting `Mink instance has not been set on Mink context class. Have you enabled the Mink Extension?` with Behat 3

In my composer.json:

{
    ...
    "behat/behat": "^3.6",
    "behat/mink-extension": "^2.3",
    "behat/mink-goutte-driver": "^1.2",
    "behat/mink-selenium2-driver": "^1.4"
}

My behat.yml:

default:
  autoload:
    '': %paths.base%/features/bootstrap
  suites:
    default:
      path:
        - %paths.base%/features
      contexts:
        - FeatureContext
        - Behat\MinkExtension\Context\MinkContext
        - Behat\MinkExtension\Context\RawMinkContext
  extensions:
    Behat\MinkExtension:
      base_url: https://localhost:8443
      goutte: ~      
      selenium2: ~
      javascript_session: selenium2
      files_path: 'vendor'
      sessions:
        default:
          selenium2: ~

My feature context:

class FeatureContext extends MinkContext
{
/** @var Behat\Mink\Element\DocumentElement */
protected $page;

/**
 * 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()
{
    $this->page = $this->getSession()->getPage();
}

/**
 * @Given /^I am not logged in$/
 */
public function iAmNotLoggedIn()
{
    // Do nothing
}

/**
 * @When /^I visit \.*$/
 */
public function iVisit($page)
{
    $this->getSession()->visit('/login');
}

/**
 * @Then /^I should see the login page$/
 */
public function iShouldSeeTheLoginPage()
{
    $this->assertSession()->elementExists('css', '#username');
    $this->assertSession()->elementExists('css', '#password');
    $this->assertSession()->elementExists('css', '#submit');
}
}

My feature file:

Feature: Viewing the main page as an unauthenticated user

@javascript
Scenario: View the main page
    Given I am not logged in
    When I visit "/login"
    Then I should see the login page

When I attempt to run the behat command I get the error Mink instance has not been set on Mink context class. Have you enabled the Mink Extension?. I have looked around at questions surrounding instances of this error and followed as many of the steps to fix as I could, but none of them seem to be doing the trick. Is there anything I am missing?

Upvotes: 0

Views: 204

Answers (1)

Andy Hall
Andy Hall

Reputation: 464

So in the constructor of the FeatureContext I am trying to access the session before it has been fully instantiated. Removing that line fixed the issue.

Upvotes: 0

Related Questions