Search code examples
phpbehatminkscenarios

Behat run again a scenario programmatically


After a question on stackoverflow Parameters in Behat 3 Ian found a solution for pass to behat parameters.

For environment it's ok, but for country parameters I'm a bit confused. I have a feature like this :

Scenario Outline: Test if first link works
    Given I am on website "<country>"
    Then I visit the url "my-url"
    Then I click on first link

Examples:
    | country |
    | US      |
    | IT      |

This is what I have, and now I want to run something like :

env="stg" country="US,IT" php53 bin/behat --suite=mySuite

But with a scenario outline, the whole scenario is run for each country. I want to do the same thing, but with a list of country passed in parameters.

Maybe with the hook AfterScenario or something like that I can run again my whole scenario with a different configuration ?

Thanks !


Solution

  • That unfortunately isn't possible with standard tools, at least not without a headache and reinventing the wheel. An interesting question though!

    You might get this working by configuring the suites in your behat.yml. You should have a default suite with the base configuration, and a suite for each language:

    default:
        extensions:
            Behat\MinkExtension
    
        suites:
            default:
                paths: […]
                contexts: […]
    
            US:
                paths: […]
                contexts: […]
    
            IT:
                paths: […]
                contexts: […]
    

    And use a @beforeSuite hook:

    /**
     * @beforeSuite
     *
     * @param BeforeSuiteScope $scope
     */
    public static function setUpSuite(BeforeSuiteScope $scope)
    {
        $suiteName = $scope->getSpecificationIterator()->getSuite()->getName();
    
        // If the suite denotes one of the languages, then set it as the main language.
    }
    

    This solution has a few cons:

    1. You can't use suites for anything else than languages.
    2. A lot of config duplication.
    3. Setting the language from the hook will be a pain. You'll probably need to store it as a static property and before each Mink request is sent pass it in the header, so that your server knows which language to use. You'll need another hook for that – anyway, this is another story of pain.

    Another solution that comes to mind is to just use another PHP / Bash script that will sequentially run Behat with the country param. It also have some cons:

    1. If you use different Behat params each time, you'll probably need to add the logic for passing them down.
    2. There might be a problem with properly displaying Behat results when running each subtask.
    3. You still need to pass that language to your app somehow, the above suggestion should work though.

    As you see, this is not a trivial task. Personally I have a better belief in the second solution, though it might be more timely, it also gives space for a lot of customisation. On another hand, unless you have a completely different content / logic for each country, you might not need to go into such extremes. Doing a few targeted checks that language can be changed and that it changes as expected should be enough. Re-running same tests for 40 countries would take hours and won't add any significant value.