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;
Auf dieser Seite können Sie das Online-Video Selenium Cucumber Java BDD Framework Class 9: HOOKS mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Testing Tutorialspoint 23 Mai 2022 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 598 Mal angesehen und es wurde von 14 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!