Search code examples
phpbehat

Either the step is already declared or Mink instance has not been set on Mink context class


I'm trying to use make some tests around a Drupal project (but Behat is out of it), however I'm having trouble around Mink and its session, and I must admit I have no clue about what I am doing.

Here are my files so far:

FeatureContext.php

use Drupal\DrupalExtension\Context\RawDrupalContext; #not used
use Behat\Mink\Exception\ExpectationException;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Session;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Behat\Context\Context; #not used
use Behat\Mink\Mink;
use DMore\ChromeDriver\ChromeDriver;


/**
 * Defines application features from the specific context.
 */
class FeatureContext extends MinkContext implements SnippetAcceptingContext {

  protected $mink;

  /**
   * FeatureContext constructor.
   * Initializes context.
   * PLEASE NOTE THAT I'M NOT SURE ABOUT THIS, BUT IT SEEMS TO WORK SO FAR
   * Every scenario gets its own context instance.
   * You can also pass arbitrary arguments to the
   * context constructor through behat.yml.
   */
  public function __construct() {
    $this->mink = new Mink(array(
      'browser' => new Session(new ChromeDriver('http://localhost:9222', null, 'http://www.website.rec'))
    ));

  // The rest of my custom functions

  }
}

behat.yml

default:
  suites:
    default:
      contexts:
      - FeatureContext
      - Drupal\DrupalExtension\Context\DrupalContext
      - Drupal\DrupalExtension\Context\MinkContext
      - Drupal\DrupalExtension\Context\MessageContext
      - Drupal\DrupalExtension\Context\DrushContext
    extensions:
      DMore\ChromeExtension\Behat\ServiceContainer\ChromeExtension: ~
      Behat\MinkExtension:
        browser_name: chrome
        base_url: http://www.spheria.rec
        sessions:
          default:
            chrome:
              api_url: http://localhost:9222

    Drupal\DrupalExtension:
      blackbox: ~

test.feature

Feature: Sample feature
    Scenario: Arrived on website, checking out what's around me
      Given I am an anonymous user
      And I go to "/"
      And I should see "Se connecter"
      And I should see "Nom d'utilisateur"
      And I should see "Mot de passe"
      When I fill in "admin@spheria.com" for "name"
      And I fill in "admin" for "pass"
      And I press "Se connecter"
      Then I should get a 200 HTTP response
      And the url should match "/dashboard"
      And I should see "Tableau de bord"

My problem is that if I use MinkContext in the behat file and in FeatureContext, the consoles returns me that every single function have been declared twice (at least, MincContext::PressButton but I wouldn't be surprised if the problem would happen with something else)

When I remove it from both behat.yml and FeatureContext, it doesn't recognize anything, and asks me to define these functions, which makes sense I guess.

And when I use MinkContext in only the behat file or the FeatureContext one, I get an error saying this:

Mink instance has not been set on Mink context class. Have you enabled the Mink Extension? (RuntimeException)

I'm using the DMore Chrome Driver, because I have trouble running Chrome with Selenium properly, and I have the feeling that instancing Mink in the constructor is creating some troubles.

It's an euphemism to say that I'm totally lost on what I should do.

How can I solve this problem?

Thank you in advance


Solution

  • You need to extend MinkContext only once else it will see duplicate steps for each time you extend it.

    One of the contexts from behat.yml is already extending MinkContext so you need to:

    1. remove that class and extend it in your FeatureContext

    or

    1. your FeatureContext should not extend MinkContext but RawMinkContext