Skip to main content
Home
  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact
programsbuzz facebook programsbuzz twitter programsbuzz linkedin
  • Log in

Main navigation

  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact

Cucumber Scenario Hooks

Profile picture for user devraj
Written by devraj on 08/30/2020 - 19:27

Hooks are blocks of code that can run at various points in the Cucumber execution cycle. Hooks allow us to better manage the code workflow and helps us to reduce the code redundancy. Hooks are used to perform prerequisite steps before testing any test scenario.

Multiple Types of Hooks Exists in Cucumber:

  • Scenario Hooks
  • Around Hooks
  • Step Hooks
  • Conditional or Tagged Hooks
  • Global Hooks

Around Hooks and Global Hooks are not supported by Cucumber JVM.

Cucumber supports 2 types of Scenario hooks @Before and @After. @Before executes before any test scenario and @After executed after the scenario. Method defined within Before and After hooks, always run, even if the scenario gets passed or failed.

In Before hook you can add code for starting WebDriver, browser, DB connection, cookies etc. In After hook you can close browser, DB connection and capture screenshots etc.

Hooks are defined in step definition files or package where step definition file is.

Think twice before you use Before:

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

Example: Cucumber Scenario Hooks

package com.pb.cucumbertest.stepdefinitions;
import com.pb.cucumbertest.helper.TestBase;
import io.cucumber.java.After;
import io.cucumber.java.Before;

@RunWith(Cucumber.class)
public class StepDefinitions extends TestBase
{	
	@Before
	public void bf()
	{
		TestBase tb = new TestBase();
		tb.setDriver();
	}

	@After
	public void af()
	{
		driver.quit();
	}
}

Here we have 2 Hooks in StepDefinition class. @Before hooks calling  method setDriver() of testbase class and @After hook calling method to close browser.

Now in our below feature file. @Before hook will be executed before each scenarios and @After hook will be executed after each scenario. I want to open my browser before every scenario and close it after every scenario. This step is repetitive and common to all the feature file and scenario inside it. So we have used hooks. Background execute after the @Before hook.

Here is the order of execution of Hooks, Background and Scenario:

1. Before Hook
2. Background
3. Scenario
4. After Hook

Feature: Registration, Login and MyAccount

  Background:
    Given I am on the homepage
    And I follow "Sign in"
  
  @smoke
  Scenario Outline: Verify Login Functionality
    And I fill "email address textbox" with "<email>"
    And I fill "password textbox" with "<password>"
    And I click "sign in button"
    Then I should see "<heading>" heading

    Examples: 
      | email                     | password | heading        |
      | goswami.tarun77@gmail.com | test1234 | MY ACCOUNT     |
      | wrongusername             | test     | AUTHENTICATION |

  @register
  Scenario: Create New User
    And I fill "registration email textbox" with "goswami.tarun77+1@gmail.com"
    Then I click "create an account button"
    And I enter following details
      | First Name | Tarun    |
      | Last Name  | Goswami  |
      | Password   | Test1234 |
      | Date       |       13 |
      | Year       |     1989 |
    And I click "register button"
Related Content
Cucumber Java Tutorial
Cucumber Step Hooks
Cucumber Tagged Hooks
Tags
Cucumber Java
  • Log in or register to post comments

Choose Your Technology

  1. Agile
  2. Apache Groovy
  3. Apache Hadoop
  4. Apache HBase
  5. Apache Spark
  6. Appium
  7. AutoIt
  8. AWS
  9. Behat
  10. Cucumber Java
  11. Cypress
  12. DBMS
  13. Drupal
  14. GitHub
  15. GitLab
  16. GoLang
  17. Gradle
  18. HTML
  19. ISTQB Foundation
  20. Java
  21. JavaScript
  22. JMeter
  23. JUnit
  24. Karate
  25. Kotlin
  26. LoadRunner
  27. matplotlib
  28. MongoDB
  29. MS SQL Server
  30. MySQL
  31. Nightwatch JS
  32. PactumJS
  33. PHP
  34. Playwright
  35. Playwright Java
  36. Playwright Python
  37. Postman
  38. Project Management
  39. Protractor
  40. PyDev
  41. Python
  42. Python NumPy
  43. Python Pandas
  44. Python Seaborn
  45. R Language
  46. REST Assured
  47. Ruby
  48. Selenide
© Copyright By iVagus Services Pvt. Ltd. 2023. All Rights Reserved.

Footer

  • Cookie Policy
  • Privacy Policy
  • Terms of Use