What is Cucumber Hook?
Cucumber hook is a block of code which is defined with scenario in the step definition file by using its own annotations @before and @after. Cucumber Hook allows us to handle the code work flow better and helps in reducing the redundancy. Actually the Hook is mainly used to start or close the web driver session after a specific function. Therefore is not relevant to any scenario.
The need for a Hook
Some times when testing, we may face some circumstances where we need to perform some pre-requisite steps before the test scenario.
The pre-requisites which may be faced are:
- To start a web driver and set up database connections.
- Setting up test data and setting up browser cookies
- Navigating to certain page
To handle these pre-requisite steps we have to use some Cucumber Hook. In cucumber there are two types of Hooks:
We can use @Before hook with any function or method when we have to start a web driver.
We can use @After hook with any function or method when we have to close web driver.
Consider an example of a tagged Hook: