Tutorial 7 – Hooks in Cucumber BDD
What you will Learn :
About cucumber Hooks
Annotate tags with hooks
Practical demonstration
Multiple @Before with orders
Multiple @After with orders
Pass scenario object to the hooks’ method
@BeforeStep
@AfterStep
Annotate tags with hooks demo
References
About cucumber Hooks
We will discuss various hooks available in cucumber. We can define setup methods (example: initializing driver, launch browser, read cookies etc) and teardown methods (example: close the browser, close the database connection etc) in the forms of hooks. Unlike ‘Background’ keyword that we studied in previous tutorial, ‘Hooks’ are not part of feature files.
Hooks can be written in :
a) step definition file or
b) a separate configuration class
@Before hook defines setup methods and @After defines teardown methods.
@Before will be executed before each scenario
@After will be executed after every scenario
Similarly:
@BeforeStep will be executed before each step of the scenario
@AfterStep will be executed after every step of the scenario
We can also have multiple @Before annotations. We can define the orders. Let us say we have 2 @Before annotations. The annotation that has ‘order = 1’ will be executed first before every scenario and the annotation that has ‘order = 2’ will be executed next before every scenario:
@Before
launching the browser method (order = 1)
@Before
initializing database method (order = 2)
Similarly we can have:
@After
closing the browser method (order = 1)
@After
disconnecting the database method (order = 2)
Annotate tags with hooks
Let us say we have 2 ‘search’ scenarios tagged with @Smoke
@Smoke
Scenario: Search
@Smoke
Scenario: Advance Search
Let us also have another scenario tagged with @Regression
@Regression
Scenario: Mouse hover
Now, we can create @Before annotation and you can tag them with hooks. So we can say:
@Before(“@Smoke”)
So it will execute only those scenarios which are tagged with @Smoke
Practical demonstration
Create a package ‘Hooks’ under src/test/java. Inside the package, create a class ‘HooksDemo’
Write 2 methods inside the class, see below. Make sure that you import the classes from cucumber library
Let us look at the ‘Search’ feature that we had written in one of our previous tutorials. This feature has only 1 scenario
Now, before this particular scenario, @Before hook will be executed. After the scenario completion, @After hook will be executed.
What we have to do is that, go to our ‘Runner’ file (see below) and mention the ‘Hooks’ package name in the ‘glue’ options
Also, mention the name of the feature file
Save the file
Let us run the RunnerTest.java
Notice the console o/p. The @Before hook is executed before all the scenario steps and @After hook is executed after all the scenario steps are over
Let us now add one more scenario to our feature file
Save the file
Let us re-run the RunnerTest.java
Notice the console o/p. The @Before hook is executed before the starting of both the scenarios. Similarly, @After hook is executed after the completion of both the scenarios
Multiple @Before with orders
Let us have one more @Before hook with a different method, see below. Let us add the order number 1 and 2
Save the file
Let us re-run the RunnerTest.java
Notice the console o/p. The 2 @Before hook methods are executed as per the order given
Multiple @After with orders
Let us now have 3 @After methods with different orders. In the case of @After hook, the orders work in revers order. So order=3 will be executed first, than order=2, finally order=1
Save the file
Let us re-run the RunnerTest.java
Notice the console o/p. The 3 @After hook methods are executed. Order=3 is executed first and then order=2 and finally order=1
Pass scenario object to the hooks’ method
We can pass scenario object to a method, see below
Import ‘Scenario’ from io.cucumber.java
At runtime, we can now use this object to return us the scenario id, scenario name etc
So let us get the scenario name
Similarly let us do the same thing in for @After method
Save the file
Let us re-run the RunnerTest.java
Notice the console o/p. The respective scenario names are getting printed for the 2 scenarios
@BeforeStep
Let us write @BeforeStep
Save the file and run RunnerTest.java
Notice below that @BeforeStep is executed before each step
@AfterStep
Let us now write @AfterStep
Save the file and run RunnerTest.java
Notice below that @AfterStep is executed after each step
Annotate tags with hooks demo
In our feature file, let us tag the 2 scenarios by tagnames @Smoke and @Production
Save the file
In our hook file, comment all the methods and just retain @Before and @After hooks, see below. Now pass the tagname @Smoke to boh of these hooks
Save the file
Run the RunnerTest.java.
Notice below that @Before and @After hooks are executed only for the scenario tagged with @Smoke
Let us now change the tagname to @Production
Save the file
Run the RunnerTest.java.
Notice below that @Before and @After hooks are executed only for the scenario tagged with @Production
You will get the same results even if you define the ‘tags’ option in RunnerTest.java, see below
References
You can refer www.baeldung.com/java-cucumber-hooks for related documentation
Thank you for reading!