MinkExtension Documentation

repository·master·Indexed 20 days ago

https://github.com/behat/minkextension

An integration layer connecting Behat 3.0+ with Mink 1.5+ for web browser automation and session management. It provides MinkAwareContext for Mink instance access, RawMinkContext for custom contexts without predefined steps, and MinkContext for out-of-the-box step definitions and hooks. Supports various drivers including Goutte, Selenium, Selenium2, Sahi, and Zombie. Note: This package is no longer maintained; users are encouraged to migrate to friends-of-behat/mink-extension.

Tokens
8.1K
Snippets
25
Records
38
Agent score
71%

What's inside MinkExtension

  1. Understand the core features of MinkExtension

    master

    MinkExtension acts as an integration layer between Behat 3.0+ and Mink 1.5+. It provides three primary capabilities for Behat testing environments:

    1. Additional Behat Services: It registers Mink, Sessions, and Drivers as services within the Behat container.
    2. MinkAwareContext: A class (Behat\MinkExtension\Context\MinkAwareContext) that you can implement or extend to gain access to the Mink instance within your custom contexts.
    3. MinkContext: A base context (Behat\MinkExtension\Context\MinkContext) that provides ready-to-use step definitions and hooks. You can use it as a standalone context or as a base for your own subcontexts.
  2. Register and manage Mink sessions

    master

    You can register multiple Mink sessions in your behat.yml configuration, assigning a specific driver to each. MinkExtension determines which session to use for a scenario based on configuration settings and scenario tags.

    Session Selection Logic

    1. Scenario Tags:
      • A scenario tagged with @mink:foo uses the session named foo.
      • A scenario tagged with @javascript uses the session configured as the javascript_session.
      • All other scenarios use the default_session.
    2. Suite Configuration: You can override the default and javascript sessions for specific suites using mink_session and mink_javascript_session.
    3. Automatic Defaults (if not explicitly configured):
      • javascript_session: The first session in your configuration that uses a JavaScript-capable driver.
      • default_session: The first session using a non-JavaScript driver; if none exist, it defaults to the first JavaScript session.
    default:
        extensions:
            Behat\MinkExtension:
                sessions:
                    first_session:
                        selenium2: ~
                    second_session:
                        goutte: ~
                    third_session:
                        selenium2: ~
    
        suites:
            first:
                mink_session: foo
                mink_javascript_session: sahi
  3. Implement MinkAwareContext to access Mink

    master
    If you implement Behat\MinkExtension\Context\MinkAwareContext, the extension will automatically call setMink(Mink $mink) and setMinkParameters(array $parameters) on your context immediately after creation and before each scenario. This allows your context to use the preconfigured Mink instance based on your behat.yml settings.
  4. How to add a new translated language

    master

    To add a new translation to the extension, follow these steps:

    1. Create a reference file: Use the existing ru language file located in the translations folder as a template.
    2. Match naming conventions: The filename must match the translated language name used in both Behat and Gherkin.
    3. Check Gherkin compatibility: If the language does not exist in Gherkin, you must contribute the translation to the Gherkin repository first for it to work correctly.
  5. Configure available Mink drivers

    master

    MinkExtension supports several drivers. If you are using the Mink PHAR, these are bundled. If you are using Composer, you must install the specific driver packages first:

    • GoutteDriver: behat/mink-goutte-driver
    • SeleniumDriver: behat/mink-selenium-driver
    • Selenium2Driver (includes SauceLabs/BrowserStack): behat/mink-selenium2-driver
    • SahiDriver: behat/mink-sahi-driver
    • ZombieDriver: behat/mink-zombie-driver
  6. Configure GoutteDriver for HTTPS and self-signed certificates

    master

    When using GoutteDriver to test applications secured with HTTPS and self-signed certificates, you can bypass Guzzle validation errors by configuring guzzle_parameters in your behat.yml.

    For Guzzle 4 or later

    Use verify: false to disable SSL verification.

    For Guzzle 3 or earlier

    Use ssl.certificate_authority: false to disable SSL verification.

    # Guzzle 4+
    default:
        extensions:
            Behat\MinkExtension:
                sessions:
                    my_session:
                        goutte:
                            guzzle_parameters:
                                verify: false
  7. Extend RawMinkContext to create custom Mink-aware contexts

    master

    To create a custom Behat context that has direct access to the Mink session and web assertions, extend Behat\MinkExtension\Context\RawMinkContext. This class provides the underlying integration with the Mink session manager without providing pre-defined step definitions, allowing you to implement your own domain-specific language (DSL).

    Key capabilities provided by RawMinkContext include:

    • Accessing the Mink instance via getMink().
    • Accessing the current session via getSession().
    • Using web assertions via assertSession().
    • Navigating to URLs via visitPath().
    • Capturing screenshots via saveScreenshot().
    use Behat\MinkExtension\Context\RawMinkContext;
    
    class MyCustomContext extends RawMinkContext
    {
        /**
         * @Given I am on the home page
         */
        public function iAmOnTheHomePage()
        {
            // visitPath uses the 'base_url' parameter to resolve relative paths
            $this->visitPath('/');
        }
    
        /**
         * @Then I should see the welcome message
         */
        public function iShouldSeeTheWelcomeMessage()
        {
            // assertSession() returns a WebAssert instance
            $this->assertSession()->pageTextContains('Welcome!');
        }
    }
  8. Configure MinkExtension in behat.yml

    master

    After installation, activate the extension by adding its class name to the extensions section of your behat.yml file. You can define a base_url and configure specific sessions (e.g., goutte).

    # behat.yml
    default:
      # ...
      extensions:
        Behat\MinkExtension:
          base_url:  'http://example.com'
          sessions:
            default:
              goutte: ~
  9. Debug browser output with MinkContext

    master

    When a test fails, you can use these steps to inspect the current state in the console:

    • Then print current URL: Outputs the current URL to the console.
    • Then print last response: Outputs the current URL followed by the full HTML content of the page to the console.
    • Then show last response: Saves the current page HTML to a temporary file and opens it in a browser.

    Note: To use show last response, you must configure the show_cmd parameter in your behat.yml (e.g., show_cmd: firefox %s).

    Then print last response
    # Or if configured:
    Then show last response
  10. Navigate and manage browser state with MinkContext

    master

    Use the following step definitions in your Behat scenarios to navigate between pages and manage the browser session:

    • Navigate to a page:
      • Given I am on "/path/to/page" or When I go to "/path/to/page" (supports absolute URLs or relative paths).
      • Given I am on the homepage or When I go to the homepage (navigates to /).
    • Browser History:
      • When I reload the page (reloads the current URL).
      • When I move backward one page (browser back).
      • When I move forward one page (browser forward).
    Given I am on "/articles/isBatmanBruceWayne"
    When I reload the page
    And I move backward one page