Hooks are blocks of code that can run at various points in the Cucumber execution cycle. They are typically used for setup and teardown of the environment before and after each scenario.
See the reference documentation.
Scenario hooks run for every scenario.
Before
hooks run before the first step of each scenario.
Before() { scenario ->
// Do something before each scenario
}
// Or:
Before() {
// Do something before each scenario
}
After
hooks run after the last step of each scenario.
After() { scenario ->
// Do something after each scenario
}
// Or:
After() {
// Do something after each scenario
}
Step hooks invoked before and after a step.
BeforeStep() { scenario ->
// Do something before step
}
// Or:
BeforeStep() {
// Do something before step
}
AfterStep() { scenario ->
// Do something after step
}
// Or:
AfterStep() {
// Do something after step
}
Hooks can be conditionally selected for execution based on the tags of the scenario.
Before("@browser and not @headless") {
// Do something before each scenario with tag @browser but not @headless
}
You can define an order between multiple hooks.
Before(10) {
// Do something before each scenario
}
Before(20) {
// Do something before each scenario
}
The default order is 1000.
You mix up conditional and order hooks with following syntax:
Before("@browser and not @headless", 10) {
// Do something before each scenario
}