1HOOKS
2
3Cucumber supports hooks, which are blocks of
4code that run before or after each scenario.
5You can define them anywhere in your project
6or step definition layers,
7using the methods @Before and @After.
8Cucumber Hooks allows us to better manage
9the code workflow and helps us to reduce
10the code redundancy. We can say that it is an unseen step,
11which allows us to perform our scenarios or tests.
12
13Hooks are blocks of code that can run at
14various points in the Cucumber execution cycle.
15They are typically used for setup and teardown of
16the environment before and after each scenario.
17
18@Before and @After annotations has to be
19imported from io.cucumber library.
20 - import io.cucumber.java.After;
21 - import io.cucumber.java.Before;
22
23Why Cucumber Hooks?
24In the world of testing, you must have
25encountered the situations where you need
26to perform the prerequisite steps before
27testing any test scenario. This prerequisite can be anything from:
28
29Launchinh WebDriver
30Setting up DB connections
31Setting up test data
32Setting up browser cookies
33Setting up implicit wait
34Navigating to certain page
35or anything before the test
36
37In the same way there are always after steps as well of the tests like:
38Closing WebDriver
39Closing DB connections
40Clearing the test data
41Clearing browser cookies
42Logging out from the application
43Printing reports or logs
44Flushing Logger
45Taking screenshots on an error
46or anything after the test
47
48To handle these kind of situations,
49cucumber hooks are the best choice to use.
50Unlike TestNG Annotations, cucumber supports only two hooks
51(Before & After) which works at the start
52and the end of each test scenario.
53As the name suggests, @before hook gets executed
54well before each test scenario, and @after hook
55gets executed after executing the scenario.
56
1HOOKS
2Hooks class is used to create pre and post condition
3for scenarios and even steps.
4Cucumber supports hooks.
5You can define them anywhere in your project
6or step definition layers,
7using the methods @Before and @After.
8@Before, @BeforeStep -> webdriver setup, maximize browser,
9database connection setup
10@After , , @AfterStep
11-> we can close any connection we open.(driver, browser)
12
13Cucumber Hooks allows us to better manage
14the code workflow and helps us to reduce
15the code redundancy. We can say that it is an unseen step,
16which allows us to perform our scenarios or tests.
17
18Hooks are blocks of code that can run at
19various points in the Cucumber execution cycle.
20They are typically used for setup and teardown of
21the environment before and after each scenario.
22
23@Before and @After annotations has to be
24imported from io.cucumber library.
25 - import io.cucumber.java.After;
26 - import io.cucumber.java.Before;
27
28To handle these kind of situations,
29cucumber hooks are the best choice to use.
30Unlike TestNG Annotations, cucumber supports only two hooks
31(Before & After) which works at the start
32and the end of each test scenario.
33As the name suggests, @before hook gets executed
34well before each test scenario, and @after hook
35gets executed after executing the scenario.