3

I was wondering if there's a possible way to parse dynamic values, specifically the name by using the *.feature Behat/Mink tests as it's running the into the Yml configuration file with Selenium2 Capabilities

The behat.yml file uses Behat/Mink/Extension extension file with Selenium 2 Capabilities parameters in the file.

    default:
      context:
        class:  'FeatureContext'
      extensions:
        Behat\MinkExtension\Extension:
         base_url: 
         javascript_session:  'selenium2'
         goutte:
         selenium2:
            browser: firefox
            wd_host: <sauce username>:<accesscode>@ondemand.saucelabs.com/wd/hub
            capabilities: {  "platform": "Windows 7" , "version": "21" , "name":"Test" }

Within the Extension.php file: https://github.com/Behat/MinkExtension/blob/2.0/src/Behat/MinkExtension/Extension.php

            arrayNode('selenium2')->
                children()->
                    scalarNode('browser')->
                        defaultValue(isset($config['selenium2']['browser']) ? $config['selenium2']['browser'] : '%behat.mink.browser_name%')->
                    end()->
                    arrayNode('capabilities')->
                        normalizeKeys(false)->
                        children()->
                            scalarNode('browserName')->
                                defaultValue(isset($config['selenium2']['capabilities']['browserName']) ? $config['selenium2']['capabilities']['browserName'] : 'firefox')->
                            end()->
                            scalarNode('version')->
                                defaultValue(isset($config['selenium2']['capabilities']['version']) ? $config['selenium2']['capabilities']['version'] : "9")->
                            end()->
                            scalarNode('platform')->
                                defaultValue(isset($config['selenium2']['capabilities']['platform']) ? $config['selenium2']['capabilities']['platform'] : 'ANY')->
                            end()->
                            scalarNode('browserVersion')->
                                defaultValue(isset($config['selenium2']['capabilities']['browserVersion']) ? $config['selenium2']['capabilities']['browserVersion'] : "9")->
                            end()->
                            scalarNode('browser')->
                                defaultValue(isset($config['selenium2']['capabilities']['browser']) ? $config['selenium2']['capabilities']['browser'] : 'firefox')->
                            end()->
                            scalarNode('ignoreZoomSetting')->
                                defaultValue(isset($config['selenium2']['capabilities']['ignoreZoomSetting']) ? $config['selenium2']['capabilities']['ignoreZoomSetting'] : 'false')->
                            end()->
                            scalarNode('name')->
                                defaultValue(isset($config['selenium2']['capabilities']['name']) ? $config['selenium2']['capabilities']['name'] : 'Behat Test')->
                            end()->
                            scalarNode('deviceOrientation')->
                                defaultValue(isset($config['selenium2']['capabilities']['deviceOrientation']) ? $config['selenium2']['capabilities']['deviceOrientation'] : 'portrait')->
                            end()->
                            scalarNode('deviceType')->
                                defaultValue(isset($config['selenium2']['capabilities']['deviceType']) ? $config['selenium2']['capabilities']['deviceType'] : 'tablet')->
                            end()->
                            scalarNode('selenium-version')->
                                defaultValue(isset($config['selenium2']['capabilities']['selenium-version']) ? $config['selenium2']['capabilities']['selenium-version'] : '2.31.0')->
                            end()->
                            scalarNode('max-duration')->
                                defaultValue(isset($config['selenium2']['capabilities']['max-duration']) ? $config['selenium2']['capabilities']['max-duration'] : '300')->
                            end()->
                            booleanNode('javascriptEnabled')->end()->
                            booleanNode('databaseEnabled')->end()->
                            booleanNode('locationContextEnabled')->end()->
                            booleanNode('applicationCacheEnabled')->end()->
                            booleanNode('browserConnectionEnabled')->end()->
                            booleanNode('webStorageEnabled')->end()->
                            booleanNode('rotatable')->end()->
                            booleanNode('acceptSslCerts')->end()->
                            booleanNode('nativeEvents')->end()->
                            booleanNode('passed')->end()->
                            booleanNode('record-video')->end()->
                            booleanNode('record-screenshots')->end()->
                            booleanNode('capture-html')->end()->
                            booleanNode('disable-popup-handler')->end()->
                            arrayNode('proxy')->
                                children()->
                                    scalarNode('proxyType')->end()->
                                    scalarNode('proxyAuthconfigUrl')->end()->
                                    scalarNode('ftpProxy')->end()->
                                    scalarNode('httpProxy')->end()->
                                    scalarNode('sslProxy')->end()->
                                end()->
                                validate()->
                                    ifTrue(function ($v) {
                                        return empty($v);
                                    })->
                                    thenUnset()->
                                end()->
                            end()->
                            arrayNode('firefox')->
                                children()->
                                    scalarNode('profile')->
                                        validate()->
                                        ifTrue(function ($v) {
                                            return !file_exists($v);
                                        })->
                                            thenInvalid('Cannot find profile zip file %s')->
                                        end()->
                                    end()->
                                    scalarNode('binary')->end()->
                                end()->
                            end()->
                            arrayNode('chrome')->
                                children()->
                                    arrayNode('switches')->
                                        prototype('scalar')->end()->
                                    end()->
                                    scalarNode('binary')->end()->
                                    arrayNode('extensions')->
                                        prototype('scalar')->end()->
                                    end()->
                                end()->
                            end()->
                        end()->
                    end()->
                    scalarNode('wd_host')->
                        defaultValue(isset($config['selenium2']['wd_host']) ? $config['selenium2']['wd_host'] : 'http://localhost:4444/wd/hub')->
                    end()->
                end()->
            end()->
            arrayNode('saucelabs')->
                children()->
                    scalarNode('username')->
                        defaultValue(getenv('SAUCE_USERNAME'))->
                    end()->
                    scalarNode('access_key')->
                        defaultValue(getenv('SAUCE_ACCESS_KEY'))->
                    end()->
                    booleanNode('connect')->
                        defaultValue(isset($config['saucelabs']['connect']) ? 'true' === $config['saucelabs']['connect'] : false)->
                    end()->
                    scalarNode('browser')->
                        defaultValue(isset($config['saucelabs']['browser']) ? $config['saucelabs']['browser'] : 'firefox')->
                    end()->
                    arrayNode('capabilities')->
                        children()->
                            scalarNode('name')->
                                defaultValue(isset($config['saucelabs']['name']) ? $config['saucelabs']['name'] : 'Behat feature suite')->
                            end()->
                            scalarNode('platform')->
                                defaultValue(isset($config['saucelabs']['platform']) ? $config['saucelabs']['platform'] : 'Linux')->
                            end()->
                            scalarNode('version')->
                                defaultValue(isset($config['saucelabs']['version']) ? $config['saucelabs']['version'] : '21')->
                            end()->
                            scalarNode('deviceType')->
                                defaultValue(isset($config['saucelabs']['deviceType']) ? $config['saucelabs']['deviceType'] : null)->
                            end()->
                            scalarNode('deviceOrientation')->
                                defaultValue(isset($config['saucelabs']['deviceOrientation']) ? $config['saucelabs']['deviceOrientation'] : null)->

