Mink provides an interface for web drivers (like Goutte and Selenium) as well as a MinkContext
which, when extended, provides additional web language for our steps.
To install Mink (and the default Goutte driver):
$ composer require --dev behat/mink-extension="^2.0"
$ composer require --dev behat/mink-goutte-driver="^1.0"
Then extend your context with MinkContext
:
<?php
use Behat\MinkExtension\Context\MinkContext;
class FeatureContext extends MinkContext … {
…
}
You can see the entire list of syntax available in your Behat install with the following command:
$ ./vendor/bin/behat -dl
Given /^(?:|I )am on "(?P<page>[^"]+)"$/
When /^(?:|I )reload the page$/
When /^(?:|I )move backward one page$/
When /^(?:|I )move forward one page$/
When /^(?:|I )press "(?P<button>(?:[^"]|\\")*)"$/
When /^(?:|I )follow "(?P<link>(?:[^"]|\\")*)"$/
When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/
You then need to configure Mink to indicate where the website you want to test is located and which Web Drivers to use (Goutte by default):
# ./behat.yml
default:
extensions:
Behat\MinkExtension:
base_url: "[your website URL]"
sessions:
default:
goutte: ~
Here is an example of a scenario using only the Mink provided steps:
# ./features/Authentication.feature
Feature: Authentication
As a security conscious developer I wish to ensure that only valid users can access our website.
Scenario: Login in successfully to my website
When I am on "/login"
And I fill in "email" with "[email protected]"
And I fill in "password" with "my_password"
And I press "Login"
Then I should see "Successfully logged in"
Scenario: Attempt to login with invalid credentials
When I am on "/login"
And I fill in "email" with "[email protected]"
And I fill in "password" with "not_my_password"
And I press "Login"
Then I should see "Login failed"
You can now test this by running the feature via Behat:
./vendor/bin/behat features/Authentication.feature
You can create your own steps using MinkContext
for common steps (for instance logging in is a very common operation):
Feature: Authentication
As a security conscious developer I wish to ensure that only valid users can access our website.
Scenario: Login in successfully to my website
Given I login as "[email protected]" with password "my_password"
Then I should see "Successfully logged in"
Scenario: Attempt to login with invalid credentials
Given I login as "[email protected]" with password "not_my_password"
Then I should see "Login failed"
You will need to extend your context file with the MinkContext
to get access to the web drivers and page interactions:
<?php
use Behat\MinkExtension\Context\MinkContext;
class FeatureContext extends MinkContext {
/**
* @Given I login as :username with password :password
*/
public function iLoginAsWithPassword($username, $password) {
$this->visit("/login");
$this->fillField("email", $username);
$this->fillField("password", $password);
$this->pressButton("Login");
}
}
Mink also provides CSS selectors in most of it's pre-provided calls which allows you to identify elements on the page using constructs such as this:
When I click on "div[id^='some-name']"
And I click on ".some-class:first"
And I click on "//html/body/table/thead/tr/th[first()]"