Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Mink Extension Laravel Package

behat/mink-extension

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Replacement Package (since this is archived):
    composer require friends-of-behat/mink-extension
    
  2. Configure behat.yml:
    default:
      extensions:
        FriendsOfBehat\MinkExtension:
          base_url: 'http://localhost'
          browser_name: chrome
          sessions:
            default: symfony2
    
  3. First Use Case: Create a feature file (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
    

Implementation Patterns

Core Workflows

  1. Session Management:

    • Use createSession() to dynamically create sessions (e.g., for parallel testing):
      $session = $this->createSession('selenium2');
      $session->visit('/dashboard');
      
    • Switch between sessions with $this->getSession().
  2. Element Interaction:

    • Chain methods for fluent assertions:
      $this->assertSession()->elementExists('css', '.user-menu')->isVisible();
      
    • Fill forms with fillField() and submit with pressButton().
  3. 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);
    }
    

Integration Tips

  • 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
    

Gotchas and Tips

Pitfalls

  1. Session Leaks:

    • Issue: Unclosed sessions cause port exhaustion in Selenium.
    • Fix: Explicitly call $session->close() in AfterScenario hooks or use MinkContext::tearDown():
      use Behat\Testwork\Hook\Scope\AfterScenarioScope;
      
      public function afterScenario(AfterScenarioScope $scope) {
          $this->getSession()->close();
      }
      
  2. Base URL Overrides:

    • Issue: base_url in behat.yml is ignored if visit() uses absolute paths.
    • Fix: Use visitPath() for relative paths or override dynamically:
      $this->getSession()->getDriver()->setBaseUrl('https://staging.example.com');
      
  3. Deprecated Methods:

    • Avoid getMink(); use $this->getSession() or $this->getMink() (deprecated in v2.0+).
    • Prefer assertSession()->[method] over direct assert*() calls.

Debugging

  • Enable Mink Debug:

    FriendsOfBehat\MinkExtension:
      debug: true
    

    Logs session details to behat.log.

  • Selenium Grid Issues:

    • Ensure hub_url is configured in behat.yml:
      selenium2:
        wd_host: http://hub.example.com/wd/hub
      
    • Use --stop-on-failure flag to isolate flaky tests:
      behat --stop-on-failure
      

Extension Points

  1. 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);
        }
    }
    
  2. 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',
            ],
        ]);
    }
    
  3. Translation Overrides: Override language files in vendor/friends-of-behat/mink-extension/i18n/ by copying them to your project’s i18n/ directory.

Laravel-Specific Quirks

  • Session Persistence: Laravel’s session may conflict with Mink’s. Clear sessions in BeforeScenario:
    public function beforeScenario() {
        \Illuminate\Support\Facades\Session::flush();
    }
    
  • Asset URLs: Use mix() or asset() helpers in behat.yml for local development:
    base_url: 'http://localhost:8000'
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui