How to use HOOKS:
-----------------
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
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)
@smoke
Scenario
@Before("@smoke")
@Before(value="@smoke", order=1)
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
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;
En esta página del sitio puede ver el video en línea Selenium Cucumber Java BDD Framework Class 9: HOOKS de Duración hora minuto segunda en buena calidad , que subió el usuario Testing Tutorialspoint 23 mayo 2022, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 598 veces y le gustó 14 a los espectadores. Disfruta viendo!