Skip to main content

Behat Tagged Hooks

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
Submitted by tgoswami on November 30, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.