Behat Mink: 00 - Behat.yml Reference

More Info: http://docs.behat.org/en/v2.5/guides/7.config.html


behat.yml

  •  

Example

    $ behat --config custom-config.yml

     

    configuration variables

    %paths.base%
    %paths.bootstrap%
    %paths.features%
    
    %behat.paths.features%
    %behat.paths.bootstrap%
    %behat.paths.bootstrap%

     

    Conventions - parameters

    • for constructors that take parameters,
      • the hyphen '-' can be used if providing parameters in the correct order
      • or <param_name> : <param_value> can be used to specify params in ANY order
        • using the <param_name>, also allows specifying the 2nd or 3rd parameter without specifying the 1st ( where the constuctor has default values assigned )
    • ~
      • use default settings (ie no params passed )

    Since '-' is used to specify the parameter assignments, they must be in the correct order

    contexts:
        - MyAwesomeContext:
            - http://localhost:8080
            - /var/tmp
    

    Aligned four spaces from the context class itself.

    Using these named parameter assignments makes life easier as they can be in any order

    Arguments would be passed to the MyAwesomeContext constructor in the order they were specified here. If you are not happy with the idea of keeping an order of arguments in your head, you can use argument names instead:

    # behat.yml
    
    default:
        suites:
            default:
                contexts:
                    - MyAwesomeContext:
                        baseUrl: http://localhost:8080
                        tempPath: /var/tmp
    

     

    default: - default profile

    • Profile blocks allow for a default profile to be setup, then secondary profiles can merge, update, and append these settings independently
    • These profiles can be specified at the CLI with the -p or --profile switches

     

    Example

    • default:
    • custom_profile:
    • another_custom_profile
    # behat.yml
    default:
     paths:
    custom_profile:
      formatters:
    another_custom_profile:
      formatters:

    default:autoload:

    • at the profile level, only the autoload: block seems to work with %paths.base%
    • paths don't seem to work at the profile level
    default:
    #  autoload:
    #    - "%paths.base%/bootstrap"
      autoload: [ "%paths.base%/bootstrap" ]

     

    default:
    #  autoload:
    #    - "%paths.base%/bootstrap"
      autoload: [ "%paths.base%/bootstrap" ]
      suites:
        default:
          paths:
            features: "%paths.base%/../features/dev"
            #bootstrap: "%paths.base%/../features/bootstrap"
          contexts:
          - FeatureContext
          - Drupal\DrupalExtension\Context\DrupalContext
          - Drupal\DrupalExtension\Context\MinkContext
          - Drupal\DrupalExtension\Context\MessageContext
          - Drupal\DrupalExtension\Context\DrushContext

     

     

     


     

    default:paths - Feature & BootStrap Paths

    while the autoloader: option will load up classes for the entire yml file, paths: can be used at the suite: level

     

    • default:
      • suite
        • default
          • paths:
            • features: features
            • bootstrap: %paths.features%/bootstrap
      • suite_B
        • default
          • paths:
            • features: features/login
            • bootstrap: "%paths.features%/bootstrap2"

     

    Example

    # behat.yml
    default:
        suite:
            default:
                paths:
                    features: features
                    bootstrap: %paths.features%/bootstrap

     

     

     


    default:filters:tags:  - filering by tags

    • These filter parameters (name and tags) accept the same strings as the Behat --name or --tags parameters do.

     

    • default:
      • filters:
        • tags:
          • "@wip"

     

    Example

    # behat.yml
    
    default:
        gherkin:
            filters:
                tags: ~@wip

    CLI

    vendor/bin/behat --tags=wip

     


     

    default:formatter:

    • name:
      • pretty
      • could also write a classname here where a class can be a default output formatter & implement
        • Behat\Behat\Formatter\FormatterInterface
        • also would specify "parameters:" for this custom class
      • multiple output formats
        • pretty,junit,html
    • parameters
      • decorated:
        • true
      • verbose:
        • false
      • time:
        • true
      • language:
        • en
      • output_path:
        • null
      • multiline_arguments:
        • true

    Multiple Formatters Example

    ci:
      formatter:
        name:                   pretty,junit,html
        parameters:
          output_path:          null,junit,behat_report.html

     

    This will write pretty text output to the console, one XML file per feature to the junit directory and an HTML report to the file behat_report.html. See each formatter’s documentation for details on what parameters are available, optional or mandatory.


    default:formatter:parameters:output_styles: - Colors

    • undefined - style of undefined step
    • pending - style of pending step
    • pending_param - style of param in pending step
    • failed - style of failed step
    • failed_param - style of param in failed step
    • passed - style of passed step
    • passed_param - style of param in passed step
    • skipped - style of skipped step
    • skipped_param - style of param in skipped step
    • comment - style of comment
    • tag - style of scenario/feature tag

     

    Example

    default:
        formatter:
            name:               pretty
            parameters:
                output_styles:
                    comment: [ black, white, [ underscore ] ]
    • Available colors for first two arguments (fg and bg) are:
      • black
      • red
      • green
      • yellow
      • blue
      • magenta
      • cyan
      • white
    • Available options are:
      • bold
      • underscore
      • blink
      • reverse
      • conceal

    default:context: - Contexts

    • default:context:class:
      • Your\Custom\Class
      • defines the class used for the environment and
        • implements " Behat\Behat\Context\ContextInterface "
    • parameters:
      • https://test.mink.loc
      • simple array passed to the constructor of the context class when instantiated BEFORE EACH SCENARIO
        default:
          suites:
            default:
              contexts:
              - FeatureContext
              - Drupal\DrupalExtension\Context\DrupalContext
              - Drupal\DrupalExtension\Context\MinkContext
              - Drupal\DrupalExtension\Context\MessageContext
              - Drupal\DrupalExtension\Context\DrushContext

         

         

        DrupalExtenstion Contexts

        Adding Contexts to the classpath provides additional hooks and functionality to the test script

        • RawDrupalContext
          • A context that provides no step definitions, but all of the necessary functionality for interacting with Drupal, and with the browser via Mink sessions.
        • DrupalContext
          • Provides step-definitions for creating users, terms, and nodes.
        • MinkContext
          • Builds on top of the Mink Extension and adds steps specific to regions and forms.
        • MarkupContext
          • Contains step definitions that deal with low-level markup (such as tags, classes, and attributes).
        • MessageContext
          • Step-definitions that are specific to Drupal messages that get displayed (notice, warning, and error).
        • DrushContext
          • Allows steps to directly call drush commands.

         


        wip: - profiles

        • defines two additional profiles ( to default )
        • Every profile uses Your\Custom\Context as the environment class
        • wip: runs only @wip tagged scenarios
        • cu: run all features & output them with junit formatter to the /var/tmp/junit path
        behat --profile wip
        behat --profile ci

         

        • default:
          • context:
            • class:
              • Your\Custom\Context
        • wip:
          • filters:
            • tags:
              • "@wip"
          • formatter:
            • name:
              • progress
        • ci
          • formatter:
            • name:
              • junit
            • paramters;
              • output_path:
                • /var/tmp/junit

        Example

        # behat.yml
        default:
            context:
                class:      Your\Custom\Context
        wip:
            filters:
                tags:       "@wip"
            formatter:
                name:       progress
        ci:
            formatter:
                name:       junit
                parameters:
                    output_path: /var/tmp/junit

         


         

        extensions

        • In the example above, we activate 2 extensions depending on profile. mink profile will have activate MinkExtension and api profile will have WebApiExtension, but both of them will also have Symfony2Extension activated as any profile always inherit from default profile.
        • Extensions help you integrate Behat with frameworks and tools, that you might need to ease your test suite building.

         

         

        Example

        # behat.yml
        default:
          extensions:
            Behat\Symfony2Extension\Extension: ~
        
        mink:
          extensions:
            mink-extension.phar:
              base_url: http://domain.org
        
        api:
          extensions:
            Behat\WebApiExtension\Extension:
              base_url: http://api.domain.org

        Drupal\MinkExtension

         

         

         

         

        goutte:

        • goutte is the default & most basic webdriver for MinkExtension
        goutte: ~

         

        selenium2:

        • selenium2 will assign selenium as the webdriver, though phantomjs can be the standin browser driver
        selenium2: ~
        • selenium2: takes parameters

         

        browser:

         

        • specifies the browser driver to be used, but not sure how this works as MinkExtension seems to just send API calls to the wd_host.
        • perhaps there are browser driver implementations that don't use wd_host & so the browser driver type must be specified
              selenium2:
                browser: chrome

         

        wd_host:

        • specifies the URL, port and location of the webdriver MinkExtension will send api calls to
        • this is also presumably used for all the other webdriver options
              selenium2:
                # webdriver port/socket
                wd_host: "http://localhost:8643/wd/hub"

         

        phantomjs with selenium2

        • selenium2 can use chrome, firefox, phantomjs, (an IE? )
        • phantomjs is a headless browser with full javascript support
        • disable goutte
        • add wd_host
            Behat\MinkExtension:
              base_url: http://unityconstruct.org/uc/
              
        
              #goutte: ~
              # goutte/curl will take precedence when not commented
              #   so it is commented when using phantomjs
              
              selenium2:
                # webdriver port/socket
                wd_host: "http://localhost:8643/wd/hub"
                  
                # aparently has no effect when using phantomjs
                #browser: phantomjs

         

         

        base_url:

        • url that will be APPENDED to by MinkExtenstion
        • normally this will be the site root
          base_url: http://unityconstruct.org/uc/

           

          Example

            default:
              suites:
                default:
                  contexts:
                  - FeatureContext
                  - Drupal\DrupalExtension\Context\DrupalContext
                  - Drupal\DrupalExtension\Context\MinkContext
                  - Drupal\DrupalExtension\Context\MessageContext
                  - Drupal\DrupalExtension\Context\DrushContext
              extensions:
                Behat\MinkExtension:
                  goutte: ~
                  selenium2: ~
                  base_url: http://unityconstruct.org/uc/

             

             

            Drivers

            • default
              • extensions:
                • Behat\MinkExtension:
                  • sessions:
                    • session_GoutteDriver:
                      • goutte:~
                    • session_Guzzle4Driver
                      • goutte:
                        • guzzle_parameters:
                          • verify: false
                    • session_Guzzle3Driver
                      • goutte:
                        • guzzle_parameters:
                          • ssl.certificate_authority: false
                    • session_Selenium2Driver
                      • selenium2: ~
                    • session_SauceLabsDriver:
                      • sauce_labs: ~
                    • session_BrowserStackDriver:
                      • browser_stack: ~
                    • session_SeleniumDriver;
                      • selenium: ~
                    • session_SahiDriver:
                      • sahi:~
                    • session_ZombieDriver
                      • zombie:
                        • #specify the path tho the node_modules directory
                        • node_modules_path: /user/local/lib/node_modules
            • GoutteDriver - behat/mink-goutte-driver
            • SeleniumDriver - behat/mink-selenium-driver
            • Selenium2Driver (also used for SauceLabs and BrowserStack) - behat/mink-selenium2-driver
            • SahiDriver - behat/mink-sahi-driver
            • ZombieDriver - behat/mink-zombie-driver

             

            Examples

            • GoutteDriver - headless driver without JavaScript support. In order to use it, modify your behat.yml profile:

              default:
                  extensions:
                      Behat\MinkExtension:
                          sessions:
                              my_session:
                                  goutte: ~

              If you use Behat/Mink/Goutte to test your application, and want to test an application secured with HTTPS, but with a self-signed certificate, you can use the following parameters to avoid the validation error triggered by Guzzle:

              • For Guzzle 4 or later:

                default:
                    extensions:
                        Behat\MinkExtension:
                            sessions:
                                my_session:
                                    goutte:
                                        guzzle_parameters:
                                            verify: false
              • For Guzzle 3 or earlier:

                default:
                    extensions:
                        Behat\MinkExtension:
                            sessions:
                                my_session:
                                    goutte:
                                        guzzle_parameters:
                                            ssl.certificate_authority: false
            • Selenium2Driver - javascript driver. In order to use it, modify your behat.yml profile:

              default:
                  extensions:
                      Behat\MinkExtension:
                          sessions:
                              my_session:
                                  selenium2: ~
            • SauceLabsDriver - special flavor of the Selenium2Driver configured to use the selenium2 hosted installation of saucelabs.com. In order to use it, modify your behat.yml profile:

              default:
                  extensions:
                      Behat\MinkExtension:
                          sessions:
                              my_session:
                                  sauce_labs: ~
            • BrowserStackDriver - special flavor of the Selenium2Driver configured to use the selenium2 hosted installation of browserstack.com. In order to use it, modify your behat.yml profile:

              default:
                  extensions:
                      Behat\MinkExtension:
                          sessions:
                              my_session:
                                  browser_stack: ~
            • SeleniumDriver - javascript driver. In order to use it, modify your behat.yml profile:

              default:
                  extensions:
                      Behat\MinkExtension:
                          sessions:
                              my_session:
                                  selenium: ~
            • SahiDriver - javascript driver. In order to use it, modify your behat.yml profile:

              default:
                  extensions:
                      Behat\MinkExtension:
                          sessions:
                              my_session:
                                  sahi: ~
            • ZombieDriver - zombie.js javascript headless driver. In order to use it, modify your behat.yml profile:

              default:
                  extensions:
                      Behat\MinkExtension:
                          sessions:
                              default:
                                  zombie:
                                      # Specify the path to the node_modules directory.
                                      node_modules_path: /usr/local/lib/node_modules/

            Note

            The phar version of Mink comes bundled with all 5 drivers and you don't need to do anything except enabling them in order to use them.

            But if you're using Composer, you need to install drivers that you need first:

            • GoutteDriver - behat/mink-goutte-driver
            • SeleniumDriver - behat/mink-selenium-driver
            • Selenium2Driver (also used for SauceLabs and BrowserStack) - behat/mink-selenium2-driver
            • SahiDriver - behat/mink-sahi-driver
            • ZombieDriver - behat/mink-zombie-driver

            Note

            All drivers share the same API, which means that you could use multiple drivers for the same suite - whichever one fits your needs for concrete scenarios. Don't try to stick to a single driver as there's simply no universal solution - every driver has its pros and cons.

             

            Additional Parameters

            There's other useful parameters, that you can use to configure your suite:

            • base_url - if you're using relative paths in your *.feature files (and you should), then this option will define which url to use as a basename for them.
            • files_path - there's a special step definition for file upload inputs usage. You can use relative paths in those steps. files_path defines the base path in which Mink should search for those relative files.
            • show_cmd - there's a special definition in MinkExtension, that saves the currently opened page into a temporary file and opens it with some browser utility (for debugging). This option defines the command to be used for opening. For example: show_cmd: 'firefox %s'.
            • show_tmp_dir - the temporary folder used to show the opened page (defaults to the system temp dir)
            • show_auto - Whether the opened page should be shown automatically when a step fails.
            • browser_name - meta-option, that defines which browser to use for Sahi, Selenium and Selenium2 drivers.
            • default_session - defines the default session (driver) to be used for all untagged scenarios. This could be any enabled session name.
            • javascript_session - defines the javascript session (driver) (the one, which will be used for @javascript tagged scenarios). This could be any enabled session name.
            • mink_loader - path to a file loaded to make Mink available (useful when using the PHAR archive for Mink, useless when using Composer)

             

             


             

            Drupal\DrupalExtension

             

            • blackbox:
              • ~
            • region_map:
              • footer: "#footer"
            • text:
              • log_out: "Sign out"
            • drush:
              • alias:
                • 'local'
            • api_driver:
              • 'drupal'
            • drupal:
              • drupal_root:
                • '/var/www/seven/drupal'
            • subcontexts
              • paths:
                • - "/var/www/seven/drupal/sites/all"
              • autoload:
                • 0
                   

             

            blackbox: Selector engine(?)

            • blackbox appears to be a front end for finding selectors
            • not sure if any params are passed, but always see '~' assigned
             Drupal\DrupalExtension: 
                blackbox: ~

             

            region_map:

            • assign CSS selectors to 'regions' to limit the scope when searching for selectors

             

            Drupal\DrupalExtension:
                  blackbox: ~
                  region_map:
                    footer: "#footer"

             

            text:

            • Override text strings
            • The Drupal Extension relies on default text for certain steps.
            • If you have customized the label visible to users, you can change that text as follows:
            Drupal\DrupalExtension:
              text:
                log_out: "Sign out"
                log_in: "Sign in"
                password_field: "Enter your password"
                username_field: "Nickname"
            

             

            selectors:

            • Provide identifiers for CSS selectors
            • The Drupal Extension makes use of three selectors for message.
            • If your CSS values are different than the defaults (shown below), you’ll need to update your behat.yml file:
             Drupal\DrupalExtension:
               selectors:
                 message_selector: '.messages'
                 error_message_selector: '.messages.messages-error'
                 success_message_selector: '.messages.messages-status'

             

             

            api_driver: = drupal

            drupal:

              • used for implementing the Drupal API Driver

              drupal_root:

              • Path to Drupal (local sites only)
              • If you’ll only be running drush commands to access a site on the same machine, you can specify the path to your Drupal root:
                  Drupal\DrupalExtension:
                    blackbox: ~
                    api_driver: 'drupal' 
                    drupal: 
                      drupal_root: '/var/www/seven/drupal' 
                    region_map:
                      footer: "#footer"

               

              api_driver: = drush

              drush:

              • allows for drush commands to be sent to server
              • You can use the Drush driver to add users, reset passwords, and log in by following the steps below, again, without having to write custom PHP.
              • You can also do this with the Drupal API driver.
              • The main advantage of the Drush driver is that it can work when your tests run on a different server than the site being tested.

              api_driver:

              • specifies drush as the api driver
              • 'drupal' is the other available api

              alias:

              • used in php for db credentials

               

              <?php
              $aliases['local'] = array(
                'root' => '/var/www/seven/drupal',
                'uri'  =>  'seven.l'
              );
              $aliases['git7site'] = array(
                'uri'  =>  'git7site.devdrupal.org',
                'host' => 'git7site.devdrupal.org'
              );

               

              Example

                  Drupal\DrupalExtension:
                    blackbox: ~
                    api_driver: 'drush' 
                    drush:
                      alias: 'local'
                    region_map:
                      footer: "#footer"

               

               

              •  

               

               

                 

                 

                subcontexts:

                •  

                 

                autoload:

                        subcontexts:
                          paths:
                            - "/var/www/seven/drupal/sites/all"
                          autoload: 0
                    

                   

                   

                  Example

                   

                   

                      Drupal\DrupalExtension:
                        blackbox: ~
                        api_driver: 'drupal' 
                        drush:
                          alias: 'local'
                        drupal: 
                          drupal_root: '/var/www/seven/drupal' 
                        region_map:
                          footer: "#footer"
                        subcontexts:
                          paths:
                            - "/var/www/seven/drupal/sites/all"

                   


                  Behatch\Extension Contexts

                   

                   

                  # behat.yml
                  default:
                      # ...
                      extensions:
                          Behatch\Extension: ~

                   

                  default:
                      suites:
                          default:
                              contexts:
                                  - behatch:context:browser
                                  - behatch:context:debug
                                  - behatch:context:system
                                  - behatch:context:json
                                  - behatch:context:table
                                  - behatch:context:rest
                                  - behatch:context:xml

                   

                  Configuration

                  • browser - more browser related steps (like mink)
                    • timeout - default timeout
                  • debug - helper steps for debugging
                    • screenshotDir - the directory where store screenshots
                  • system - shell related steps
                    • root - the root directory of the filesystem
                  • json - JSON related steps
                    • evaluationMode - javascript "foo.bar" or php "foo->bar"
                  • table - play with HTML the tables
                  • rest - send GET, POST, ... requests and test the HTTP headers
                  • xml - XML related steps

                  Configuration Example

                  For example, if you want to change default directory to screenshots - you can do it this way:

                  default:
                      suites:
                          default:
                              contexts:
                                  - behatch:context:debug:
                                      screenshotDir: "var"

                   


                   

                  imports

                  • The imports block allows you to share your feature suite configuration between projects and their test suites
                  • All files from the imports block will be loaded by Behat and merged into your behat.yml config.

                   

                  Example

                  # behat.yml
                  imports:
                      - some_installed_pear_package_or_lib/behat.yml
                      - /full/path/to/custom_behat_config.yml

                  Environment Variable

                  • If you want to configure some system-wide Behat defaults, then BEHAT_PARAMS environment variable is right for you:
                  • You could setup default value for any option, that available for you in behat.yml.
                  • Just provide options in url format (parseable by parse_str() php function).
                  • Behat will use those options as default ones and you will always be able to redefine them with project behat.yml (it has higher priority).

                  Example

                  export BEHAT_PARAMS="formatter[name]=progress&context[parameters][base_url]=http://localhost"

                   

                   

                   

                   

                   

                   

                   

                   

                  Tags