Behat Tagged Hooks

Profile picture for user devraj

Sometimes you may want a certain hook to run only for certain scenarios or features. This can be achieved by associating a @BeforeFeature, @AfterFeature, @BeforeScenario, @AfterScenario with one or more tags.

So, if you want to execute before scenario or after scenario to be executed for your feature or scenario which is tagged with @login, you can do it like this:

/**
  * @BeforeFeature @login
*/
public static function setupFeature()
{
    echo "Before Feature Executed";
}

/**
  * @AfterFeature @login
*/
public static function teardownFeature()
{
    echo "After Feature Executed";
}

Similarly you can do it for scenario as well.

You can also use OR (||) to execute if scenario is tagged with @login or @smoke

/**
  * @BeforeScenario @login||@smoke
*/
public static function setupScenario()
{
    echo "Before Scenario Executed";
}

To execute if scenario is tagged with @login and @smoke, you can use AND (&&):

/**
  * @BeforeScenario @login&&@smoke
*/
public static function setupScenario()
{
    echo "Before Scenario Executed";
}
Tags