Under Extension.php in the MinkExtension directory, it loads up the selenium2.xml file with a set of arrays to parse the information in that file onto the behat.yml.

I noticed that if there isn't any parameters from the Selenium2 capabilities specified in the yml file, the Extension.php will assign a default value based on the array name:

scalarNode('name')-> defaultValue(isset($config['selenium2']['capabilities']['name']) ? $config['selenium2']['capabilities']['name'] : 'Behat Test')->

With this said, I was wondering if there's any possible way to use the name of the Behat/Mink Feature tests <test>.feature within my local directory to parse a title within the name while the tests are running.

Ben Fong
  • 31
  • 4

1 Answers1

3

Late answer but maybe better than nothing...

How about using tags in your .feature tests? You can advise behat to execute only those tests with the given tag:

behat documentation - tags

If you want to execute your tests dynamically you could parse your tag for example through an ant-file:

<target name="test-behat">
    <exec dir="${basedir}" executable="bin/behat" failonerror="false">
        <arg line="--config ${basedir}/behat.yml --tags '${env.SELENIUM_TAG}'"/>
    </exec>
</target>

That means in behat-cli:

bin/behat --tags '@your_tag'

Start your ant-script manually:

ant -file <path to your ant-script> -D'SELENIUM_TAG'='@whatever_you_want'

For full automation you could start your ant-script (and your behat tests) e.g. via jenkins (or any other CI that can handle apache ant). You can also pass any parameters needed for your behat.yml, composer.json (if you use composer) and so on.

If you really want to execute only one .feature you can go the same way.

<target name="test-behat">
    <exec dir="${basedir}" executable="bin/behat" failonerror="false">
        <arg line="--config ${basedir}/behat.yml '${env.FEATURE_FILE}'"/>
    </exec>
</target>

That means in behat-cli:

bin/behat your.feature

Start it with

ant -file <path to your ant-script> -D'FEATURE_FILE'='your.feature'

Corresponding lines from behat --help:

behat [--many config params we don't need right here] [features]

Arguments:
features          Feature(s) to run. Could be:
                  - a dir (features/)
                  - a feature (*.feature)
                  - a scenario at specific line (*.feature:10).
                  - all scenarios at or after a specific line (*.feature:10-*).
                  - all scenarios at a line within a specific range (*.feature:10-20)