react hooks class code example

Example: hook class

Cucumber supports hooks,
which are blocks of code that
run before or after each scenario.
You can define them anywhere in
your project or step definition layers,
using the methods @Before and @After. 
Cucumber Hooks allows us to better
manage the code workflow and helps us
to reduce the code redundancy.
We can say that it is an unseen step, 
which allows us to perform our scenarios or tests.

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.

@Before and @After annotations has to be imported from io.cucumber library.
	- import io.cucumber.java.After;
	- import io.cucumber.java.Before;

Why Cucumber Hooks?
In the world of testing, you must 
have encountered the situations
where you need to perform the prerequisite
steps before testing any test scenario.
This prerequisite can be anything from:

Launchinh WebDriver
Setting up DB connections
Setting up test data
Setting up browser cookies
Setting up implicit wait
Navigating to certain page
or anything before the test

In the same way there are always after
steps as well of the tests like:
Closing WebDriver
Closing DB connections
Clearing the test data
Clearing browser cookies
Logging out from the application
Printing reports or logs
Flushing Logger
Taking screenshots on an error
or anything after the test

To handle these kind of situations,
cucumber hooks are the best choice to use. 
Unlike TestNG Annotations, cucumber 
supports only two hooks (Before & After) 
which works at the start and the end of each
test scenario. As the name suggests,
@before hook gets executed well before 
each test scenario, and @after hook gets
executed after executing the scenario.