QA Tutorials

What is Cucumber Hook?

What is Cucumber Hook?

Cucumber hook is a block of code which is defined with scenario in the step definition file by using its own annotations @before and @after. Cucumber Hook allows us to handle the code work flow better and helps in reducing the redundancy. Actually the Hook is mainly used to start or close the web driver session after a specific function. Therefore is not relevant to any scenario.

The need for a Hook

Some times when testing, we may face some circumstances where we need to perform some pre-requisite steps before the test scenario.

The pre-requisites which may be faced are:

  1. To start a web driver and set up database connections.
  2. Setting up test data and setting up browser cookies
  3. Navigating to certain page

To handle these pre-requisite steps we have to use some Cucumber Hook. In cucumber there are two types of Hooks:

@Before ,@After

We can use @Before hook with any function or method when we have to start a web driver.

We can use @After hook with any function or method when we have to close web driver.

Consider an example of a tagged Hook:

@Before ('@RegressionTest)   
@After ('@RegressionTest)  

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button