Selenium Cucumber Java BDD Framework Class 9: HOOKS

preview_player
Показать описание
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

@smoke
Scenario

Single tag

Multiple tags
Tags with AND OR conditions

Skip or Ignore Tags

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
Рекомендации по теме