Search code examples
bddbehat

How define parameters for behat scenarios


Is there any way to use parameteres/variables in behat scenarios?

For example:

Scenario: Description
    Given I am on "https://{{company_name}}.example.com/"
    When I click on "xxxxx"
    And I wait 2 seconds
    Then I should see {{company_name}}

and in behat.yml

default:
    extensions:
        Behat\MinkExtension\Extension:
            goutte: ~
            selenium2: ~

companyX:
    params:
        company_name: XXX
    extensions:
        Behat\MinkExtension\Extension:

companyY:
    params:
        company_name: YYY
    extensions:
        Behat\MinkExtension\Extension:

I think about using some template engine and render every set of tests separately, but maybe there is easier solution.


Solution

  • You cannot pass parameters to the .feature file. Just put the configuration inside behat.yml. Instead of using the step:

    Given I am on "https://{{company_name}}.example.com/"
    

    use:

    Given I am on homepage
    

    And set the url with 'base_url' in behat.yml:

    default:
        extensions:
            Behat\MinkExtension\Extension:
                goutte: ~
                selenium2: ~
    
    companyX:
        extensions:
            Behat\MinkExtension\Extension:
                base_url: https://companyx.example.com/
    
    companyY:
        extensions:
            Behat\MinkExtension\Extension:
                base_url: https://companyy.example.com/
    

    Then depending on how you run behat:

    behat --profile companyX
    behat --profile companyY
    

    a different url will be used.

    More info about this: http://docs.behat.org/guides/7.config.html#context