Scenario Hooks

Scenario hooks

As the name suggests, they would allow us to hook some piece of code before and after running any scenario in our framework.

They are somewhat similar to Background but the scope of a Background is limited only to a specific feature file. Whereas, the hooks are applicable to all scenarios across all the feature files.

They can be used in 2 ways:

  • @Before
  • @After

@Before when specified runs before executing any scenario.

@After when specified runs right after executing any scenario irrespecive of the scenario's pass/fail status.

Consider the following code to understand the hooks.

public class StartingSteps extends DriverFactory {


    @Given("^the user is on landing page$")
    public void setup() throws Throwable {
        driver.get("http://accountsdemo.herokuapp.com");
        driver.manage().window().maximize();
    }

    @Before
    public void beforeScenario(){
        System.out.println("this will run before the actual scenario");
    }

    @After
    public void afterScenario(){
        System.out.println("this will run after scneario is finished, even if it failed");
    }

}

Now run the test again to see the output from the hooks.

results matching ""

    No results matching ""