atk4/behat-mink-selenium2-driver
Selenium2 driver for Behat Mink (ATK4 fork), enabling browser-based acceptance tests via Selenium/WebDriver. Use it to run Mink test suites against real browsers for end-to-end UI testing in PHP projects.
Installation Add the package via Composer:
composer require atk4/behat-mink-selenium2-driver
Ensure behat/mink and behat/mink-selenium2-driver (or similar) are also installed.
Configure Mink in behat.yml
Update your Behat config to use the ATK4 driver:
default:
extensions:
Behat\MinkExtension:
base_url: 'http://your-laravel-app.test'
goutte: ~
selenium2: ~
drivers:
selenium2:
host: 'localhost'
port: 4444
browser: 'chrome'
atk4:
class: 'Atk4\Mink\Driver\Selenium2\Atk4Driver'
First Use Case: Basic Test Write a feature file to test a Laravel route:
Feature: Login Test
Scenario: User logs in successfully
Given I am on "/login"
When I fill in "email" with "user@example.com"
And I fill in "password" with "password123"
And I press "Login"
Then I should see "Dashboard"
Laravel Session Handling Use the driver’s ability to persist sessions across tests:
// In a Behat context
public function __construct() {
$this->getSession()->start();
}
Dynamic URL Generation Leverage Laravel’s URL helpers in Behat contexts:
use Illuminate\Support\Facades\URL;
public function visitLaravelRoute($route) {
$url = URL::to($route);
$this->getSession()->visit($url);
}
Form Submission with CSRF Tokens Extract CSRF tokens from Laravel’s meta tags:
public function fillFormWithCsrf($form, $fields) {
$csrfToken = $this->getSession()->getPage()->find('meta[name="csrf-token"]')->getAttribute('content');
$fields['_token'] = $csrfToken;
foreach ($fields as $name => $value) {
$this->getSession()->getPage()->fillField($name, $value);
}
$this->getSession()->getPage()->pressButton('submit');
}
Database Testing
Use Laravel’s DatabaseTransactions trait to reset the DB after each test:
use Illuminate\Foundation\Testing\DatabaseTransactions;
class FeatureContext extends MinkContext {
use DatabaseTransactions;
}
Selenium Server Dependency
docker run -d -p 4444:4444 selenium/standalone-chrome
behat.yml:
suites:
default:
before_scenarios:
- Atk4\Mink\Driver\Selenium2\Atk4Driver::ensureSeleniumRunning
Headless Mode Quirks
args in the driver config:
drivers:
selenium2:
atk4:
args:
- '--headless=new'
- '--disable-gpu'
Laravel Mix Assets
npm run dev or npm run prod in a pre-test hook.Slow Tests
@mink tags to skip Selenium tests in CI:
@mink
Scenario: Slow Selenium test
Screenshots on Failure
Add this to your behat.yml:
extensions:
Behat\MinkExtension:
selenium2:
atk4:
screenshot_on_failure: true
Log Driver Output Enable Selenium logs:
drivers:
selenium2:
atk4:
log_level: 'info'
Inspect Elements
Use the executeScript method to debug:
$this->getSession()->executeScript('return document.body.innerHTML');
Custom Wait Conditions Extend the driver for Laravel-specific waits:
use Atk4\Mink\Driver\Selenium2\Atk4Driver;
class LaravelAtk4Driver extends Atk4Driver {
public function waitForAJAX() {
return $this->wait(10, function () {
return $this->executeScript('return jQuery.active == 0');
});
}
}
Laravel Auth Context Create a context for auth flows:
class AuthContext implements Context {
public function loginAs($user) {
$this->visit('/login');
$this->fillField('email', $user['email']);
$this->fillField('password', $user['password']);
$this->pressButton('Login');
}
}
API + UI Testing Combine with Laravel’s HTTP tests:
public function assertAPIAndUI($endpoint, $expectedUI) {
$response = Http::post('/api/' . $endpoint);
$this->visit('/dashboard');
$this->assertPageContains($expectedUI);
}
How can I help you explore Laravel packages today?