composer require friends-of-behat/mink-extension
behat.yml:
default:
extensions:
FriendsOfBehat\MinkExtension:
base_url: 'http://localhost'
browser_name: chrome
sessions:
default: symfony2
login.feature) and a context class extending MinkContext:
use Behat\MinkExtension\Context\MinkContext;
class FeatureContext extends MinkContext {
/**
* @Given I am on the login page
*/
public function iAmOnTheLoginPage() {
$this->visitPath('/login');
}
}
Run tests:
behat login.feature
Session Management:
createSession() to dynamically create sessions (e.g., for parallel testing):
$session = $this->createSession('selenium2');
$session->visit('/dashboard');
$this->getSession().Element Interaction:
$this->assertSession()->elementExists('css', '.user-menu')->isVisible();
fillField() and submit with pressButton().Hooks for Setup/Teardown:
use Behat\Testwork\Hook\Scope\BeforeScenarioScope;
public function beforeScenario(BeforeScenarioScope $scope) {
$session = $this->createSession('selenium2');
$session->start();
$scope->getEnvironment()->getContext(MinkContext::class)->setSession($session);
}
Laravel-Specific:
Use laravel driver for testing Laravel apps:
sessions:
laravel: laravel
Configure in behat.yml with:
FriendsOfBehat\MinkExtension:
laravel:
env: testing
path: /path/to/laravel/storage/logs
Custom Drivers:
Extend Mink\Driver\DriverInterface and register via:
sessions:
custom: custom_driver
Parallel Testing:
Use parallel extension with session isolation:
extensions:
FriendsOfBehat\MinkExtension:
sessions:
default: selenium2
parallel:
- selenium2
- selenium2
Session Leaks:
$session->close() in AfterScenario hooks or use MinkContext::tearDown():
use Behat\Testwork\Hook\Scope\AfterScenarioScope;
public function afterScenario(AfterScenarioScope $scope) {
$this->getSession()->close();
}
Base URL Overrides:
base_url in behat.yml is ignored if visit() uses absolute paths.visitPath() for relative paths or override dynamically:
$this->getSession()->getDriver()->setBaseUrl('https://staging.example.com');
Deprecated Methods:
getMink(); use $this->getSession() or $this->getMink() (deprecated in v2.0+).assertSession()->[method] over direct assert*() calls.Enable Mink Debug:
FriendsOfBehat\MinkExtension:
debug: true
Logs session details to behat.log.
Selenium Grid Issues:
hub_url is configured in behat.yml:
selenium2:
wd_host: http://hub.example.com/wd/hub
--stop-on-failure flag to isolate flaky tests:
behat --stop-on-failure
Custom Contexts:
Extend MinkAwareContext to inject Mink into any context:
use Behat\MinkExtension\Context\MinkAwareContext;
class ApiContext extends MinkAwareContext {
public function __construct(Mink $mink) {
$this->setMink($mink);
}
}
Hooks for Drivers:
Override getMink() in contexts to swap drivers dynamically:
public function getMink() {
return $this->createMink([
'selenium2' => [
'browser' => 'firefox',
'wd_host' => 'http://localhost:4444/wd/hub',
],
]);
}
Translation Overrides:
Override language files in vendor/friends-of-behat/mink-extension/i18n/ by copying them to your project’s i18n/ directory.
BeforeScenario:
public function beforeScenario() {
\Illuminate\Support\Facades\Session::flush();
}
mix() or asset() helpers in behat.yml for local development:
base_url: 'http://localhost:8000'
How can I help you explore Laravel packages today?