Book Image

Cucumber Cookbook

By : Shankar Garg
Book Image

Cucumber Cookbook

By: Shankar Garg

Overview of this book

Table of Contents (13 chapters)
Cucumber Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Tagging the Hooks


What if we want to execute some piece of code only before certain Scenarios and not before all Scenarios? Consider a situation where we want to invoke the Selenium Webdriver for the Scenarios related to browser automation and the REST Client code for Scenarios related to REST Services automation. How do we do this in Cucumber? Let's see this in the next section.

Getting ready

For this recipe, we are going to use update home_page.feature like this:

Feature: Home Page

  Background: flow till home page
    Given user is on Application home page

  @web
  Scenario: Home Page Default content on Web
    Then user gets a GitHub Bootcamp section

  @rest
  Scenario: GitHub account REST Service
    When user sends a GET request

How to do it…

  1. Update the code of the Hooks.java class with the following code:

    package com.automation;
    
    import org.openqa.selenium.WebDriver;
    
    import cucumber.api.java.After;
    import cucumber.api.java.Before;
    
    public class Hooks {
    
      public static WebDriver driver...