what is hook class

HOOKS

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.

4
8
Al Wei 70 points

                                    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.

4 (8 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source