Testing / Cucumber Interview Questions
Explain the lifecycle of hooks around a scenario's execution?
Cucumber runs several distinct categories of hooks at different points relative to a scenario and its individual steps, and understanding the exact ordering matters for reasoning about setup, teardown, and diagnostic capture correctly.
@Before hooks run once, before the scenario's first step, commonly used for setup like opening a browser session. Then, for every step in the scenario, @BeforeStep runs, the step itself executes, and @AfterStep runs, in that order, repeating for each subsequent step. Only after every step has been processed (successfully or not, since a failed step still triggers @AfterStep and eventually @After) do the scenario-level @After hooks run, commonly used for teardown like closing a browser or, notably, capturing a screenshot conditionally based on whether the scenario ultimately failed, since @After hooks receive the scenario's final status.
More Related questions...