Selenium Cucumber Java BDD Framework 9 - Hooks | Step by Step
Automation Step by Step Automation Step by Step
529K subscribers
82,154 views
844

 Published On Oct 14, 2020

FREE Tutorials - https://automationstepbystep.com/
QUIZ - https://forms.gle/np9teC67jdaQeHmD9
1 - What are Hooks
2 - Why to use Hooks
3 - When to use Hooks
4 - How to create & use Hooks - Step by Step Demo
5 - Conditional Hooks
Useful Tips

What are HOOKS
Blocks of code that runs before OR after each scenario
Hooks in Cucumber are like Listeners in TestNG
Can define hooks by using annotations @Before @After

Scenario Hooks - runs before and after each scenario
Step Hooks - runs before and after each step
Conditional Hooks - hooks associated with tags for conditional execution

Why to use HOOKS
To manage the setup and teardown
To avoid rewriting the common setup or teardown actions
Allow better management of code workflow

When to use HOOKS
Whenever you have some common setup and teardown actions to be executed before each scenario

How to use HOOKS
Step 1 - Create a new or use an existing Feature File
Step 2 - Create the steps for the scenario in the feature file
Step 3 - Create setup and teardown methods and mark with annotation
@Before
@After
@BeforeSteps
@AfterSteps

Step 4 - Create new or use an existing TestRunner class
Step 5 - Run the TestRunner class and check execution

We can use Tags with Hooks - Conditional Hooks
Hooks can be conditionally selected for execution based on the tags of the scenario
To run a particular hook only for certain scenarios, you can associate a Before or After hook with a tag expression
Tags can be used with
@BeforeSteps
@AfterSteps
@After(value="@smoke", order=2)


Single tag
tags = {"@smoke"}

Multiple tags
Tags with AND OR conditions

tags = {"@smoke or @regression"}

tags = {"@smoke and @regression"}

tags = {"@smoke and not @regression"}

Skip or Ignore Tags

tags = {"(@smoke or @regression) and not @important"}

Ordering Hooks
We can use multiple Before and After hooks and also assign order of execution

@Before(order=0)
@Before(order=1)

Background
Whatever happens in hooks is invisible to people who only read the features
Only use hooks for low-level logic such as starting a browser or deleting data from a database.
You should consider using a background as a more explicit alternative, especially if the setup should be readable by non-technical people

Note - we will learn about background in next session

You can keep your function name anything
Hooks will get executed even if the test fails

You need to import the library
import io.cucumber.java.Before;
import io.cucumber.java.After;

You can support by Joining Automation Step By Step channel: https://www.youtube.com/automationste...

Every LIKE & SUBSCRIPTION gives me great motivation to keep working for you

You can support my mission for education by sharing this knowledge and helping as many people as you can

If my work has helped you, consider helping any animal near you, in any way you can.

_______ ONLINE COURSES TO LEARN _______
https://automationstepbystep.com/onli...

Never Stop Learning
Raghav

show more

Share/Embed