Testing / Cucumber Interview Questions
What is the difference between @Before and @BeforeStep hooks?
@Before runs exactly once per scenario, immediately before its first step executes. @BeforeStep runs once before every individual step within that scenario, meaning it fires multiple times per scenario, once for each step in sequence.
| @Before | @BeforeStep |
| Runs once per scenario. | Runs once before every step in that scenario. |
| Good for scenario-level setup, like opening a browser or starting a transaction. | Good for per-step concerns, like logging each step or capturing state before every action. |
| Has no awareness of which specific step is about to run. | Receives information about the specific upcoming step. |
A common practical use of @BeforeStep/@AfterStep together is capturing a screenshot or logging state around every single step during a UI test, giving much finer-grained debugging evidence than a single before/after screenshot at the scenario boundary would provide when trying to pinpoint exactly which step's action caused an unexpected page state.
More Related questions...